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]
|
[package]
|
||||||
name = "julid-rs"
|
name = "julid-rs"
|
||||||
version = "1.6.180339"
|
version = "1.6.1803398"
|
||||||
authors = ["Joe Ardent <code@ardent.nebcorp.com>"]
|
authors = ["Joe Ardent <code@ardent.nebcorp.com>"]
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
keywords = ["ulid", "library", "sqlite", "extension", "julid"]
|
keywords = ["ulid", "library", "sqlite", "extension", "julid"]
|
||||||
|
|
17
README.md
17
README.md
|
@ -20,6 +20,8 @@ sqlite> select datetime(julid_timestamp(julid_new()), 'auto');
|
||||||
2023-07-27 17:47:50
|
2023-07-27 17:47:50
|
||||||
sqlite> select julid_counter(julid_new());
|
sqlite> select julid_counter(julid_new());
|
||||||
0
|
0
|
||||||
|
sqlite> select julid_string();
|
||||||
|
01HM4WJ7T90001P8SN9898FBTN
|
||||||
```
|
```
|
||||||
|
|
||||||
Crates.io: <https://crates.io/crates/julid-rs>
|
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
|
* `julid_new()`: create a new Julid and return it as a 16-byte
|
||||||
[blob](https://www.sqlite.org/datatype3.html#storage_classes_and_datatypes)
|
[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_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 was created (convenient for passing to the builtin `datetime()` function)
|
||||||
* `julid_counter(julid)`: show the value of this julid's monotonic counter
|
* `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
|
$ julid-gen -h
|
||||||
Generate and print Julids
|
Generate and print Julids
|
||||||
|
|
||||||
Usage: julid-gen [NUM]
|
Usage: julid-gen [OPTIONS] [NUM]
|
||||||
|
|
||||||
Arguments:
|
Arguments:
|
||||||
[NUM] Number of Julids to generate [default: 1]
|
[NUM] Number of Julids to generate [default: 1]
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
|
-d, --decode <INPUT> Print the components of the given Julid
|
||||||
|
-a, --answer The answer to the meaning of Julid
|
||||||
-h, --help Print help
|
-h, --help Print help
|
||||||
-V, --version Print version
|
-V, --version Print version
|
||||||
|
|
||||||
$ julid-gen
|
$ julid-gen
|
||||||
01H9DYRVDX0001X0RE5Y7XFGBC
|
01H9DYRVDX0001X0RE5Y7XFGBC
|
||||||
|
|
||||||
$ julid-gen 5
|
$ julid-gen 3
|
||||||
01H9DYT48E000EK2EH7P67N8GG
|
01H9DYT48E000EK2EH7P67N8GG
|
||||||
01H9DYT48E000ZBKXVZ91HEZX4
|
01H9DYT48E000ZBKXVZ91HEZX4
|
||||||
01H9DYT48E0012VX89PYX4HDKP
|
01H9DYT48E0012VX89PYX4HDKP
|
||||||
01H9DYT48E001GE29AWCH1RDCM
|
|
||||||
01H9DYT48E0028CDHNVC59KKHQ
|
$ julid-gen -d 01H9DYT48E0012VX89PYX4HDKP
|
||||||
|
Created at: 2023-09-03 16:42:57.678 UTC
|
||||||
|
Monotonic counter: 2
|
||||||
|
Entropy: 3311563785709860470
|
||||||
```
|
```
|
||||||
|
|
||||||
# Thanks
|
# Thanks
|
||||||
|
|
2
VERSION
2
VERSION
|
@ -1 +1 @@
|
||||||
1.6180339
|
1.61803398
|
||||||
|
|
|
@ -2,19 +2,32 @@ use clap::Parser;
|
||||||
use julid::Julid;
|
use julid::Julid;
|
||||||
|
|
||||||
#[derive(Debug, Parser)]
|
#[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 {
|
struct Cli {
|
||||||
#[clap(help = "Print the timestamp of the given Julid", short, long)]
|
#[clap(
|
||||||
pub timestamp: Option<String>,
|
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)]
|
#[clap(help = "Number of Julids to generate", default_value_t = 1)]
|
||||||
pub num: usize,
|
pub num: usize,
|
||||||
|
#[clap(
|
||||||
|
help = "The answer to the meaning of Julid",
|
||||||
|
default_value_t = false,
|
||||||
|
short,
|
||||||
|
long
|
||||||
|
)]
|
||||||
|
pub answer: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let cli = Cli::parse();
|
let cli = Cli::parse();
|
||||||
if let Some(ts) = cli.timestamp {
|
if let Some(ts) = cli.input {
|
||||||
if let Ok(ts) = Julid::from_string(&ts) {
|
if let Ok(ts) = Julid::from_str(&ts) {
|
||||||
println!("{}", ts.created_at());
|
println!("Created at:\t\t{}", ts.created_at());
|
||||||
|
println!("Monotonic counter:\t{}", ts.counter());
|
||||||
|
println!("Entropy:\t\t{}", ts.random());
|
||||||
} else {
|
} else {
|
||||||
eprintln!("Could not parse input '{}' as a Julid", ts);
|
eprintln!("Could not parse input '{}' as a Julid", ts);
|
||||||
std::process::exit(1);
|
std::process::exit(1);
|
||||||
|
@ -23,7 +36,12 @@ fn main() {
|
||||||
// Just print some Julids
|
// Just print some Julids
|
||||||
let num = cli.num;
|
let num = cli.num;
|
||||||
for _ in 0..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
|
/// ```rust
|
||||||
/// use julid::julid::Julid;
|
/// use julid::julid::Julid;
|
||||||
/// let text = "01D39ZY06FGSCTVN4T2V9PKHFZ";
|
/// let text = "01D39ZY06FGSCTVN4T2V9PKHFZ";
|
||||||
/// let id = Julid::from_string(text).unwrap();
|
/// let id = Julid::from_str(text).unwrap();
|
||||||
///
|
///
|
||||||
/// assert_eq!(&id.to_string(), text);
|
/// assert_eq!(&id.to_string(), text);
|
||||||
/// ```
|
/// ```
|
||||||
|
@ -156,11 +156,19 @@ impl Julid {
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// use julid::julid::Julid;
|
/// use julid::julid::Julid;
|
||||||
/// let text = "01D39ZY06FGSCTVN4T2V9PKHFZ";
|
/// let text = "01D39ZY06FGSCTVN4T2V9PKHFZ";
|
||||||
/// let result = Julid::from_string(text);
|
/// let result = Julid::from_str(text);
|
||||||
///
|
///
|
||||||
/// assert!(result.is_ok());
|
/// assert!(result.is_ok());
|
||||||
/// assert_eq!(&result.unwrap().to_string(), text);
|
/// 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> {
|
pub const fn from_string(encoded: &str) -> Result<Julid, DecodeError> {
|
||||||
match base32::decode(encoded) {
|
match base32::decode(encoded) {
|
||||||
Ok(int_val) => Ok(Julid(int_val)),
|
Ok(int_val) => Ok(Julid(int_val)),
|
||||||
|
@ -231,7 +239,7 @@ impl FromStr for Julid {
|
||||||
type Err = DecodeError;
|
type Err = DecodeError;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
Julid::from_string(s)
|
Julid::from_str(s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -248,7 +256,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_static() {
|
fn test_static() {
|
||||||
let s = Julid(0x41414141414141414141414141414141).as_string();
|
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!(&s, "21850M2GA1850M2GA1850M2GA1");
|
||||||
assert_eq!(u.0, 0x41414141414141414141414141414141);
|
assert_eq!(u.0, 0x41414141414141414141414141414141);
|
||||||
}
|
}
|
||||||
|
|
|
@ -114,6 +114,6 @@ pub mod julid_as_str {
|
||||||
D: Deserializer<'de>,
|
D: Deserializer<'de>,
|
||||||
{
|
{
|
||||||
let deserialized_str = String::deserialize(deserializer)?;
|
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)?;
|
let string = <&str as Decode<Sqlite>>::decode(value)?;
|
||||||
Julid::from_string(string)?
|
Julid::from_str(string)?
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue