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!(
31            parse_scope("/org/acme/project"),
32            vec!["org", "acme", "project"]
33        );
34    }
35
36    #[test]
37    fn ancestors_deep() {
38        let result = scope_ancestors("/org/acme/project/api");
39        assert_eq!(
40            result,
41            vec![
42                "/org/acme/project/api",
43                "/org/acme/project",
44                "/org/acme",
45                "/org",
46                "/"
47            ]
48        );
49    }
50
51    #[test]
52    fn ancestors_root() {
53        let result = scope_ancestors("/");
54        assert_eq!(result, vec!["/"]);
55    }
56
57    #[test]
58    fn ancestors_single() {
59        let result = scope_ancestors("/project");
60        assert_eq!(result, vec!["/project", "/"]);
61    }
62}