From 6ab055e1615cb8ef58d26dd5be3963217a0f5397 Mon Sep 17 00:00:00 2001 From: Matt Keeter Date: Wed, 24 Apr 2024 15:39:28 -0400 Subject: [PATCH 1/3] Add UUID pretty-printer --- Cargo.lock | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 37 +++++++++++++++++++++++++++++++++-- 3 files changed, 91 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6e80cab..02e300b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -31,6 +31,15 @@ dependencies = [ "libc", ] +[[package]] +name = "aho-corasick" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" +dependencies = [ + "memchr", +] + [[package]] name = "anyhow" version = "1.0.80" @@ -200,12 +209,29 @@ dependencies = [ "anyhow", "chrono", "getopts", + "regex", "rhai", "serde", "serde_json", "serde_repr", ] +[[package]] +name = "memchr" +version = "2.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" + +[[package]] +name = "num-integer" +version = "0.1.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" +dependencies = [ + "autocfg", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.18" @@ -239,6 +265,35 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "regex" +version = "1.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" + [[package]] name = "rhai" version = "1.17.1" diff --git a/Cargo.toml b/Cargo.toml index 11956ed..ce3958e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" anyhow = "1.0.58" chrono = { version = "0.4.19", features = ["serde"] } getopts = "0.2.21" +regex = "1.10" rhai = { version = "1.14", features = ["serde"] } serde = { version = "1.0.138", features = ["derive"] } serde_json = "1.0.82" diff --git a/src/main.rs b/src/main.rs index 67ac45f..75f1b18 100644 --- a/src/main.rs +++ b/src/main.rs @@ -225,7 +225,7 @@ fn emit_record( serde_json::Value::String(s) => { let mut out = String::new(); for c in s.chars() { - if c != '"' && c != '\'' { + if c != '"' && c != '\'' && c.is_ascii() { out.push_str(&c.escape_default().to_string()); } else { out.push(c); @@ -324,6 +324,7 @@ fn main() -> Result<()> { "SCRIPT", ); opts.optopt("f", "", "read input from a file rather than stdin", "FILE"); + opts.optflag("u", "", "automatically reformat UUIDs"); let a = match opts.parse(std::env::args().skip(1)) { Ok(a) => { @@ -398,7 +399,39 @@ fn main() -> Result<()> { Colour::None }; - while let Some(l) = lines.next().transpose()? { + let uuid_animals = a.opt_present("u"); + const ANIMALS: [&str; 64] = [ + "🐶", "🐱", "🐭", "🐹", "🐰", "🦊", "🐻", "🐼", "🐨", "🐯", "🦁", "🐮", + "🐷", "🐽", "🐸", "🐵", "🐔", "🐧", "🐦", "🐤", "🪿", "🦆", "🦉", "🦇", + "🦇", "🐺", "🐗", "🐴", "🦄", "🫎", "🐝", "🪱", "🐛", "🦋", "🐌", "🐞", + "🐜", "🪲", "🪰", "🪳", "🦟", "🦗", "🕷️", "🦍", "🦂", "🐢", "🐍", "🦎", + "🦖", "🦕", "🐙", "🦑", "🪼", "🦩", "🐿️", "🦀", "🐡", "🐠", "🐟", "🐬", + "🐳", "🐋", "🦈", "🦭", + ]; + let re = regex::Regex::new( + r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}", + ) + .unwrap(); + + while let Some(mut l) = lines.next().transpose()? { + if uuid_animals { + let mut new = vec![]; + for m in re.find_iter(&l) { + let mut hasher = std::hash::DefaultHasher::new(); + use std::hash::{Hash, Hasher}; + m.as_str().hash(&mut hasher); + let hash = hasher.finish() as usize; + new.push(( + m.as_str().to_owned(), + ANIMALS[hash % ANIMALS.len()].to_owned() + + ANIMALS[(hash / ANIMALS.len()) % ANIMALS.len()], + )); + } + for (uuid, r) in &new { + l = l.replace(uuid, r); + } + } + match serde_json::from_str::(&l) { Ok(j) => { match serde_json::from_value::(j.clone()) { From 94aa1dedbefb03f02ba570368c443bc82f29d8a1 Mon Sep 17 00:00:00 2001 From: Matt Keeter Date: Wed, 24 Apr 2024 15:54:52 -0400 Subject: [PATCH 2/3] Use repeatable hash digest for UUID -> emoji mapping --- Cargo.lock | 94 ++++++++++++++++++++++++++++++++++++++++++----------- Cargo.toml | 1 + src/main.rs | 12 +++---- 3 files changed, 82 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 02e300b..a0cd096 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -16,6 +16,15 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "aho-corasick" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" +dependencies = [ + "memchr", +] + [[package]] name = "android-tzdata" version = "0.1.1" @@ -31,15 +40,6 @@ dependencies = [ "libc", ] -[[package]] -name = "aho-corasick" -version = "1.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" -dependencies = [ - "memchr", -] - [[package]] name = "anyhow" version = "1.0.80" @@ -58,6 +58,15 @@ version = "2.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + [[package]] name = "bumpalo" version = "3.15.3" @@ -117,12 +126,51 @@ version = "0.8.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" +[[package]] +name = "cpufeatures" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53fe5e26ff1b7aef8bca9c6080520cfb8d9333c7568e1829cef191a9723e5504" +dependencies = [ + "libc", +] + [[package]] name = "crunchy" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + [[package]] name = "getopts" version = "0.2.21" @@ -214,6 +262,7 @@ dependencies = [ "serde", "serde_json", "serde_repr", + "sha2", ] [[package]] @@ -222,16 +271,6 @@ version = "2.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" -[[package]] -name = "num-integer" -version = "0.1.45" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" -dependencies = [ - "autocfg", - "num-traits", -] - [[package]] name = "num-traits" version = "0.2.18" @@ -371,6 +410,17 @@ dependencies = [ "syn", ] +[[package]] +name = "sha2" +version = "0.10.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "smallvec" version = "1.13.1" @@ -427,6 +477,12 @@ dependencies = [ "crunchy", ] +[[package]] +name = "typenum" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" + [[package]] name = "unicode-ident" version = "1.0.12" diff --git a/Cargo.toml b/Cargo.toml index ce3958e..6354160 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,4 @@ rhai = { version = "1.14", features = ["serde"] } serde = { version = "1.0.138", features = ["derive"] } serde_json = "1.0.82" serde_repr = "0.1.8" +sha2 = "0.10.8" diff --git a/src/main.rs b/src/main.rs index 75f1b18..d4c1206 100644 --- a/src/main.rs +++ b/src/main.rs @@ -417,14 +417,14 @@ fn main() -> Result<()> { if uuid_animals { let mut new = vec![]; for m in re.find_iter(&l) { - let mut hasher = std::hash::DefaultHasher::new(); - use std::hash::{Hash, Hasher}; - m.as_str().hash(&mut hasher); - let hash = hasher.finish() as usize; + use sha2::{Digest, Sha256}; + let hash = Sha256::digest(m.as_str()); + // We can express 12 of randomness here with 2x emojis + let a = (hash[0] as usize) % ANIMALS.len(); + let b = (hash[1] as usize) % ANIMALS.len(); new.push(( m.as_str().to_owned(), - ANIMALS[hash % ANIMALS.len()].to_owned() - + ANIMALS[(hash / ANIMALS.len()) % ANIMALS.len()], + ANIMALS[a].to_owned() + ANIMALS[b], )); } for (uuid, r) in &new { From bf1b81802984db6b8ce023931c0edce2517a2f21 Mon Sep 17 00:00:00 2001 From: Matt Keeter Date: Wed, 24 Apr 2024 17:00:25 -0400 Subject: [PATCH 3/3] 12 what?? --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index d4c1206..cbe2194 100644 --- a/src/main.rs +++ b/src/main.rs @@ -419,7 +419,7 @@ fn main() -> Result<()> { for m in re.find_iter(&l) { use sha2::{Digest, Sha256}; let hash = Sha256::digest(m.as_str()); - // We can express 12 of randomness here with 2x emojis + // We can express 12 bits of randomness here with 2x emojis let a = (hash[0] as usize) % ANIMALS.len(); let b = (hash[1] as usize) % ANIMALS.len(); new.push((