From d4a388fc44b232a3a881743556d1f3d66dd8be03 Mon Sep 17 00:00:00 2001 From: Joe Ardent Date: Sun, 17 Apr 2022 13:36:04 -0700 Subject: [PATCH] Add support for ksuid. --- Cargo.lock | 54 ++++++++++++++++++++++++++++++++++++++++++++++++----- Cargo.toml | 3 ++- README.md | 14 +++++++++----- VERSION | 2 +- src/main.rs | 4 +++- 5 files changed, 64 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 453a3e1..8f245b9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -22,12 +22,24 @@ dependencies = [ "winapi", ] +[[package]] +name = "base-encode" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a17bd29f7c70f32e9387f4d4acfa5ea7b7749ef784fb78cf382df97069337b8c" + [[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +[[package]] +name = "byteorder" +version = "1.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" + [[package]] name = "cfg-if" version = "1.0.0" @@ -51,9 +63,9 @@ dependencies = [ [[package]] name = "getrandom" -version = "0.2.3" +version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753" +checksum = "9be70c98951c83b8d2f8f60d7065fa6d5146873094452a1008da8c2f1e4205ad" dependencies = [ "cfg-if", "libc", @@ -71,9 +83,18 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.107" +version = "0.2.123" 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]] name = "ppv-lite86" @@ -123,10 +144,11 @@ dependencies = [ [[package]] name = "randical" -version = "1.6.180" +version = "1.6.18033" dependencies = [ "clap", "rand", + "svix-ksuid", "uuid", ] @@ -136,6 +158,18 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "textwrap" version = "0.11.0" @@ -145,6 +179,16 @@ dependencies = [ "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]] name = "unicode-width" version = "0.1.9" diff --git a/Cargo.toml b/Cargo.toml index dd04750..36dafe9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "randical" -version = "1.6.1803" +version = "1.6.18033" authors = ["Joe Ardent "] edition = "2021" @@ -22,3 +22,4 @@ maintenance = { status = "actively-developed" } rand = "0.8" clap = "2" uuid = { version = "0.8", default-features = false } +svix-ksuid = "0.6" \ No newline at end of file diff --git a/README.md b/README.md index 21dd9a2..f7a2445 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ random values of varying types, with uniform distribution. See below for usage a ```text -Radical Random Value Generator 1.61803 +Radical Random Value Generator 1.618033 Generates arbitrary numbers of uniformly distributed random values. @@ -27,13 +27,14 @@ FLAGS: OPTIONS: -n, --num-vals Number of random values to print out. Defaults to 1. -t, --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: ```text -$ randical -n 3 # print out five bools +$ randical -n 3 # print out three bools Bogus. Radical! Radical! @@ -50,13 +51,16 @@ $ randical -t f # floating-point numbers from [0,1) $ randical -t U # v4 UUIDs 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 $? 1 $ randical -e $ echo $? 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: diff --git a/VERSION b/VERSION index 9a0be07..f8903b6 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.61803 +1.618033 diff --git a/src/main.rs b/src/main.rs index dc236a6..0f8c0ac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use std::process::exit; use clap::{App, Arg, ArgMatches}; use rand::prelude::*; +use svix_ksuid::{KsuidLike, KsuidMs}; use uuid::{Builder, Variant, Version}; const VERSION: &str = include_str!("../VERSION"); @@ -33,7 +34,7 @@ fn get_args() -> ArgMatches<'static> { .short("t") .long("type") .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::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::())), "b" => Box::new(move || print_bool(&mut rng, args)), "U" => Box::new(move || print_uuidv4(&mut rng)), + "k" => Box::new(move || println!("{}", KsuidMs::new(None, None).to_string())), _ => panic!(), } }