Skip to content

Reason Network: your team's coding agents, talking to each other. The engine behind `reason network`.

Transliterate names before slugging them

WhitePepperLambSoupc73b597653e8
diff --git a/Cargo.lock b/Cargo.lock
index 92d8b5f..cc54dd5 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -686,6 +686,12 @@ dependencies = [
"unicode-xid",
]
+[[package]]
+name = "deunicode"
+version = "1.6.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "abd57806937c9cc163efc8ea3910e00a62e2aeb0b8119f1793a978088f8f6b04"
+
[[package]]
name = "diatomic-waker"
version = "0.2.3"
@@ -2829,6 +2835,7 @@ dependencies = [
"cliclack",
"console",
"data-encoding",
+ "deunicode",
"futures-lite",
"getrandom 0.3.4",
"iroh",
diff --git a/Cargo.toml b/Cargo.toml
index e79f94a..a133c4a 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -17,6 +17,7 @@ blake3 = "1"
clap = { version = "4", features = ["derive"] }
cliclack = "0.5"
data-encoding = "2"
+deunicode = "1"
futures-lite = "2"
getrandom = "0.3"
iroh = "1.3"
diff --git a/src/util.rs b/src/util.rs
index f6dc2f7..4ee4f9b 100644
--- a/src/util.rs
+++ b/src/util.rs
@@ -99,10 +99,12 @@ pub fn process(pid: u32) -> Option<(u32, String)> {
}
}
-/// Lowercase letters, digits and single dashes: a name that is safe in an address.
+/// Lowercase letters, digits and single dashes: a name that is safe in an address. Letters of other scripts are
+/// transliterated first (`José` -> `jose`, `代码` -> `dai-ma`), so the address stays plain ASCII, which every version
+/// accepts, and nothing but punctuation comes out empty.
pub fn slug(s: &str) -> String {
let mut out = String::new();
- for c in s.chars() {
+ for c in deunicode::deunicode(s).chars() {
if c.is_ascii_alphanumeric() {
out.push(c.to_ascii_lowercase());
} else if !out.is_empty() && !out.ends_with('-') {
@@ -242,6 +244,37 @@ mod tests {
assert_eq!(slug("--"), "");
}
+ #[test]
+ fn slugs_of_other_scripts_are_transliterated() {
+ assert_eq!(slug("José Peña"), "jose-pena");
+ assert_eq!(slug("Müller Straße"), "muller-strasse");
+ assert_eq!(slug("Проверка кода"), "proverka-koda");
+ assert_eq!(slug("代码审查"), "dai-ma-shen-cha");
+ // Mixed with ASCII, the ASCII part stays where it was.
+ assert_eq!(slug("api 审查"), "api-shen-cha");
+ // Scripts that used to come out empty or cut to a letter now give a usable address.
+ for name in [
+ "コードレビュー",
+ "코드 리뷰",
+ "مراجعة الكود",
+ "בדיקת קוד",
+ "कोड समीक्षा",
+ "ตรวจโค้ด",
+ "Έλεγχος",
+ "Łódź żółć",
+ ] {
+ let s = slug(name);
+ assert!(
+ s.len() > 2 && s.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-'),
+ "{name}: {s:?}"
+ );
+ assert_eq!(slug(&s), s, "{name}: slugging again changes nothing");
+ }
+ // What was already a slug stays one, so names saved by an older version don't change.
+ assert_eq!(slug("sams-macbook-pro"), "sams-macbook-pro");
+ assert_eq!(slug("v2"), "v2");
+ }
+
#[test]
fn ages() {
assert_eq!(ago(0, 59_000), "59s");