From 0ebb79eba3d24f4be632c3d64a41094273195298 Mon Sep 17 00:00:00 2001 From: akhildevelops Date: Wed, 18 Dec 2024 17:50:14 +0530 Subject: [PATCH 1/8] cli interface --- .github/workflows/rust.yml | 17 ++++++- README.md | 23 ++++++++- fake/Cargo.toml | 10 +++- fake/src/bin/cli/fake_gen.rs | 88 +++++++++++++++++++++++++++++++++ fake/src/bin/cli/main.rs | 81 ++++++++++++++++++++++++++++++ fake/src/bin/cli/names.rs | 1 + fake/src/bin/cli/subcommands.rs | 30 +++++++++++ 7 files changed, 246 insertions(+), 4 deletions(-) create mode 100644 fake/src/bin/cli/fake_gen.rs create mode 100644 fake/src/bin/cli/main.rs create mode 100644 fake/src/bin/cli/names.rs create mode 100644 fake/src/bin/cli/subcommands.rs diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 6966732..edcaf82 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -53,4 +53,19 @@ jobs: - uses: actions-rs/cargo@v1 with: command: clippy - args: --workspace --tests --examples -- -D warnings \ No newline at end of file + args: --workspace --tests --examples -- -D warnings + cli: + name: Fake-CLI + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: actions-rs/cargo@v1 + with: + command: build + args: --release --bin cli --features clap + - name: Upload Bin Artifact + uses: actions/upload-artifact@v4 + with: + name: fake + path: target/release/cli + diff --git a/README.md b/README.md index dea7009..5ff6cfb 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [![Docs Status](https://docs.rs/fake/badge.svg)](https://docs.rs/fake) [![Latest Version](https://img.shields.io/crates/v/fake.svg)](https://crates.io/crates/fake) -A Rust library for generating fake data. +A Rust library and command line tool for generating fake data. ## Installation @@ -15,7 +15,7 @@ Default: fake = { version = "3.0.1", features = ["derive"] } ``` -Available features: +Available library features: - `derive`: if you want to use `#[derive(Dummy)]` - supported crates feature flags: @@ -40,6 +40,7 @@ Available features: ## Usage +### In rust code ```rust use fake::{Dummy, Fake, Faker}; use rand::rngs::StdRng; @@ -107,6 +108,24 @@ fn main() { } ``` +## Command line +```shell +Usage: cli [OPTIONS] [COMMAND] + +Commands: + Name + FirstName + CityPrefix + Password + help + +Options: + -r, --repeat [default: 1] + -l, --locale [default: EN] + -h, --help Print help + -V, --version Print version +``` + # Fakers with locale ## Lorem diff --git a/fake/Cargo.toml b/fake/Cargo.toml index c21834a..be3efb6 100644 --- a/fake/Cargo.toml +++ b/fake/Cargo.toml @@ -2,7 +2,7 @@ name = "fake" version = "3.0.1" authors = ["cksac "] -description = "An easy to use library for generating fake data like name, number, address, lorem, dates, etc." +description = "An easy to use library and command line for generating fake data like name, number, address, lorem, dates, etc." keywords = ["faker", "data", "generator", "random"] license = "MIT OR Apache-2.0" readme = "README.md" @@ -40,6 +40,7 @@ url-escape = { version = "0.1", optional = true } bson = { version = "2", optional = true } url = { version = "2", optional = true } indexmap = { version = "2", optional = true} +clap = { version = "4.0.32", optional = true, features=["cargo"] } [dev-dependencies] chrono = { version = "0.4", features = ["clock"], default-features = false } @@ -59,6 +60,7 @@ bigdecimal = ["bigdecimal-rs", "rust_decimal"] geo = ["geo-types", "num-traits"] http = ["dep:http", "url-escape"] bson_oid = ["bson"] +clap = ["dep:clap"] [[example]] name = "basic" @@ -81,3 +83,9 @@ required-features = [ name = "usage" path = "examples/usage.rs" required-features = ["derive"] + +[[bin]] +name = "cli" +required-features = [ + "clap" +] diff --git a/fake/src/bin/cli/fake_gen.rs b/fake/src/bin/cli/fake_gen.rs new file mode 100644 index 0000000..768820b --- /dev/null +++ b/fake/src/bin/cli/fake_gen.rs @@ -0,0 +1,88 @@ +use clap::{builder::StyledStr, ArgMatches}; +use fake::{faker, Fake}; +use rand::Rng; +#[derive(Clone, Copy, Debug)] +#[allow(non_camel_case_types)] +pub enum AVAILABLE_LOCALES { + EN, + FR_FR, + ZH_TW, + ZH_CN, + JA_JP, + AR_SA, + PT_BR, +} + +macro_rules! some_rules { + ($locale:expr, $module:ident, $fake:ident($($arg:ident)?)) => { + + match $locale { + AVAILABLE_LOCALES::EN => { + let s = faker::$module::en::$fake($($arg)?); + Box::new(move |rng: &mut R| s.fake_with_rng(rng)) + + } + AVAILABLE_LOCALES::FR_FR => { + let s = faker::$module::fr_fr::$fake($($arg)?); + Box::new(move |rng: &mut R| s.fake_with_rng(rng)) + + } + AVAILABLE_LOCALES::ZH_TW => { + let s = faker::$module::zh_tw::$fake($($arg)?); + Box::new(move |rng: &mut R| s.fake_with_rng(rng)) + + } + AVAILABLE_LOCALES::ZH_CN => { + let s = faker::$module::zh_cn::$fake($($arg)?); + Box::new(move |rng: &mut R| s.fake_with_rng(rng)) + + } + AVAILABLE_LOCALES::AR_SA => { + let s = faker::$module::ar_sa::$fake($($arg)?); + Box::new(move |rng: &mut R| s.fake_with_rng(rng)) + + } + AVAILABLE_LOCALES::JA_JP => { + let s = faker::$module::ja_jp::$fake($($arg)?); + Box::new(move |rng: &mut R| s.fake_with_rng(rng)) + + } + AVAILABLE_LOCALES::PT_BR => { + let s = faker::$module::pt_br::$fake($($arg)?); + Box::new(move |rng: &mut R| s.fake_with_rng(rng)) + + } + } + }; +} + +pub fn fake_generator( + matches: ArgMatches, + locale: AVAILABLE_LOCALES, + help_message: StyledStr, +) -> Box String> +where + R: Rng, +{ + match matches.subcommand() { + Some(("FirstName", _)) => { + some_rules!(locale, name, FirstName()) + } + Some(("Name", _)) => { + some_rules!(locale, name, Name()) + } + Some(("CityPrefix", _)) => { + some_rules!(locale, address, CityPrefix()) + } + Some(("Password", matches)) => { + let min = *matches.get_one::("min").unwrap(); + let max = *matches.get_one::("max").unwrap(); + let range = min..max; + some_rules!(locale, internet, Password(range)) + } + _ => { + println!("Didn't receive subcommand\n {}", help_message); + std::process::exit(0) + } + } +} diff --git a/fake/src/bin/cli/main.rs b/fake/src/bin/cli/main.rs new file mode 100644 index 0000000..76f2b9c --- /dev/null +++ b/fake/src/bin/cli/main.rs @@ -0,0 +1,81 @@ +use clap::{command, value_parser, Arg}; +use rand::Rng; +use std::io::{self, Write}; + +mod fake_gen; +#[allow(non_upper_case_globals)] +mod names; +mod subcommands; + +const AVAILABLE_LOCALES: [&str; 7] = ["en", "fr_fr", "zh_tw", "zh_cn", "ja_jp", "ar_sa", "pt_br"]; + +pub use fake_gen::{fake_generator, AVAILABLE_LOCALES}; +pub fn main() { + let stdout = io::stdout(); + let mut buf_stdout = io::BufWriter::new(stdout); + + let mut thread_rng = rand::thread_rng(); + let args = cli_parser(); + + writeln!( + buf_stdout, + "Generating {} fakes for {:?} locale", + args.0.repeats, args.0.locale + ) + .unwrap(); + + (0..args.0.repeats).for_each(|_| writeln!(buf_stdout, "{}", args.1(&mut thread_rng)).unwrap()); +} + +impl TryFrom<&str> for AVAILABLE_LOCALES { + type Error = String; + fn try_from(str_val: &str) -> Result { + let str_val = str_val.to_lowercase(); + let variant = match str_val.as_str(){ + "en" => AVAILABLE_LOCALES::EN, + "fr_fr" => AVAILABLE_LOCALES::FR_FR, + "zh_tw" => AVAILABLE_LOCALES::ZH_TW, + "zh_cn" => AVAILABLE_LOCALES::ZH_CN, + "ja_jp" => AVAILABLE_LOCALES::JA_JP, + "ar_sa" => AVAILABLE_LOCALES::AR_SA, + "pt_br" => AVAILABLE_LOCALES::PT_BR, + _=> return Err(format!("{} is either an invalid locale or not yet supported.\n The supported locales are: {:?}",str_val,AVAILABLE_LOCALES)) + }; + Ok(variant) + } +} + +fn cli_parser() -> (Args, impl Fn(&mut R) -> String) { + let mut command = command!() + .arg( + Arg::new("repeat") + .long("repeat") + .short('r') + .default_value("1") + .value_parser(value_parser!(u32)), + ) + .arg( + Arg::new("locale") + .short('l') + .long("locale") + .default_value("EN") + .value_parser(|value: &str| AVAILABLE_LOCALES::try_from(value)), + ) + .subcommands(subcommands::all_fakegen_commands()) + .arg_required_else_help(true); + let help_message = command.render_help(); + let matches = command.get_matches(); + let repeats = *matches.get_one::("repeat").unwrap(); + let locale = matches + .get_one::("locale") + .unwrap() + .to_owned(); + + let fake_gen = fake_generator::(matches, locale, help_message); + (Args { repeats, locale }, fake_gen) +} + +struct Args { + repeats: u32, + locale: AVAILABLE_LOCALES, +} diff --git a/fake/src/bin/cli/names.rs b/fake/src/bin/cli/names.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/fake/src/bin/cli/names.rs @@ -0,0 +1 @@ + diff --git a/fake/src/bin/cli/subcommands.rs b/fake/src/bin/cli/subcommands.rs new file mode 100644 index 0000000..206700b --- /dev/null +++ b/fake/src/bin/cli/subcommands.rs @@ -0,0 +1,30 @@ +use clap::{value_parser, Arg, Command}; +macro_rules! generate_command { + ($fake:literal) => { + Command::new($fake) + }; + ($fake:literal, range, $min:literal, $max:literal) => { + Command::new($fake) + .arg( + Arg::new("max") + .long("max") + .default_value($max) + .value_parser(value_parser!(usize)), + ) + .arg( + Arg::new("min") + .long("min") + .default_value($min) + .value_parser(value_parser!(usize)), + ) + }; +} + +pub fn all_fakegen_commands() -> Vec { + vec![ + generate_command!("Name"), + generate_command!("FirstName"), + generate_command!("CityPrefix"), + generate_command!("Password", range, "10", "20"), + ] +} From d29038edc7e218cfc6b3b36fcf6b52e111817e3b Mon Sep 17 00:00:00 2001 From: akhildevelops Date: Wed, 18 Dec 2024 17:55:50 +0530 Subject: [PATCH 2/8] github actions filters --- .github/workflows/rust.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index edcaf82..8e24d58 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -3,8 +3,16 @@ name: Rust on: push: branches: [ master ] + paths: + - "**.rs" + - "**/Cargo.toml" + - "workflows/**" pull_request: branches: [ master ] + paths: + - "**.rs" + - "**/Cargo.toml" + - "workflows/**" jobs: build: From c063a306e20549aca7a3213d2b8f4e4ce4d312dc Mon Sep 17 00:00:00 2001 From: akhildevelops Date: Wed, 18 Dec 2024 17:57:20 +0530 Subject: [PATCH 3/8] fix workdflows --- .github/workflows/rust.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 8e24d58..160d1b2 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -6,13 +6,13 @@ on: paths: - "**.rs" - "**/Cargo.toml" - - "workflows/**" + - ".github/workflows/**" pull_request: branches: [ master ] paths: - "**.rs" - "**/Cargo.toml" - - "workflows/**" + - ".github/workflows/**" jobs: build: From b528d50ec67424a6ab16e20d2f09497849c7350d Mon Sep 17 00:00:00 2001 From: akhildevelops Date: Wed, 18 Dec 2024 18:05:18 +0530 Subject: [PATCH 4/8] fake as cli --- fake/Cargo.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fake/Cargo.toml b/fake/Cargo.toml index be3efb6..17ff72a 100644 --- a/fake/Cargo.toml +++ b/fake/Cargo.toml @@ -85,7 +85,8 @@ path = "examples/usage.rs" required-features = ["derive"] [[bin]] -name = "cli" +name = "fake" +path = "src/bin/cli/main.rs" required-features = [ "clap" ] From de71a3bf7330334cc181f417e5ee9ba920225eee Mon Sep 17 00:00:00 2001 From: akhildevelops Date: Wed, 18 Dec 2024 18:05:53 +0530 Subject: [PATCH 5/8] fetch from the path --- .github/workflows/rust.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 160d1b2..f960826 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -75,5 +75,5 @@ jobs: uses: actions/upload-artifact@v4 with: name: fake - path: target/release/cli + path: target/release/fake From 54561893e6d103c0e437085355fa0eee26b71456 Mon Sep 17 00:00:00 2001 From: akhildevelops Date: Wed, 18 Dec 2024 18:07:12 +0530 Subject: [PATCH 6/8] fix binary name --- .github/workflows/rust.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index f960826..a166682 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -70,7 +70,7 @@ jobs: - uses: actions-rs/cargo@v1 with: command: build - args: --release --bin cli --features clap + args: --release --bin fake --features clap - name: Upload Bin Artifact uses: actions/upload-artifact@v4 with: From d58b2cc937d464da369d9e2535eb0cdc8d667e30 Mon Sep 17 00:00:00 2001 From: akhildevelops Date: Sat, 21 Dec 2024 23:04:45 +0530 Subject: [PATCH 7/8] powers several other fake gen --- fake/Cargo.toml | 4 +- fake/src/bin/cli/fake_gen.rs | 213 ++++++++++++++++++++++++++------ fake/src/bin/cli/main.rs | 8 +- fake/src/bin/cli/subcommands.rs | 30 ----- 4 files changed, 182 insertions(+), 73 deletions(-) delete mode 100644 fake/src/bin/cli/subcommands.rs diff --git a/fake/Cargo.toml b/fake/Cargo.toml index 17ff72a..f922d84 100644 --- a/fake/Cargo.toml +++ b/fake/Cargo.toml @@ -60,7 +60,7 @@ bigdecimal = ["bigdecimal-rs", "rust_decimal"] geo = ["geo-types", "num-traits"] http = ["dep:http", "url-escape"] bson_oid = ["bson"] -clap = ["dep:clap"] +cli = ["dep:clap"] [[example]] name = "basic" @@ -88,5 +88,5 @@ required-features = ["derive"] name = "fake" path = "src/bin/cli/main.rs" required-features = [ - "clap" + "cli" ] diff --git a/fake/src/bin/cli/fake_gen.rs b/fake/src/bin/cli/fake_gen.rs index 768820b..f4d5eaa 100644 --- a/fake/src/bin/cli/fake_gen.rs +++ b/fake/src/bin/cli/fake_gen.rs @@ -13,76 +13,215 @@ pub enum AVAILABLE_LOCALES { PT_BR, } +macro_rules! fake_gen_on_return_type { + ($s:ident,$rng:ident,Vec) => { + format!("{:?}", $s.fake_with_rng::, _>($rng)) + }; + ($s:ident,$rng:ident) => { + $s.fake_with_rng($rng) + }; +} + macro_rules! some_rules { - ($locale:expr, $module:ident, $fake:ident($($arg:ident)?)) => { + ($locale:expr, $module:ident, $fake:ident($($arg:ident)?)$(,$return_type:ident)?) => { match $locale { AVAILABLE_LOCALES::EN => { let s = faker::$module::en::$fake($($arg)?); - Box::new(move |rng: &mut R| s.fake_with_rng(rng)) + Box::new(move |rng: &mut R| fake_gen_on_return_type!(s,rng$(,$return_type)?)) } AVAILABLE_LOCALES::FR_FR => { let s = faker::$module::fr_fr::$fake($($arg)?); - Box::new(move |rng: &mut R| s.fake_with_rng(rng)) + Box::new(move |rng: &mut R| fake_gen_on_return_type!(s,rng$(,$return_type)?)) } AVAILABLE_LOCALES::ZH_TW => { let s = faker::$module::zh_tw::$fake($($arg)?); - Box::new(move |rng: &mut R| s.fake_with_rng(rng)) + Box::new(move |rng: &mut R| fake_gen_on_return_type!(s,rng$(,$return_type)?)) } AVAILABLE_LOCALES::ZH_CN => { let s = faker::$module::zh_cn::$fake($($arg)?); - Box::new(move |rng: &mut R| s.fake_with_rng(rng)) + Box::new(move |rng: &mut R| fake_gen_on_return_type!(s,rng$(,$return_type)?)) } AVAILABLE_LOCALES::AR_SA => { let s = faker::$module::ar_sa::$fake($($arg)?); - Box::new(move |rng: &mut R| s.fake_with_rng(rng)) + Box::new(move |rng: &mut R| fake_gen_on_return_type!(s,rng$(,$return_type)?)) } AVAILABLE_LOCALES::JA_JP => { let s = faker::$module::ja_jp::$fake($($arg)?); - Box::new(move |rng: &mut R| s.fake_with_rng(rng)) + Box::new(move |rng: &mut R| fake_gen_on_return_type!(s,rng$(,$return_type)?)) } AVAILABLE_LOCALES::PT_BR => { let s = faker::$module::pt_br::$fake($($arg)?); - Box::new(move |rng: &mut R| s.fake_with_rng(rng)) + Box::new(move |rng: &mut R| fake_gen_on_return_type!(s,rng$(,$return_type)?)) } } }; } -pub fn fake_generator( - matches: ArgMatches, - locale: AVAILABLE_LOCALES, - help_message: StyledStr, -) -> Box String> -where - R: Rng, -{ - match matches.subcommand() { - Some(("FirstName", _)) => { - some_rules!(locale, name, FirstName()) - } - Some(("Name", _)) => { - some_rules!(locale, name, Name()) - } - Some(("CityPrefix", _)) => { - some_rules!(locale, address, CityPrefix()) - } - Some(("Password", matches)) => { - let min = *matches.get_one::("min").unwrap(); - let max = *matches.get_one::("max").unwrap(); - let range = min..max; - some_rules!(locale, internet, Password(range)) - } - _ => { - println!("Didn't receive subcommand\n {}", help_message); - std::process::exit(0) - } - } +use clap::{value_parser, Arg, Command}; +macro_rules! generate_command { + ($fake:literal) => { + Command::new($fake) + }; + ($fake:ident) => { + Command::new(stringify!($fake)) + }; + ($fake:ident($($arg:ident: $type:ty=$default:literal),+)) => { + Command::new(stringify!($fake))$(.arg( + Arg::new(stringify!($arg)) + .long(stringify!($arg)) + .default_value(stringify!($default)) + .value_parser(value_parser!($type))))+ + + }; +} + +macro_rules! right_arm { + ($locale:ident, $module:ident, $fake:ident) => { + some_rules!($locale, $module, $fake()) + }; + ($locale:ident, $module:ident, $fake:ident($arg:ident: u8),$sub_matches:ident) => {{ + let value = *$sub_matches.get_one::(stringify!($arg)).unwrap(); + some_rules!($locale, $module, $fake(value)) + }}; + + ($locale:ident, $module:ident, $fake:ident(min: usize, max: usize),$sub_matches:ident$(,$return_type:ident)?) => {{ + let min = *$sub_matches.get_one::("min").unwrap(); + let max = *$sub_matches.get_one::("max").unwrap(); + let range = min..max; + some_rules!($locale, $module, $fake(range)$(,$return_type)?) + }}; +} + +macro_rules! fakegen_commands { + ($(($fake:ident$(($($arg:ident: $arg_type:tt=$arg_default:literal),+))?$(->$return_type:ident)?,$module:ident)),+) => { + ( + vec![$(generate_command!($fake$(($($arg:$arg_type=$arg_default),+))?)),+], + |arg_matches:ArgMatches, locale:AVAILABLE_LOCALES, help_message:StyledStr| { + match arg_matches.subcommand(){ + $(Some((stringify!($fake),_sub_matches))=>{ + right_arm!(locale, $module, $fake$(($($arg: $arg_type),+),_sub_matches)?$(,$return_type)?) + })+ + _ => { + println!("Didn't receive subcommand\n {}", help_message); + std::process::exit(0) + } + } + } + + ) + }; +} + +pub fn all_fakegen_commands() -> ( + Vec, + impl Fn(ArgMatches, AVAILABLE_LOCALES, StyledStr) -> Box String>, +) { + fakegen_commands!( + //address + (CityPrefix, address), + (CitySuffix, address), + (CityName, address), + (CountryName, address), + (CountryCode, address), + (StreetSuffix, address), + (StreetName, address), + (TimeZone, address), + (StateName, address), + (StateAbbr, address), + (SecondaryAddressType, address), + (SecondaryAddress, address), + (ZipCode, address), + (PostCode, address), + (BuildingNumber, address), + (Latitude, address), + (Longitude, address), + (Geohash(precision:u8=1),address), + + //barcode + (Isbn,barcode), + (Isbn10,barcode), + (Isbn13,barcode), + + //creditcard + (CreditCardNumber,creditcard), + + //company + (CompanySuffix, company), + (CompanyName, company), + (Buzzword, company), + (BuzzwordMiddle, company), + (BuzzwordTail, company), + (CatchPhrase, company), + (BsVerb, company), + (BsAdj, company), + (BsNoun, company), + (Bs, company), + (Profession, company), + (Industry, company), + + //internet + (FreeEmailProvider,internet), + (DomainSuffix,internet), + (FreeEmail,internet), + (SafeEmail,internet), + (Username,internet), + (Password(min:usize=10, max:usize=20),internet), + (IPv4,internet), + (IPv6,internet), + (IP,internet), + (MACAddress,internet), + (UserAgent,internet), + + //job + (Seniority,job), + (Field,job), + (Position,job), + + //lorem + (Word,lorem), + (Words(min:usize=5, max:usize=10)->Vec,lorem), + (Sentence(min:usize=5, max:usize=10),lorem), + (Sentences(min:usize=5, max:usize=10)->Vec,lorem), + (Paragraph(min:usize=5, max:usize=10),lorem), + (Paragraphs(min:usize=5, max:usize=10)->Vec,lorem), + + //name + (FirstName,name), + (LastName,name), + (Title,name), + (Suffix,name), + (Name,name), + (NameWithTitle,name), + + //phone_number + (PhoneNumber,phone_number), + (CellNumber,phone_number), + + //filesystem + (FilePath,filesystem), + (FileName,filesystem), + (FileExtension,filesystem), + (DirPath,filesystem), + (MimeType,filesystem), + (Semver,filesystem), + (SemverStable,filesystem), + (SemverUnstable,filesystem), + + //currency + (CurrencyCode,currency), + (CurrencyName,currency), + (CurrencySymbol,currency), + + //finance + (Bic,finance), + (Isin,finance) + ) } diff --git a/fake/src/bin/cli/main.rs b/fake/src/bin/cli/main.rs index 76f2b9c..935b0b4 100644 --- a/fake/src/bin/cli/main.rs +++ b/fake/src/bin/cli/main.rs @@ -5,11 +5,10 @@ use std::io::{self, Write}; mod fake_gen; #[allow(non_upper_case_globals)] mod names; -mod subcommands; const AVAILABLE_LOCALES: [&str; 7] = ["en", "fr_fr", "zh_tw", "zh_cn", "ja_jp", "ar_sa", "pt_br"]; -pub use fake_gen::{fake_generator, AVAILABLE_LOCALES}; +pub use fake_gen::{all_fakegen_commands, AVAILABLE_LOCALES}; pub fn main() { let stdout = io::stdout(); let mut buf_stdout = io::BufWriter::new(stdout); @@ -46,6 +45,7 @@ impl TryFrom<&str> for AVAILABLE_LOCALES { } fn cli_parser() -> (Args, impl Fn(&mut R) -> String) { + let (subcommands, fake_generator) = all_fakegen_commands::(); let mut command = command!() .arg( Arg::new("repeat") @@ -61,7 +61,7 @@ fn cli_parser() -> (Args, impl Fn(&mut R) -> String) { .default_value("EN") .value_parser(|value: &str| AVAILABLE_LOCALES::try_from(value)), ) - .subcommands(subcommands::all_fakegen_commands()) + .subcommands(subcommands) .arg_required_else_help(true); let help_message = command.render_help(); let matches = command.get_matches(); @@ -71,7 +71,7 @@ fn cli_parser() -> (Args, impl Fn(&mut R) -> String) { .unwrap() .to_owned(); - let fake_gen = fake_generator::(matches, locale, help_message); + let fake_gen = fake_generator(matches, locale, help_message); (Args { repeats, locale }, fake_gen) } diff --git a/fake/src/bin/cli/subcommands.rs b/fake/src/bin/cli/subcommands.rs deleted file mode 100644 index 206700b..0000000 --- a/fake/src/bin/cli/subcommands.rs +++ /dev/null @@ -1,30 +0,0 @@ -use clap::{value_parser, Arg, Command}; -macro_rules! generate_command { - ($fake:literal) => { - Command::new($fake) - }; - ($fake:literal, range, $min:literal, $max:literal) => { - Command::new($fake) - .arg( - Arg::new("max") - .long("max") - .default_value($max) - .value_parser(value_parser!(usize)), - ) - .arg( - Arg::new("min") - .long("min") - .default_value($min) - .value_parser(value_parser!(usize)), - ) - }; -} - -pub fn all_fakegen_commands() -> Vec { - vec![ - generate_command!("Name"), - generate_command!("FirstName"), - generate_command!("CityPrefix"), - generate_command!("Password", range, "10", "20"), - ] -} From c600f5481fbae5c6739d164a66a5afc378510f0e Mon Sep 17 00:00:00 2001 From: akhildevelops Date: Sat, 21 Dec 2024 23:08:01 +0530 Subject: [PATCH 8/8] fix actions where feature is cli --- .github/workflows/rust.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index a166682..d525195 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -70,7 +70,7 @@ jobs: - uses: actions-rs/cargo@v1 with: command: build - args: --release --bin fake --features clap + args: --release --bin fake --features cli - name: Upload Bin Artifact uses: actions/upload-artifact@v4 with: