From ff26eaaa4739c086653e56d8936616e25bc11ab1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Rigo?= Date: Thu, 17 Oct 2024 23:11:31 +0200 Subject: [PATCH] base: fix stdin parsing --- src/baseapp.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/baseapp.rs b/src/baseapp.rs index 6f19f7c..567b54b 100644 --- a/src/baseapp.rs +++ b/src/baseapp.rs @@ -4,6 +4,8 @@ use clap::{arg, Command}; use num_bigint::BigUint; use num_traits::Num; +use crate::applet::SliceExt; + pub struct BaseIntApplet { source_radix: Option, target_radix: u32, @@ -50,13 +52,16 @@ impl Applet for BaseIntApplet { } fn process(&self, val: Vec) -> Result> { + // Remove whitespace to make conversions work with stdin input + let val = val.trim(); + let (srcrad, int) = if let Some(src) = self.source_radix { ( src, BigUint::parse_bytes(&val, src).context("Could not convert input")?, ) } else { - let int_str = String::from_utf8(val).context("Could not convert value to string")?; + let int_str = String::from_utf8_lossy(val); // Base was not specified, check standard prefixes if int_str.len() > 2 && &int_str[0..2] == "0x" { @@ -122,6 +127,17 @@ mod tests { .success(); } + #[test] + fn test_base_cli_stdin() { + assert_cmd::Command::cargo_bin("rsbkb") + .expect("Could not run binary") + .args(&["base"]) + .write_stdin("0xA\n") + .assert() + .stdout("10") + .success(); + } + #[test] fn test_base_cli_arg_to() { assert_cmd::Command::cargo_bin("rsbkb")