diff options
| author | Owen Jacobson <owen@grimoire.ca> | 2024-10-29 20:44:03 -0400 |
|---|---|---|
| committer | Owen Jacobson <owen@grimoire.ca> | 2024-10-29 20:44:03 -0400 |
| commit | 9010c7feeca8f4e7e501ad474911deaaf7a1a367 (patch) | |
| tree | e2fb736e7923c0d084ee3ebffe03752dc237edfb /src/channel/validate.rs | |
| parent | 15311c7bd816a83d0641de6b6bb3c41bb67079db (diff) | |
Restrict channel names, too.
Thankfully, channel creation only happens in one place, so we don't need a state machine for this.
Diffstat (limited to 'src/channel/validate.rs')
| -rw-r--r-- | src/channel/validate.rs | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/channel/validate.rs b/src/channel/validate.rs new file mode 100644 index 0000000..0c97293 --- /dev/null +++ b/src/channel/validate.rs @@ -0,0 +1,23 @@ +use unicode_segmentation::UnicodeSegmentation as _; + +use crate::name::Name; + +// Picked out of a hat. The power of two is not meaningful. +const NAME_TOO_LONG: usize = 64; + +pub fn name(name: &Name) -> bool { + let display = name.display(); + + [ + display.graphemes(true).count() < NAME_TOO_LONG, + display.chars().all(|ch| !ch.is_control()), + display.chars().next().is_some_and(|c| !c.is_whitespace()), + display.chars().last().is_some_and(|c| !c.is_whitespace()), + display + .chars() + .zip(display.chars().skip(1)) + .all(|(a, b)| !(a.is_whitespace() && b.is_whitespace())), + ] + .into_iter() + .all(|value| value) +} |
