| 1 | pub const WHALE_NAME_MAX_CHARS: usize = 32; |
| 2 | |
| 3 | pub const WHALE_COLORS: &[(&str, [u8; 3])] = &[ |
| 4 | ("scout", [0x35, 0x94, 0xd8]), |
| 5 | ("patch", [0x54, 0xbb, 0xc5]), |
| 6 | ("harbor", [0x78, 0x96, 0xdf]), |
| 7 | ("echo", [0x43, 0x9e, 0xbd]), |
| 8 | ("keel", [0x76, 0x8c, 0xc8]), |
| 9 | ("lantern", [0x67, 0x98, 0xd0]), |
| 10 | ("violet", [0xa7, 0x8b, 0xfa]), |
| 11 | ("amber", [0xfb, 0xbf, 0x24]), |
| 12 | ]; |
| 13 | |
| 14 | fn hides_text(c: char) -> bool { |
| 15 | c.is_control() |
| 16 | || matches!( |
| 17 | c, |
| 18 | '\u{200b}'..='\u{200f}' | '\u{202a}'..='\u{202e}' | '\u{2060}'..='\u{2069}' | '\u{feff}' |
| 19 | ) |
| 20 | } |
| 21 | |
| 22 | pub fn normalize_whale_name(text: &str) -> Result<Option<String>, String> { |
| 23 | let name = text.split_whitespace().collect::<Vec<_>>().join(" "); |
| 24 | if name.is_empty() { |
| 25 | return Ok(None); |
| 26 | } |
| 27 | if name.chars().any(hides_text) { |
| 28 | return Err( |
| 29 | "whale_name cannot contain control or invisible formatting characters".to_string(), |
| 30 | ); |
| 31 | } |
| 32 | let count = name.chars().count(); |
| 33 | if count > WHALE_NAME_MAX_CHARS { |
| 34 | return Err(format!( |
| 35 | "whale_name is {count} characters; the limit is {WHALE_NAME_MAX_CHARS}" |
| 36 | )); |
| 37 | } |
| 38 | Ok(Some(name)) |
| 39 | } |
| 40 | |
| 41 | pub fn normalize_whale_color(value: &str) -> Result<Option<String>, String> { |
| 42 | let value = value.trim(); |
| 43 | if value.is_empty() || value.eq_ignore_ascii_case("default") { |
| 44 | return Ok(None); |
| 45 | } |
| 46 | let lower = value.to_ascii_lowercase(); |
| 47 | if WHALE_COLORS.iter().any(|(name, _)| *name == lower) { |
| 48 | return Ok(Some(lower)); |
| 49 | } |
| 50 | if let Some([r, g, b]) = parse_hex(&lower) { |
| 51 | return Ok(Some(format!("#{r:02x}{g:02x}{b:02x}"))); |
| 52 | } |
| 53 | let names = WHALE_COLORS |
| 54 | .iter() |
| 55 | .map(|(name, _)| *name) |
| 56 | .collect::<Vec<_>>() |
| 57 | .join(", "); |
| 58 | Err(format!( |
| 59 | "invalid whale_color '{value}'. Expected one of {names}, a #rrggbb hex value, or default." |
| 60 | )) |
| 61 | } |
| 62 | |
| 63 | pub fn whale_color_rgb(value: &str) -> Option<[u8; 3]> { |
| 64 | let lower = value.trim().to_ascii_lowercase(); |
| 65 | WHALE_COLORS |
| 66 | .iter() |
| 67 | .find(|(name, _)| *name == lower) |
| 68 | .map(|(_, rgb)| *rgb) |
| 69 | .or_else(|| parse_hex(&lower)) |
| 70 | } |
| 71 | |
| 72 | fn parse_hex(value: &str) -> Option<[u8; 3]> { |
| 73 | let hex = value.strip_prefix('#').unwrap_or(value); |
| 74 | if hex.len() != 6 || !hex.bytes().all(|byte| byte.is_ascii_hexdigit()) { |
| 75 | return None; |
| 76 | } |
| 77 | let channel = |at: usize| u8::from_str_radix(&hex[at..at + 2], 16).ok(); |
| 78 | Some([channel(0)?, channel(2)?, channel(4)?]) |
| 79 | } |
| 80 | |
| 81 | #[cfg(test)] |
| 82 | mod tests { |
| 83 | use super::*; |
| 84 | |
| 85 | #[test] |
| 86 | fn names_collapse_whitespace_and_reject_hidden_or_long_text() { |
| 87 | assert_eq!( |
| 88 | normalize_whale_name(" Moby Dick ").unwrap().as_deref(), |
| 89 | Some("Moby Dick") |
| 90 | ); |
| 91 | assert_eq!(normalize_whale_name(" ").unwrap(), None); |
| 92 | assert_eq!( |
| 93 | normalize_whale_name(&"鲸".repeat(32)).unwrap(), |
| 94 | Some("鲸".repeat(32)) |
| 95 | ); |
| 96 | let too_long = normalize_whale_name(&"x".repeat(33)).unwrap_err(); |
| 97 | assert!(too_long.contains("limit is 32"), "{too_long}"); |
| 98 | for hidden in ["\u{1b}[31m", "Mo\u{202e}by", "a\u{200b}b", "a\u{7f}"] { |
| 99 | assert!(normalize_whale_name(hidden).is_err(), "{hidden:?}"); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | #[test] |
| 104 | fn colors_accept_the_named_palette_and_hex_only() { |
| 105 | assert_eq!( |
| 106 | normalize_whale_color(" Harbor ").unwrap().as_deref(), |
| 107 | Some("harbor") |
| 108 | ); |
| 109 | assert_eq!( |
| 110 | normalize_whale_color("#7FB2E0").unwrap().as_deref(), |
| 111 | Some("#7fb2e0") |
| 112 | ); |
| 113 | assert_eq!( |
| 114 | normalize_whale_color("000000").unwrap().as_deref(), |
| 115 | Some("#000000") |
| 116 | ); |
| 117 | assert_eq!(normalize_whale_color("DEFAULT").unwrap(), None); |
| 118 | assert_eq!(normalize_whale_color("").unwrap(), None); |
| 119 | for rejected in [ |
| 120 | "blue", |
| 121 | "#12345", |
| 122 | "#12345g", |
| 123 | "#1234567", |
| 124 | "+12345", |
| 125 | "rgb(1,2,3)", |
| 126 | ] { |
| 127 | let error = normalize_whale_color(rejected).unwrap_err(); |
| 128 | assert!(error.contains("scout, patch"), "{rejected}: {error}"); |
| 129 | } |
| 130 | assert_eq!(whale_color_rgb("amber"), Some([0xfb, 0xbf, 0x24])); |
| 131 | assert_eq!(whale_color_rgb("#0a141e"), Some([10, 20, 30])); |
| 132 | assert_eq!(whale_color_rgb("nope"), None); |
| 133 | } |
| 134 | } |
| 135 |