Skip to content

More readable UUIDs #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ 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"
serde_repr = "0.1.8"
sha2 = "0.10.8"
37 changes: 35 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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) {
use sha2::{Digest, Sha256};
let hash = Sha256::digest(m.as_str());
// 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((
m.as_str().to_owned(),
ANIMALS[a].to_owned() + ANIMALS[b],
));
}
for (uuid, r) in &new {
l = l.replace(uuid, r);
}
}

match serde_json::from_str::<serde_json::Value>(&l) {
Ok(j) => {
match serde_json::from_value::<BunyanEntry>(j.clone()) {
Expand Down