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 76e54d9..05c87a9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,152 +1,72 @@ -use std::str; +use crate::opts::{Opts, VersionOpts}; use uuid; +use crate::opts::common as common_opts; + +mod execute; +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 => 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, + 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 => todo!("SHA1 UUID generation is not supported"), }, 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)), - } - } -} diff --git a/src/opts.rs b/src/opts.rs new file mode 100644 index 0000000..979b28b --- /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. +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"], 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, +}