Skip to content

Commit

Permalink
update binary arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
mplanchard committed Jan 13, 2025
1 parent 2ccc3cd commit a5a138e
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 37 deletions.
4 changes: 4 additions & 0 deletions crates/cuid/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ repository.workspace = true
cuid1 = { workspace = true, optional = true }
cuid2 = { workspace = true, optional = true }

[dev-dependencies]
paste.workspace = true
wasm-bindgen-test.workspace = true

[lib]
name = "cuid"
path = "src/lib.rs"
Expand Down
107 changes: 75 additions & 32 deletions crates/cuid/src/bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ use std::{
pub fn main() {
let args: CuidArgs = env::args().into();

let id = match args.v2 {
true => {
let id = match args.version {
CuidVersion::V1 => {
if args.slug {
// construct a v2 cuid with the same length as cuid1 slugs
cuid2::CuidConstructor::new().with_length(10).create_id()
one_off_cuid1_slug()
} else {
cuid2::create_id()
one_off_cuid1()
}
}
false => {
CuidVersion::V2 => {
if args.slug {
one_off_cuid1_slug()
// construct a v2 cuid with the same length as cuid1 slugs
cuid2::CuidConstructor::new().with_length(10).create_id()
} else {
one_off_cuid1()
cuid2::create_id()
}
}
};
Expand All @@ -33,45 +33,88 @@ const HELP: &str = r#"Usage: cuid [OPTION]...
Generate and print a CUID.
Options:
--v2 generate a v2 CUID/slug (this will eventually be the default)
--slug generate a slug instead of a full CUID
-h, --help display this help and exit
-v, --version display version information and exit"#;
-v, --version display version information and exit
--cuid <1|2> generate a CUID/slug using the specified version (default 1)
--slug generate a slug instead of a full CUID"#;

const VERSION: &str = env!("CARGO_PKG_VERSION");

#[derive(Debug)]
enum CuidVersion {
V1,
V2,
}

/// Commandline arguments for the CUID binary
#[derive(Debug)]
struct CuidArgs {
/// Whether to produce a slug instead of a CUID
slug: bool,
v2: bool,
version: CuidVersion,
}
impl From<Args> for CuidArgs {
fn from(args: Args) -> Self {
let mut slug = false;
let mut v2 = false;
let mut version = CuidVersion::V1;

// The first argument should be the binary name. Skip it.
args.skip(1).for_each(|arg| match arg.as_str() {
"-h" | "--help" => {
println!("{}", HELP);
exit(0);
}
"-v" | "--version" => {
println!("{}", VERSION);
exit(0);
// start on 1 to skip binary name.
let mut idx = 1;
let args = args.collect::<Vec<_>>();
loop {
match args.get(idx) {
None => {
break;
}
Some(arg) => match arg.as_str() {
"-h" | "--help" => {
println!("{}", HELP);
exit(0);
}
"-v" | "--version" => {
println!("{}", VERSION);
exit(0);
}
"--slug" => slug = true,
// yeah yeah I should probably just use clap at this point,
// but we'll get to it eventually
"--cuid" => {
idx += 1;
match args.get(idx) {
None => print_error_and_exit("--cuid requires an argument"),
Some(arg) => match arg.as_str() {
"1" => version = CuidVersion::V1,
"2" => version = CuidVersion::V2,
_ => {
print_error_and_exit(
"unrecognized cuid version, must be one of: 1|2",
);
}
},
}
}
arg if arg.starts_with("--cuid=") => match arg.split_once("=").unwrap().1 {
"1" => version = CuidVersion::V1,
"2" => version = CuidVersion::V2,
_ => {
print_error_and_exit("unrecognized cuid version, must be one of: 1|2");
}
},
_ => {
print_error_and_exit(&format!("unrecognized argument {}", arg));
}
},
}
"--slug" => slug = true,
"--v2" => v2 = true,
_ => {
println!("error: unrecognized argument {}", arg);
println!();
println!("{}", HELP);
exit(1);
}
});
idx += 1;
}

CuidArgs { slug, v2 }
CuidArgs { slug, version }
}
}

fn print_error_and_exit(msg: &str) {
println!("error: {}", msg);
println!();
println!("{}", HELP);
exit(1);
}
39 changes: 37 additions & 2 deletions crates/cuid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,48 @@
//!
#[cfg(feature = "v1")]
pub use cuid1::{cuid as cuid1, is_cuid as is_cuid1, is_slug as is_cuid1_slug, slug as cuid1_slug};
pub use cuid1::{
self as v1, cuid as cuid1, is_cuid as is_cuid1, is_slug as is_cuid1_slug, slug as cuid1_slug,
};
#[cfg(feature = "v1")]
#[doc(hidden)]
pub use cuid1::{one_off_cuid1, one_off_cuid1_slug};

#[cfg(feature = "v2")]
pub use cuid2::{
cuid as cuid2, is_cuid2, is_slug as is_cuid2_slug, slug as cuid2_slug,
self as v2, cuid as cuid2, is_cuid2, is_slug as is_cuid2_slug, slug as cuid2_slug,
CuidConstructor as Cuid2Constructor,
};

#[cfg(test)]
mod test {
use super::*;

/// Run an already-defined test in WASM as well.
macro_rules! wasm_test {
($name:ident) => {
paste::paste! {
#[wasm_bindgen_test::wasm_bindgen_test]
fn [<wasm_ $name>]() {

Check warning on line 60 in crates/cuid/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, stable, -p cuid)

function `wasm_test_v1` is never used

Check warning on line 60 in crates/cuid/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, stable, -p cuid)

function `wasm_test_v2` is never used

Check warning on line 60 in crates/cuid/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, nightly, -p cuid)

function `wasm_test_v1` is never used

Check warning on line 60 in crates/cuid/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, nightly, -p cuid)

function `wasm_test_v2` is never used

Check warning on line 60 in crates/cuid/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, beta, -p cuid)

function `wasm_test_v1` is never used

Check warning on line 60 in crates/cuid/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, beta, -p cuid)

function `wasm_test_v2` is never used

Check warning on line 60 in crates/cuid/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest, beta, -p cuid)

function `wasm_test_v1` is never used

Check warning on line 60 in crates/cuid/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest, beta, -p cuid)

function `wasm_test_v2` is never used

Check warning on line 60 in crates/cuid/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest, stable, -p cuid)

function `wasm_test_v1` is never used

Check warning on line 60 in crates/cuid/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest, stable, -p cuid)

function `wasm_test_v2` is never used

Check warning on line 60 in crates/cuid/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest, nightly, -p cuid)

function `wasm_test_v1` is never used

Check warning on line 60 in crates/cuid/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest, nightly, -p cuid)

function `wasm_test_v2` is never used

Check warning on line 60 in crates/cuid/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest, beta, -p cuid)

function `wasm_test_v1` is never used

Check warning on line 60 in crates/cuid/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest, beta, -p cuid)

function `wasm_test_v2` is never used

Check warning on line 60 in crates/cuid/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest, nightly, -p cuid)

function `wasm_test_v1` is never used

Check warning on line 60 in crates/cuid/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest, nightly, -p cuid)

function `wasm_test_v2` is never used

Check warning on line 60 in crates/cuid/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest, stable, -p cuid)

function `wasm_test_v1` is never used

Check warning on line 60 in crates/cuid/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test (macos-latest, stable, -p cuid)

function `wasm_test_v2` is never used
$name()
}
}
};
}

#[cfg(feature = "v1")]
#[test]
fn test_v1() {
is_cuid1(cuid1());
is_cuid1_slug(cuid1_slug());
}
wasm_test!(test_v1);

#[cfg(feature = "v2")]
#[test]
fn test_v2() {
is_cuid2(cuid2());
is_cuid2_slug(cuid2_slug());
}
wasm_test!(test_v2);
}
4 changes: 1 addition & 3 deletions crates/cuid1/src/fingerprint.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::process;

use crate::text::{pad, to_base_string};
use crate::BASE;

Expand All @@ -10,7 +8,7 @@ static FINGERPRINT_PADDING: usize = 2;
/// On WASM, which does not have PIDs, replace with a random number.
fn pid() -> String {
#[cfg(not(target_family = "wasm"))]
let pid = process::id();
let pid = std::process::id();
#[cfg(target_family = "wasm")]
let pid = rand::random::<u32>();

Expand Down

0 comments on commit a5a138e

Please sign in to comment.