Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split commandline options to their own file #12

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ Cargo.lock
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/
.idea/*
3 changes: 3 additions & 0 deletions src/execute.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub trait Execute {
fn execute(&self) -> ();
}
174 changes: 47 additions & 127 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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<Self, Self::Err> {
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<Self, Self::Err> {
match s {
"lowercase" | "lower" | "l" => Ok(CaseOpts::Lower),
"uppercase" | "upper" | "u" => Ok(CaseOpts::Upper),
_ => Err(format!("Invalid case: {}", s)),
}
}
}
36 changes: 36 additions & 0 deletions src/opts.rs
Original file line number Diff line number Diff line change
@@ -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 => {}
}
}
}
17 changes: 17 additions & 0 deletions src/opts/common.rs
Original file line number Diff line number Diff line change
@@ -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,
}
46 changes: 46 additions & 0 deletions src/opts/generate.rs
Original file line number Diff line number Diff line change
@@ -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,
}