added reverser files
This commit is contained in:
parent
97072d506e
commit
23977eacb3
9
reverser/Cargo.toml
Normal file
9
reverser/Cargo.toml
Normal file
@ -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"] }
|
1
reverser/src/api/mod.rs
Normal file
1
reverser/src/api/mod.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
pub mod stringer;
|
20
reverser/src/api/stringer.rs
Normal file
20
reverser/src/api/stringer.rs
Normal file
@ -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;
|
||||||
|
}
|
72
reverser/src/main.rs
Normal file
72
reverser/src/main.rs
Normal file
@ -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<Commands>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Subcommand)]
|
||||||
|
|
||||||
|
enum Commands {
|
||||||
|
/// Reverses a string
|
||||||
|
Reverse(Reverse),
|
||||||
|
/// Inspects a string
|
||||||
|
Inspect(Inspect),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Args)]
|
||||||
|
struct Reverse {
|
||||||
|
/// The string to reverse
|
||||||
|
string: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Args)]
|
||||||
|
struct Inspect {
|
||||||
|
/// The string to inspect
|
||||||
|
string: Option<String>,
|
||||||
|
|
||||||
|
#[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 => {}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user