Skip to main content

graphforge_core/
identifier.rs

1//! Canonical graph identifier contract shared by every Rust surface.
2
3/// Maximum encoded UTF-8 bytes in a graph label, relationship, or property name.
4pub const MAX_GRAPH_IDENTIFIER_BYTES: usize = 255;
5
6/// Whether `value` is a canonical bounded Unicode graph identifier.
7#[must_use]
8pub fn is_graph_identifier(value: &str) -> bool {
9    if value.len() > MAX_GRAPH_IDENTIFIER_BYTES {
10        return false;
11    }
12    let mut characters = value.chars();
13    characters
14        .next()
15        .is_some_and(|first| first == '_' || first.is_alphabetic())
16        && characters.all(|character| character == '_' || character.is_alphanumeric())
17}
18
19#[cfg(test)]
20mod tests {
21    use super::*;
22
23    #[test]
24    fn unicode_identifier_limit_is_encoded_bytes() {
25        let at_limit = format!("A{}", "é".repeat(127));
26        let over_limit = format!("A{}x", "é".repeat(127));
27        assert_eq!(at_limit.len(), 255);
28        assert_eq!(over_limit.len(), 256);
29        assert!(is_graph_identifier(&at_limit));
30        assert!(!is_graph_identifier(&over_limit));
31    }
32}