Skip to main content

sofie_core/
field_name.rs

1/// Carries the `Idempotency-Key` of a form posted without JavaScript.
2pub const IDEMPOTENCY_KEY_FIELD: &str = "_idempotency_key";
3
4#[must_use]
5pub fn is_field_name(text: &str) -> bool {
6    !text.is_empty()
7        && text
8            .bytes()
9            .all(|byte| byte.is_ascii_alphanumeric() || byte == b'_')
10}
11
12#[cfg(test)]
13mod tests {
14    use super::*;
15
16    #[test]
17    fn a_field_name_is_ascii_letters_digits_and_underscores() {
18        for accepted in ["name", "line_2", "_", "A1"] {
19            assert!(is_field_name(accepted), "{accepted}");
20        }
21        for refused in ["", "a-b", "a.b", "a b", "a[0]", "รค"] {
22            assert!(!is_field_name(refused), "{refused}");
23        }
24    }
25}