From 23977eacb34ea448919ac8d515c3d9f1693bb375 Mon Sep 17 00:00:00 2001 From: Dom Sec <54043553+dom-sec@users.noreply.github.com> Date: Wed, 22 Mar 2023 19:49:40 -0400 Subject: [PATCH] added reverser files --- reverser/Cargo.toml | 9 +++++ reverser/src/api/mod.rs | 1 + reverser/src/api/stringer.rs | 20 ++++++++++ reverser/src/main.rs | 72 ++++++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+) create mode 100644 reverser/Cargo.toml create mode 100644 reverser/src/api/mod.rs create mode 100644 reverser/src/api/stringer.rs create mode 100644 reverser/src/main.rs diff --git a/reverser/Cargo.toml b/reverser/Cargo.toml new file mode 100644 index 0000000..cb91a4c --- /dev/null +++ b/reverser/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "reverser" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +clap = { version = "4.1.11", features = ["derive"] } diff --git a/reverser/src/api/mod.rs b/reverser/src/api/mod.rs new file mode 100644 index 0000000..27618bc --- /dev/null +++ b/reverser/src/api/mod.rs @@ -0,0 +1 @@ +pub mod stringer; diff --git a/reverser/src/api/stringer.rs b/reverser/src/api/stringer.rs new file mode 100644 index 0000000..3b4ce7b --- /dev/null +++ b/reverser/src/api/stringer.rs @@ -0,0 +1,20 @@ +pub fn reverse(input: &String) -> String { + return input.chars().rev().collect(); +} + +pub fn inspect(input: &String, digits: bool) -> (i32, String) { + if !digits { + return (input.len() as i32, String::from("char")); + } + return (inspect_numbers(input), String::from("digit")); +} + +fn inspect_numbers(input: &String) -> i32 { + let mut count = 0; + for c in input.chars() { + if c.is_digit(10) { + count += 1; + } + } + return count; +} diff --git a/reverser/src/main.rs b/reverser/src/main.rs new file mode 100644 index 0000000..440eef2 --- /dev/null +++ b/reverser/src/main.rs @@ -0,0 +1,72 @@ +use ::clap::{Parser, Subcommand, Args}; + +mod api; + +#[derive(Parser)] +#[command(author, version)] +#[command(about = "stringer - a simple CLI to reverse strings", long_about = "stringer is a simple, nimble cli for reversing and inspecting strings")] + +struct Cli { + #[command(subcommand)] + command: Option, +} + +#[derive(Subcommand)] + +enum Commands { + /// Reverses a string + Reverse(Reverse), + /// Inspects a string + Inspect(Inspect), +} + +#[derive(Args)] +struct Reverse { + /// The string to reverse + string: Option, +} + +#[derive(Args)] +struct Inspect { + /// The string to inspect + string: Option, + + #[arg(short = 'd', long = "digits")] + only_digits: bool, +} + +fn main() { + let cli = Cli::parse(); + + match &cli.command { + Some(Commands::Reverse(name)) => { + match name.string { + Some(ref _name) => { + let reverse = api::stringer::reverse(_name); + println!("{}", reverse); + } + None => { + println!("Please provide a string to reverse"); + } + } + } + Some(Commands::Inspect(name)) => { + match name.string { + Some(ref _name) => { + let (res, kind) = api::stringer::inspect(_name, name.only_digits); + + let mut plural_s = "s"; + if res == 1 { + plural_s = ""; + } + + println!("{:?} has {} {}{}.", _name, res, kind, plural_s); + } + None => { + println!("Please provide a string to inspect"); + } + } + } + None => {} + } +}