summaryrefslogtreecommitdiff
path: root/src/user/validate.rs
blob: 0c97293d6cddd536538fcd5c6649dec8e09e72fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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)
}