From 705afc19e953133aadf811a0a51597e169f7aa62 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sun, 14 Jan 2024 13:25:22 -0800 Subject: [PATCH] Add `--decode` option, deprecate `from_string()`, bump version for new release. --- Cargo.toml | 2 +- README.md | 21 +++++++++++++++------ VERSION | 2 +- src/bin/gen.rs | 32 +++++++++++++++++++++++++------- src/julid.rs | 16 ++++++++++++---- src/serde.rs | 2 +- src/sqlx.rs | 2 +- 7 files changed, 56 insertions(+), 21 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b55980e..6aecef2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "julid-rs" -version = "1.6.180339" +version = "1.6.1803398" authors = ["Joe Ardent "] edition = "2021" keywords = ["ulid", "library", "sqlite", "extension", "julid"] diff --git a/README.md b/README.md index 258bb53..4813c01 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,8 @@ sqlite> select datetime(julid_timestamp(julid_new()), 'auto'); 2023-07-27 17:47:50 sqlite> select julid_counter(julid_new()); 0 +sqlite> select julid_string(); +01HM4WJ7T90001P8SN9898FBTN ``` Crates.io: @@ -83,6 +85,8 @@ The extension, when loaded into SQLite, provides the following functions: * `julid_new()`: create a new Julid and return it as a 16-byte [blob](https://www.sqlite.org/datatype3.html#storage_classes_and_datatypes) + * `julid_string()`: create a new Julid and return it as a 26-character [base-32 + Crockford](https://en.wikipedia.org/wiki/Base32)-encoded string * `julid_seconds(julid)`: get the number seconds (as a 64-bit float) since the UNIX epoch that this julid was created (convenient for passing to the builtin `datetime()` function) * `julid_counter(julid)`: show the value of this julid's monotonic counter @@ -207,24 +211,29 @@ And then using it is as simple as, $ julid-gen -h Generate and print Julids -Usage: julid-gen [NUM] +Usage: julid-gen [OPTIONS] [NUM] Arguments: [NUM] Number of Julids to generate [default: 1] Options: - -h, --help Print help - -V, --version Print version + -d, --decode Print the components of the given Julid + -a, --answer The answer to the meaning of Julid + -h, --help Print help + -V, --version Print version $ julid-gen 01H9DYRVDX0001X0RE5Y7XFGBC -$ julid-gen 5 +$ julid-gen 3 01H9DYT48E000EK2EH7P67N8GG 01H9DYT48E000ZBKXVZ91HEZX4 01H9DYT48E0012VX89PYX4HDKP -01H9DYT48E001GE29AWCH1RDCM -01H9DYT48E0028CDHNVC59KKHQ + +$ julid-gen -d 01H9DYT48E0012VX89PYX4HDKP +Created at: 2023-09-03 16:42:57.678 UTC +Monotonic counter: 2 +Entropy: 3311563785709860470 ``` # Thanks diff --git a/VERSION b/VERSION index fb9e103..a99a75d 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.6180339 +1.61803398 diff --git a/src/bin/gen.rs b/src/bin/gen.rs index 51a9139..aa71586 100644 --- a/src/bin/gen.rs +++ b/src/bin/gen.rs @@ -2,19 +2,32 @@ use clap::Parser; use julid::Julid; #[derive(Debug, Parser)] -#[command(author, version = "1.618033", about = "Generate and print Julids")] +#[command(author, version = "1.61803398", about = "Generate and print Julids")] struct Cli { - #[clap(help = "Print the timestamp of the given Julid", short, long)] - pub timestamp: Option, + #[clap( + help = "Print the components of the given Julid", + short = 'd', + long = "decode" + )] + pub input: Option, #[clap(help = "Number of Julids to generate", default_value_t = 1)] pub num: usize, + #[clap( + help = "The answer to the meaning of Julid", + default_value_t = false, + short, + long + )] + pub answer: bool, } fn main() { let cli = Cli::parse(); - if let Some(ts) = cli.timestamp { - if let Ok(ts) = Julid::from_string(&ts) { - println!("{}", ts.created_at()); + if let Some(ts) = cli.input { + if let Ok(ts) = Julid::from_str(&ts) { + println!("Created at:\t\t{}", ts.created_at()); + println!("Monotonic counter:\t{}", ts.counter()); + println!("Entropy:\t\t{}", ts.random()); } else { eprintln!("Could not parse input '{}' as a Julid", ts); std::process::exit(1); @@ -23,7 +36,12 @@ fn main() { // Just print some Julids let num = cli.num; for _ in 0..num { - println!("{}", Julid::new()); + let j = if cli.answer { + 42u128.into() + } else { + Julid::new() + }; + println!("{j}"); } } } diff --git a/src/julid.rs b/src/julid.rs index 0d4a993..ecc8de0 100644 --- a/src/julid.rs +++ b/src/julid.rs @@ -139,7 +139,7 @@ impl Julid { /// ```rust /// use julid::julid::Julid; /// let text = "01D39ZY06FGSCTVN4T2V9PKHFZ"; - /// let id = Julid::from_string(text).unwrap(); + /// let id = Julid::from_str(text).unwrap(); /// /// assert_eq!(&id.to_string(), text); /// ``` @@ -156,11 +156,19 @@ impl Julid { /// ```rust /// use julid::julid::Julid; /// let text = "01D39ZY06FGSCTVN4T2V9PKHFZ"; - /// let result = Julid::from_string(text); + /// let result = Julid::from_str(text); /// /// assert!(result.is_ok()); /// assert_eq!(&result.unwrap().to_string(), text); /// ``` + pub const fn from_str(encoded: &str) -> Result { + match base32::decode(encoded) { + Ok(int_val) => Ok(Julid(int_val)), + Err(err) => Err(err), + } + } + + #[deprecated(since = "1.6.1803398", note = "use `from_str` instead")] pub const fn from_string(encoded: &str) -> Result { match base32::decode(encoded) { Ok(int_val) => Ok(Julid(int_val)), @@ -231,7 +239,7 @@ impl FromStr for Julid { type Err = DecodeError; fn from_str(s: &str) -> Result { - Julid::from_string(s) + Julid::from_str(s) } } @@ -248,7 +256,7 @@ mod tests { #[test] fn test_static() { let s = Julid(0x41414141414141414141414141414141).as_string(); - let u = Julid::from_string(&s).unwrap(); + let u = Julid::from_str(&s).unwrap(); assert_eq!(&s, "21850M2GA1850M2GA1850M2GA1"); assert_eq!(u.0, 0x41414141414141414141414141414141); } diff --git a/src/serde.rs b/src/serde.rs index ebf7cc7..3eb1dd7 100644 --- a/src/serde.rs +++ b/src/serde.rs @@ -114,6 +114,6 @@ pub mod julid_as_str { D: Deserializer<'de>, { let deserialized_str = String::deserialize(deserializer)?; - Julid::from_string(&deserialized_str).map_err(serde::de::Error::custom) + Julid::from_str(&deserialized_str).map_err(serde::de::Error::custom) } } diff --git a/src/sqlx.rs b/src/sqlx.rs index f8bcb3f..ad26855 100644 --- a/src/sqlx.rs +++ b/src/sqlx.rs @@ -32,7 +32,7 @@ impl Decode<'_, Sqlite> for Julid { } _ => { let string = <&str as Decode>::decode(value)?; - Julid::from_string(string)? + Julid::from_str(string)? } };