//! The canonical JSON every signed text here uses (as deploy approvals, SIGNING.md): fixed key order, no spaces, //! `"` and `\` escaped and nothing else, no newline. Callers only write strings that passed [`text_ok`]. /// Appends `s` as a JSON string with minimal escaping. pub fn string(out: &mut String, s: &str) { out.push('"'); for c in s.chars() { match c { '"' => out.push_str("\\\""), '\\' => out.push_str("\\\\"), c => out.push(c), } } out.push('"'); } /// Appends `["a","b"]`. pub fn strings(out: &mut String, list: &[String]) { out.push('['); for (i, s) in list.iter().enumerate() { if i > 0 { out.push(','); } string(out, s); } out.push(']'); } /// Characters a shown text may not hold: control characters, and the invisible ones that change how a line looks /// on a card (bidi overrides and isolates, zero-width characters, line separators, soft hyphen). pub fn unprintable(c: char) -> bool { c.is_control() || matches!(c, '\u{00AD}' | '\u{061C}' | '\u{180E}' | '\u{200B}'..='\u{200F}' | '\u{2028}'..='\u{202E}') || matches!(c, '\u{2060}'..='\u{2069}' | '\u{FEFF}') } /// 1..=`max` characters, none of them [`unprintable`]. pub fn text_ok(s: &str, max: usize) -> bool { (1..=max).contains(&s.chars().count()) && !s.chars().any(unprintable) } /// `min..=max` bytes, every one ASCII alphanumeric or in `extra`. pub fn ascii_ok(s: &str, min: usize, max: usize, extra: &[u8]) -> bool { (min..=max).contains(&s.len()) && s.bytes().all(|c| c.is_ascii_alphanumeric() || extra.contains(&c)) } /// An environment name: `[a-z][a-z0-9-]{0,31}`. pub fn env_ok(s: &str) -> bool { let b = s.as_bytes(); !b.is_empty() && b.len() <= 32 && b[0].is_ascii_lowercase() && b.iter().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || *c == b'-') }