From 338abf9d01c76cf2ad488dad9e9150e5d71cce08 Mon Sep 17 00:00:00 2001 From: Hunar Roop Kahlon Date: Wed, 12 Oct 2022 17:25:44 -0700 Subject: [PATCH 1/2] start splitting files off Signed-off-by: Hunar Roop Kahlon --- src/main.rs | 173 ++++++++++++------------------------------- src/opts.rs | 36 +++++++++ src/opts/common.rs | 17 +++++ src/opts/generate.rs | 46 ++++++++++++ 4 files changed, 147 insertions(+), 125 deletions(-) create mode 100644 src/opts.rs create mode 100644 src/opts/common.rs create mode 100644 src/opts/generate.rs diff --git a/src/main.rs b/src/main.rs index 76e54d9..b10ab55 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,152 +1,75 @@ -use std::str; +use crate::opts::{Opts, VersionOpts}; use uuid; +use crate::opts::common as common_opts; + +mod opts; fn main() { - let opts: Opts = clap::Parser::parse(); + let opts = Opts::parse(); match opts { - Opts::Generate(opts) => match opts.version { - GenerateVersionOpts::Nil(opts) => { - (0..opts.number).for_each(|_| { + Opts::Generate { version } => match version { + VersionOpts::Nil { + case, + format, + number, + } => { + (0..number).for_each(|_| { let uuid = uuid::Uuid::nil(); - let format = &opts.format; - let case = &opts.case; match case { - CaseOpts::Lower => match format { - FormatOpts::Braced => println!("{:x}", uuid.braced()), - FormatOpts::Hyphenated => println!("{:x}", uuid.hyphenated()), - FormatOpts::Simple => println!("{:x}", uuid.simple()), - FormatOpts::Urn => println!("{:x}", uuid.urn()), + common_opts::Case::Lower => match format { + common_opts::Format::Braced => println!("{:x}", uuid.braced()), + common_opts::Format::Hyphenated => println!("{:x}", uuid.hyphenated()), + common_opts::Format::Simple => println!("{:x}", uuid.simple()), + common_opts::Format::Urn => println!("{:x}", uuid.urn()), }, - CaseOpts::Upper => match format { - FormatOpts::Braced => println!("{:X}", uuid.braced()), - FormatOpts::Hyphenated => println!("{:X}", uuid.hyphenated()), - FormatOpts::Simple => println!("{:X}", uuid.simple()), - FormatOpts::Urn => panic!("UUID URN format is not supported in uppercase"), + common_opts::Case::Upper => match format { + common_opts::Format::Braced => println!("{:X}", uuid.braced()), + common_opts::Format::Hyphenated => println!("{:X}", uuid.hyphenated()), + common_opts::Format::Simple => println!("{:X}", uuid.simple()), + common_opts::Format::Urn => { + panic!("UUID URN format is not supported in uppercase") + } }, } }); } - GenerateVersionOpts::Mac => {} - GenerateVersionOpts::Dce => {} - GenerateVersionOpts::Md5 => {} - GenerateVersionOpts::Random(opts) => { - (0..opts.number).for_each(|_| { + VersionOpts::Mac => {} + VersionOpts::Dce => {} + VersionOpts::Md5 => {} + VersionOpts::Random { + case, + format, + number, + } => { + (0..number).for_each(|_| { let uuid = uuid::Uuid::new_v4(); - let format = &opts.format; - let case = &opts.case; match case { - CaseOpts::Lower => match format { - FormatOpts::Braced => println!("{:x}", uuid.braced()), - FormatOpts::Hyphenated => println!("{:x}", uuid.hyphenated()), - FormatOpts::Simple => println!("{:x}", uuid.simple()), - FormatOpts::Urn => println!("{:x}", uuid.urn()), + common_opts::Case::Lower => match format { + common_opts::Format::Braced => println!("{:x}", uuid.braced()), + common_opts::Format::Hyphenated => println!("{:x}", uuid.hyphenated()), + common_opts::Format::Simple => println!("{:x}", uuid.simple()), + common_opts::Format::Urn => println!("{:x}", uuid.urn()), }, - CaseOpts::Upper => match format { - FormatOpts::Braced => println!("{:X}", uuid.braced()), - FormatOpts::Hyphenated => println!("{:X}", uuid.hyphenated()), - FormatOpts::Simple => println!("{:X}", uuid.simple()), - FormatOpts::Urn => panic!("UUID URN format is not supported in uppercase"), + common_opts::Case::Upper => match format { + common_opts::Format::Braced => println!("{:X}", uuid.braced()), + common_opts::Format::Hyphenated => println!("{:X}", uuid.hyphenated()), + common_opts::Format::Simple => println!("{:X}", uuid.simple()), + common_opts::Format::Urn => { + panic!("UUID URN format is not supported in uppercase") + } }, } }); } - GenerateVersionOpts::Sha1 => {} + VersionOpts::Sha1 => {} }, Opts::Parse => {} } } -#[derive(clap::Parser)] -#[clap(about, author, version)] -enum Opts { - #[clap(aliases= &["g", "gen"])] - Generate(GenerateOpts), - #[clap(aliases = &["p"])] - Parse, -} - -#[derive(clap::Parser)] -#[clap(about)] -struct GenerateOpts { - #[clap(subcommand)] - version: GenerateVersionOpts, -} - -#[derive(clap::Parser)] -enum GenerateVersionOpts { - #[clap(aliases = &["0", "v0"])] - Nil(GenerateNilOpts), - #[clap(aliases = &["1", "v1"])] - Mac, - #[clap(aliases = &["2", "v2"])] - Dce, - #[clap(aliases = &["3", "v3"])] - Md5, - #[clap(aliases = &["4", "v4"])] - Random(GenerateRandomOpts), - #[clap(aliases = &["5", "v5"])] - Sha1, -} - -#[derive(clap::Parser)] -struct GenerateNilOpts { - #[clap(short, long, default_value = "lowercase")] - case: CaseOpts, - #[clap(short, long, default_value = "1")] - number: usize, - #[clap(short, long, default_value = "hyphenated")] - format: FormatOpts, -} - -#[derive(clap::Parser)] -struct GenerateRandomOpts { - #[clap(short, long, default_value = "lowercase")] - case: CaseOpts, - #[clap(short, long, default_value = "1")] - number: usize, - #[clap(short, long, default_value = "hyphenated")] - format: FormatOpts, -} - -#[derive(Clone, clap::Parser)] -enum FormatOpts { - Braced, - Hyphenated, - Simple, - Urn, -} - -impl str::FromStr for FormatOpts { - type Err = String; - - fn from_str(s: &str) -> Result { - match s { - "braced" | "b" => Ok(FormatOpts::Braced), - "hyphenated" | "hypen" | "h" => Ok(FormatOpts::Hyphenated), - "simple" | "s" => Ok(FormatOpts::Simple), - "urn" | "u" => Ok(FormatOpts::Urn), - _ => Err(format!("Invalid format: {}", s)), - } - } -} - -#[derive(Clone, clap::Parser)] -enum CaseOpts { - Lower, - Upper, -} - -impl str::FromStr for CaseOpts { - type Err = String; - - fn from_str(s: &str) -> Result { - match s { - "lowercase" | "lower" | "l" => Ok(CaseOpts::Lower), - "uppercase" | "upper" | "u" => Ok(CaseOpts::Upper), - _ => Err(format!("Invalid case: {}", s)), - } - } +trait Execute { + fn execute(&self) -> T; } diff --git a/src/opts.rs b/src/opts.rs new file mode 100644 index 0000000..8c550eb --- /dev/null +++ b/src/opts.rs @@ -0,0 +1,36 @@ +pub mod common; +mod generate; + +// Temporary workaround for allowing main.rs to access the `VersionOpts` enum. +use crate::Execute; +pub use generate::VersionOpts; + +/// ruuhid is a command line tool for generating and parsing UUIDs. +#[derive(clap::Parser)] +#[clap(about, author, version)] +pub enum Opts { + /// Generate a UUID. + #[clap(aliases= &["g", "gen"], about, version)] + Generate { + #[clap(subcommand)] + version: generate::VersionOpts, + }, + /// Parse a UUID. + #[clap(aliases = &["p"])] + Parse, +} + +impl Opts { + pub fn parse() -> Self { + clap::Parser::parse() + } +} + +impl Execute for Opts { + fn execute(&self) { + match self { + Opts::Generate { version } => {} + Opts::Parse => {} + } + } +} diff --git a/src/opts/common.rs b/src/opts/common.rs new file mode 100644 index 0000000..1a54530 --- /dev/null +++ b/src/opts/common.rs @@ -0,0 +1,17 @@ +use std::str; + +/// The `Case` enum is used to specify the case of the generated UUID. +#[derive(Clone, clap::Parser, clap::ValueEnum)] +pub enum Case { + Lower, + Upper, +} + +/// The `Format` enum is used to specify the format of the generated UUID. +#[derive(Clone, clap::Parser, clap::ValueEnum)] +pub enum Format { + Braced, + Hyphenated, + Simple, + Urn, +} diff --git a/src/opts/generate.rs b/src/opts/generate.rs new file mode 100644 index 0000000..73f1623 --- /dev/null +++ b/src/opts/generate.rs @@ -0,0 +1,46 @@ +use crate::opts::common; + +/// Version of the UUID to generate. +#[derive(clap::Parser)] +pub enum VersionOpts { + /// Generate a `nil` UUID. + #[clap(aliases = &["0", "v0"], about)] + Nil { + #[clap(short, long, default_value = "lower")] + case: common::Case, + #[clap(short, long, default_value = "hyphenated")] + format: common::Format, + #[clap(short, long, default_value = "1")] + number: usize, + }, + /// Generate a `MAC` UUID. + /// + /// Currently not supported. + #[clap(aliases = &["1", "v1"])] + Mac, + /// Generate a `DCE` UUID. + /// + /// Currently not supported. + #[clap(aliases = &["2", "v2"])] + Dce, + /// Generate a `MD5` UUID. + /// + /// Currently not supported. + #[clap(aliases = &["3", "v3"])] + Md5, + /// Generate a `random` UUID. + #[clap(aliases = &["4", "v4"])] + Random { + #[clap(short, long, default_value = "lower")] + case: common::Case, + #[clap(short, long, default_value = "hyphenated")] + format: common::Format, + #[clap(short, long, default_value = "1")] + number: usize, + }, + /// Generate a `SHA1` UUID. + /// + /// Currently not supported. + #[clap(aliases = &["5", "v5"])] + Sha1, +} From cbb037beeab663db6d5b0fb5e2ff8fa0bb1aab92 Mon Sep 17 00:00:00 2001 From: Hunar Roop Kahlon Date: Wed, 12 Oct 2022 17:38:59 -0700 Subject: [PATCH 2/2] more cleanup Signed-off-by: Hunar Roop Kahlon --- .gitignore | 2 +- src/execute.rs | 3 +++ src/main.rs | 13 +++++-------- src/opts.rs | 6 +++--- 4 files changed, 12 insertions(+), 12 deletions(-) create mode 100644 src/execute.rs diff --git a/.gitignore b/.gitignore index f0e9209..a930c7a 100644 --- a/.gitignore +++ b/.gitignore @@ -16,4 +16,4 @@ Cargo.lock # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 # User-specific stuff -.idea/ +.idea/* diff --git a/src/execute.rs b/src/execute.rs new file mode 100644 index 0000000..a19440c --- /dev/null +++ b/src/execute.rs @@ -0,0 +1,3 @@ +pub trait Execute { + fn execute(&self) -> (); +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index b10ab55..05c87a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use crate::opts::{Opts, VersionOpts}; use uuid; use crate::opts::common as common_opts; +mod execute; mod opts; fn main() { @@ -35,9 +36,9 @@ fn main() { } }); } - VersionOpts::Mac => {} - VersionOpts::Dce => {} - VersionOpts::Md5 => {} + VersionOpts::Mac => todo!("MAC UUID generation is not supported"), + VersionOpts::Dce => todo!("DCE UUID generation is not supported"), + VersionOpts::Md5 => todo!("MD5 UUID generation is not supported"), VersionOpts::Random { case, format, @@ -64,12 +65,8 @@ fn main() { } }); } - VersionOpts::Sha1 => {} + VersionOpts::Sha1 => todo!("SHA1 UUID generation is not supported"), }, Opts::Parse => {} } } - -trait Execute { - fn execute(&self) -> T; -} diff --git a/src/opts.rs b/src/opts.rs index 8c550eb..979b28b 100644 --- a/src/opts.rs +++ b/src/opts.rs @@ -1,16 +1,16 @@ pub mod common; mod generate; -// Temporary workaround for allowing main.rs to access the `VersionOpts` enum. -use crate::Execute; +/// Temporary workaround for allowing main.rs to access the [`VersionOpts`] enum. pub use generate::VersionOpts; +use crate::execute::Execute; /// ruuhid is a command line tool for generating and parsing UUIDs. #[derive(clap::Parser)] #[clap(about, author, version)] pub enum Opts { /// Generate a UUID. - #[clap(aliases= &["g", "gen"], about, version)] + #[clap(aliases= &["g", "gen"], version)] Generate { #[clap(subcommand)] version: generate::VersionOpts,