Skip to main content

memory_core/store/
scope.rs

1pub fn parse_scope(scope: &str) -> Vec<&str> {
2    scope.split('/').filter(|s| !s.is_empty()).collect()
3}
4
5pub fn scope_ancestors(scope: &str) -> Vec<String> {
6    let segments = parse_scope(scope);
7    let mut ancestors = Vec::with_capacity(segments.len() + 1);
8
9    for i in (0..=segments.len()).rev() {
10        if i == 0 {
11            ancestors.push("/".to_string());
12        } else {
13            ancestors.push(format!("/{}", segments[..i].join("/")));
14        }
15    }
16    ancestors
17}
18
19#[cfg(test)]
20mod tests {
21    use super::*;
22
23    #[test]
24    fn parse_root() {
25        assert!(parse_scope("/").is_empty());
26    }
27
28    #[test]
29    fn parse_segments() {
30        assert_eq!(parse_scope("/org/acme/project"), vec!["org", "acme", "project"]);
31    }
32
33    #[test]
34    fn ancestors_deep() {
35        let result = scope_ancestors("/org/acme/project/api");
36        assert_eq!(
37            result,
38            vec!["/org/acme/project/api", "/org/acme/project", "/org/acme", "/org", "/"]
39        );
40    }
41
42    #[test]
43    fn ancestors_root() {
44        let result = scope_ancestors("/");
45        assert_eq!(result, vec!["/"]);
46    }
47
48    #[test]
49    fn ancestors_single() {
50        let result = scope_ancestors("/project");
51        assert_eq!(result, vec!["/project", "/"]);
52    }
53}