Add --decode
option, deprecate from_string()
, bump version for new release.
This commit is contained in:
parent
6a9e43feee
commit
705afc19e9
7 changed files with 56 additions and 21 deletions
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "julid-rs"
|
||||
version = "1.6.180339"
|
||||
version = "1.6.1803398"
|
||||
authors = ["Joe Ardent <code@ardent.nebcorp.com>"]
|
||||
edition = "2021"
|
||||
keywords = ["ulid", "library", "sqlite", "extension", "julid"]
|
||||
|
|
21
README.md
21
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: <https://crates.io/crates/julid-rs>
|
||||
|
@ -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 <INPUT> 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
|
||||
|
|
2
VERSION
2
VERSION
|
@ -1 +1 @@
|
|||
1.6180339
|
||||
1.61803398
|
||||
|
|
|
@ -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<String>,
|
||||
#[clap(
|
||||
help = "Print the components of the given Julid",
|
||||
short = 'd',
|
||||
long = "decode"
|
||||
)]
|
||||
pub input: Option<String>,
|
||||
#[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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
16
src/julid.rs
16
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<Julid, DecodeError> {
|
||||
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<Julid, DecodeError> {
|
||||
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<Self, Self::Err> {
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ impl Decode<'_, Sqlite> for Julid {
|
|||
}
|
||||
_ => {
|
||||
let string = <&str as Decode<Sqlite>>::decode(value)?;
|
||||
Julid::from_string(string)?
|
||||
Julid::from_str(string)?
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue