Skip to main content

systemprompt_identifiers/
scope.rs

1//! Scope dimension identifier.
2//!
3//! The name of a subject-attribute dimension (an extension `RuleType` slug
4//! such as `project` or `cost_centre`) that a gateway request can be
5//! attributed to. The value attributed for a dimension is the tenant's own
6//! vocabulary and is carried as text.
7//!
8//! Copyright (c) systemprompt.io — Business Source License 1.1.
9//! See <https://systemprompt.io> for licensing details.
10
11use crate::error::IdValidationError;
12
13crate::define_id!(ScopeDimension, checked, validate_scope_dimension);
14
15const MAX_SCOPE_DIMENSION_LEN: usize = 64;
16
17pub fn validate_scope_dimension(value: &str) -> Result<(), IdValidationError> {
18    let mut chars = value.chars();
19    let Some(first) = chars.next() else {
20        return Err(IdValidationError::empty("ScopeDimension"));
21    };
22    if value.len() > MAX_SCOPE_DIMENSION_LEN {
23        return Err(IdValidationError::invalid(
24            "ScopeDimension",
25            format!("must be at most {MAX_SCOPE_DIMENSION_LEN} characters"),
26        ));
27    }
28    if !first.is_ascii_lowercase() {
29        return Err(IdValidationError::invalid(
30            "ScopeDimension",
31            "must start with a lowercase letter",
32        ));
33    }
34    if !chars.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_') {
35        return Err(IdValidationError::invalid(
36            "ScopeDimension",
37            "may contain only lowercase letters, digits and '_'",
38        ));
39    }
40    Ok(())
41}