use super::funejson::Value; use super::auth_recovery_codes_generate_recovery_codes::RECOVERY_ALPHABET; /// A typed code in its one stored form: ASCII lower case, spaces and hyphens /// dropped, i/l read as 1 and o as 0 (Crockford). None when what is left is /// not exactly 16 characters of the alphabet. pub fn normalise_recovery_code(code: &str) -> Option { let mut out = String::with_capacity(16); for raw in code.chars() { if raw == ' ' || raw == '-' { continue; } let mut ch = raw.to_ascii_lowercase(); if ch == 'i' || ch == 'l' { ch = '1'; } else if ch == 'o' { ch = '0'; } if !RECOVERY_ALPHABET.contains(ch) { return None; } out.push(ch); } if out.len() == 16 { Some(out) } else { None } } pub fn fune_vector(args: &[Value]) -> Value { let code = match &args[0] { Value::Str(s) => s.clone(), _ => panic!("code must be a string"), }; match normalise_recovery_code(&code) { Some(s) => Value::Str(s), None => Value::Null, } }