1
Fork 0

name generation

This commit is contained in:
Andy Killorin 2023-12-16 11:57:42 -06:00
commit a657d2754e
Signed by: ank
GPG key ID: B6241CA3B552BCA4
6 changed files with 185 additions and 0 deletions

26
server/Cargo.toml Normal file
View file

@ -0,0 +1,26 @@
[package]
name = "server"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
anyhow = "1.0.75"
axum = "0.7.2"
bit-struct = "0.3.2"
feistel_rs = "0.1.0"
rstar = "0.11.0"
rustmatica = "0.1.1"
tokio = { version = "1.0", features = ["full"] }
tower = { version = "0.4", features = ["util", "timeout", "load-shed", "limit"] }
tower-http = { version = "0.5.0", features = [
"add-extension",
"auth",
"compression-full",
"limit",
"trace",
] }
tower-layer = "0.3.2"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

8
server/Makefile Normal file
View file

@ -0,0 +1,8 @@
surnames.txt:
curl https://raw.githubusercontent.com/Hyneman/moby-project/672f6bdca054c42d375f065ffee87e8ceba0c242/moby/mwords/21986na.mes |\
iconv -f cp1252 - > surnames.txt
names.txt:
curl https://raw.githubusercontent.com/Hyneman/moby-project/672f6bdca054c42d375f065ffee87e8ceba0c242/moby/mwords/3897male.nam > male.txt
curl https://raw.githubusercontent.com/Hyneman/moby-project/672f6bdca054c42d375f065ffee87e8ceba0c242/moby/mwords/4946fema.len > female.txt
cat male.txt female.txt | sort | uniq > names.txt

16
server/order.txt Normal file
View file

@ -0,0 +1,16 @@
I
II
IIV
IV
V
VI
VII
IIX
IX
X
XI
XII
IIX
XIV
XV
XVI

8
server/pronouns.txt Normal file
View file

@ -0,0 +1,8 @@
he/him
she/her
they/them
xe/xem
ze/zir
fae/faer
e/em

33
server/src/main.rs Normal file
View file

@ -0,0 +1,33 @@
use std::env::args;
use axum::{
routing::get,
Router,
};
use anyhow::Error;
use rstar;
use rustmatica::BlockState;
mod names;
struct ControlState {
//turtles: Vec<Turtle>,
//world: unimplemented!(),
//chunkloaders: unimplemented!(),
}
#[tokio::main]
async fn main() -> Result<(), Error> {
println!("{}", names::Name::from_num(args().nth(1).unwrap().parse().unwrap()).to_str());
println!("{:?}", feistel_rs::feistel_encrypt(&[127,127], &[127,127], 1));
let serv = Router::new().route("/", get(|| async { "Hello" }));
let listener = tokio::net::TcpListener::bind("0.0.0.0:48228").await.unwrap();
axum::serve(listener, serv).await.unwrap();
Ok(())
}

94
server/src/names.rs Normal file
View file

@ -0,0 +1,94 @@
use anyhow::Context;
use bit_struct::*;
bit_struct! {
pub struct Name(u32) {
first: u13,
last: u14,
order: u3,
pronouns: u2,
}
}
const FIRST_NAMES: &str = include_str!("../names.txt");
const LAST_NAMES: &str = include_str!("../surnames.txt");
const ORDER: &str = include_str!("../order.txt");
const PRONOUNS: &str = include_str!("../pronouns.txt");
impl Name {
pub fn from_str(name: &str) -> anyhow::Result<Self> {
let parts: Vec<&str> = name.splitn(4, ' ').collect();
let (first, last, order, pronouns) = (parts[0], parts[1], parts[2], parts[3]);
let first = FIRST_NAMES.lines().position(|x| *x == *first).map(|n| u13::new(n as u16)).context("unknown first name")?;
let last = LAST_NAMES.lines().position(|x| *x == *last).map(|n| u14::new(n as u16)).context("unknown last name")?;
let order = ORDER.lines().position(|x| *x == *order).map(|n| u3::new(n as u8)).context("unknown title")?;
let pronouns = PRONOUNS.lines().position(|x| *x == *pronouns).map(|n| u2::new(n as u8)).context("invalid gender")?;
// first name 14 bits
// last name 13 bits
// order 3 bits
// pronouns 2 bits
// TODO: trim tables to avoid "zach" crashes
Ok(Name::new(first.unwrap(), last.unwrap(), order.unwrap(), pronouns.unwrap()))
}
pub fn from_num(name: u32) -> Self {
// This is ok because every possible value is valid
unsafe {
Name(UnsafeStorage::new_unsafe(name))
}
}
pub fn to_num(self) -> u32 {
self.raw()
}
pub fn to_str(&mut self) -> String {
let first = FIRST_NAMES.lines().nth(self.first().get().value() as usize).unwrap();
let last = LAST_NAMES.lines().nth(self.last().get().value() as usize).unwrap();
let order = ORDER.lines().nth(self.order().get().value() as usize).unwrap();
let pronouns = PRONOUNS.lines().nth(self.pronouns().get().value() as usize).unwrap();
let mut name = String::new();
name.push_str(first);
name.push(' ');
name.push_str(last);
name.push(' ');
name.push_str(order);
name.push(' ');
name.push_str(pronouns);
name
}
}
#[cfg(test)]
mod tests {
use super::*;
fn test_to_from(num: u32) -> u32 {
let name = Name::from_num(num).to_str();
Name::from_str(&name).unwrap().to_num()
}
#[test]
fn middling() {
const TEST_NUM: u32 = 6522345;
assert_eq!(TEST_NUM, test_to_from(TEST_NUM));
}
#[test]
fn small() {
const TEST_NUM: u32 = u32::MIN;
assert_eq!(TEST_NUM, test_to_from(TEST_NUM));
}
#[test]
fn big() {
const TEST_NUM: u32 = u32::MAX - 1;
assert_eq!(TEST_NUM, test_to_from(TEST_NUM));
}
}