Add support for ksuid.

This commit is contained in:
Joe Ardent 2022-04-17 13:36:04 -07:00
parent 0abe44d3a0
commit d4a388fc44
5 changed files with 64 additions and 13 deletions

54
Cargo.lock generated
View file

@ -22,12 +22,24 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "base-encode"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a17bd29f7c70f32e9387f4d4acfa5ea7b7749ef784fb78cf382df97069337b8c"
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.3.2" version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "byteorder"
version = "1.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
version = "1.0.0" version = "1.0.0"
@ -51,9 +63,9 @@ dependencies = [
[[package]] [[package]]
name = "getrandom" name = "getrandom"
version = "0.2.3" version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753" checksum = "9be70c98951c83b8d2f8f60d7065fa6d5146873094452a1008da8c2f1e4205ad"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"libc", "libc",
@ -71,9 +83,18 @@ dependencies = [
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.107" version = "0.2.123"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fbe5e23404da5b4f555ef85ebed98fb4083e55a00c317800bc2a50ede9f3d219" checksum = "cb691a747a7ab48abc15c5b42066eaafde10dc427e3b6ee2a1cf43db04c763bd"
[[package]]
name = "num_threads"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aba1801fb138d8e85e11d0fc70baf4fe1cdfffda7c6cd34a854905df588e5ed0"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "ppv-lite86" name = "ppv-lite86"
@ -123,10 +144,11 @@ dependencies = [
[[package]] [[package]]
name = "randical" name = "randical"
version = "1.6.180" version = "1.6.18033"
dependencies = [ dependencies = [
"clap", "clap",
"rand", "rand",
"svix-ksuid",
"uuid", "uuid",
] ]
@ -136,6 +158,18 @@ version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "svix-ksuid"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca50325f94cd44757a8a79749ff16d60d46fd50d4a5da62eb864ff1afd0fa87d"
dependencies = [
"base-encode",
"byteorder",
"getrandom",
"time",
]
[[package]] [[package]]
name = "textwrap" name = "textwrap"
version = "0.11.0" version = "0.11.0"
@ -145,6 +179,16 @@ dependencies = [
"unicode-width", "unicode-width",
] ]
[[package]]
name = "time"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2702e08a7a860f005826c6815dcac101b19b5eb330c27fe4a5928fec1d20ddd"
dependencies = [
"libc",
"num_threads",
]
[[package]] [[package]]
name = "unicode-width" name = "unicode-width"
version = "0.1.9" version = "0.1.9"

View file

@ -1,6 +1,6 @@
[package] [package]
name = "randical" name = "randical"
version = "1.6.1803" version = "1.6.18033"
authors = ["Joe Ardent <code@ardent.nebcorp.com>"] authors = ["Joe Ardent <code@ardent.nebcorp.com>"]
edition = "2021" edition = "2021"
@ -22,3 +22,4 @@ maintenance = { status = "actively-developed" }
rand = "0.8" rand = "0.8"
clap = "2" clap = "2"
uuid = { version = "0.8", default-features = false } uuid = { version = "0.8", default-features = false }
svix-ksuid = "0.6"

View file

@ -5,7 +5,7 @@ random values of varying types, with uniform distribution. See below for usage a
```text ```text
Radical Random Value Generator 1.61803 Radical Random Value Generator 1.618033
Generates arbitrary numbers of uniformly distributed random values. Generates arbitrary numbers of uniformly distributed random values.
@ -27,13 +27,14 @@ FLAGS:
OPTIONS: OPTIONS:
-n, --num-vals <NUM_VALS> Number of random values to print out. Defaults to 1. -n, --num-vals <NUM_VALS> Number of random values to print out. Defaults to 1.
-t, --type <TYPE> Type of random value to print. Defaults to 'bool'. -t, --type <TYPE> Type of random value to print. Defaults to 'bool'.
Possible values are 'b'ool, 'f'loat64, 'U'UIDv4, 'u'nsigned64, and 's'igned64 Possible values are 'b'ool, 'f'loat64, 'U'UIDv4, 'u'nsigned64, 's'igned64, and 'k'suid
with millisecond precision.
``` ```
Some examples: Some examples:
```text ```text
$ randical -n 3 # print out five bools $ randical -n 3 # print out three bools
Bogus. Bogus.
Radical! Radical!
Radical! Radical!
@ -50,13 +51,16 @@ $ randical -t f # floating-point numbers from [0,1)
$ randical -t U # v4 UUIDs $ randical -t U # v4 UUIDs
0237bf92-a629-440a-a1b5-ae32741b8ebd 0237bf92-a629-440a-a1b5-ae32741b8ebd
$ randical -e # exit with status 0 or 1, with equal probability $ randical -e # exit with status 0 or 1, with equal probability
$ echo $? $ echo $?
1 1
$ randical -e $ randical -e
$ echo $? $ echo $?
0 0
$ $ randical -t k -n 3 # print out three "K-sorted UID"s; see https://segment.com/blog/a-brief-history-of-the-uuid/
27wOG5VmO0vOzDsk66ELjzDa20X
27wOG5WdjugQa5HTnGr9IABktzF
27wOG5WK0MgZ49lrBUNWGe6XDFI
``` ```
You can use it to do some simple modeling in a shell one-liners, like so: You can use it to do some simple modeling in a shell one-liners, like so:

View file

@ -1 +1 @@
1.61803 1.618033

View file

@ -2,6 +2,7 @@ use std::process::exit;
use clap::{App, Arg, ArgMatches}; use clap::{App, Arg, ArgMatches};
use rand::prelude::*; use rand::prelude::*;
use svix_ksuid::{KsuidLike, KsuidMs};
use uuid::{Builder, Variant, Version}; use uuid::{Builder, Variant, Version};
const VERSION: &str = include_str!("../VERSION"); const VERSION: &str = include_str!("../VERSION");
@ -33,7 +34,7 @@ fn get_args() -> ArgMatches<'static> {
.short("t") .short("t")
.long("type") .long("type")
.takes_value(true) .takes_value(true)
.help("Type of random value to print. Defaults to 'bool'.\nPossible values are 'b'ool, 'f'loat64, 'U'UIDv4, 'u'nsigned64, and 's'igned64"), .help("Type of random value to print. Defaults to 'bool'.\nPossible values are 'b'ool, 'f'loat64, 'U'UIDv4, 'u'nsigned64, 's'igned64, and 'k'suid with millisecond precision."),
) )
.arg( .arg(
Arg::with_name("BULE") Arg::with_name("BULE")
@ -108,6 +109,7 @@ fn get_generator<'a>(args: &'a ArgMatches) -> Box<(dyn FnMut() + 'a)> {
"s" => Box::new(move || println!("{}", rng.gen::<i64>())), "s" => Box::new(move || println!("{}", rng.gen::<i64>())),
"b" => Box::new(move || print_bool(&mut rng, args)), "b" => Box::new(move || print_bool(&mut rng, args)),
"U" => Box::new(move || print_uuidv4(&mut rng)), "U" => Box::new(move || print_uuidv4(&mut rng)),
"k" => Box::new(move || println!("{}", KsuidMs::new(None, None).to_string())),
_ => panic!(), _ => panic!(),
} }
} }