Skip to content

Commit

Permalink
Display num of digits in cli
Browse files Browse the repository at this point in the history
  • Loading branch information
hesampakdaman committed Apr 20, 2024
1 parent 1cf4151 commit 20f893f
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn cli(args: &[String]) -> Result<String, CliErr> {
let input = parse(args)?;
let cmd_map = CommandMap::default();
match cmd_map.get(&input.command_name) {
Some(cmd) => Ok(cmd.run(&input.number)),
Some(cmd) => Ok(format!("{}\n{}", input, cmd.run(&input.number))),
None => Err(CliErr::CommandNotFound(cmd_map.available_commands())),
}
}
Expand All @@ -31,15 +31,27 @@ fn parse(args: &[String]) -> Result<ParsedInput, CliErr> {
let n: U256 = args[2].parse().map_err(|_| CliErr::ParseIntErr)?;
Ok(ParsedInput {
number: n,
digit_len: args[2].len(),
command_name,
})
}

struct ParsedInput {
number: U256,
digit_len: usize,
command_name: String,
}

impl std::fmt::Display for ParsedInput {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Using command {} with number {} ({} digits)",
self.command_name, self.number, self.digit_len
)
}
}

#[derive(PartialEq, Debug)]
enum CliErr {
CommandNotFound(String),
Expand Down

0 comments on commit 20f893f

Please sign in to comment.