1
use std::collections::HashMap;
2

            
3
use zbus::wire::OwnedObjectPath;
4

            
5
use crate::collection::Collection;
6

            
7
35
pub(crate) async fn has_alias(
8
    collections: &HashMap<OwnedObjectPath, Collection>,
9
    alias: &str,
10
) -> bool {
11
70
    if alias.is_empty() {
12
8
        return false;
13
    }
14
140
    for collection in collections.values() {
15
20
        if collection.alias().await == alias {
16
4
            return true;
17
        }
18
    }
19
34
    false
20
}
21

            
22
38
pub(crate) async fn make_unique_label_and_alias(
23
    collections: &HashMap<OwnedObjectPath, Collection>,
24
    label: &str,
25
    alias: &str,
26
) -> (String, String) {
27
74
    let base_path = crate::collection::collection_path(label)
28
        .expect("Sanitized label should always produce valid object path");
29
114
    if !collections.contains_key(&base_path) && !has_alias(collections, alias).await {
30
36
        return (label.to_owned(), alias.to_owned());
31
    }
32

            
33
4
    let mut counter = 2;
34
4
    loop {
35
8
        let new_label = format!("{label}{counter}");
36
4
        let path = crate::collection::collection_path(&new_label)
37
            .expect("Sanitized label should always produce valid object path");
38
4
        let new_alias = format!("{alias}{counter}");
39

            
40
17
        if !collections.contains_key(&path) && !has_alias(collections, &new_alias).await {
41
5
            return (new_label, new_alias);
42
        }
43
        counter += 1;
44
    }
45
}
46

            
47
#[cfg(test)]
48
mod tests {
49
    use super::*;
50
    use crate::tests::TestServiceSetup;
51

            
52
    #[tokio::test]
53
    async fn has_alias_empty_is_false() {
54
        let collections = HashMap::new();
55
        assert!(!has_alias(&collections, "").await);
56
    }
57

            
58
    #[tokio::test]
59
    async fn has_alias_empty_collections() {
60
        let collections = HashMap::new();
61
        assert!(!has_alias(&collections, "default").await);
62
    }
63

            
64
    #[tokio::test]
65
    async fn unique_when_no_collections() {
66
        let collections = HashMap::new();
67
        let (label, alias) = make_unique_label_and_alias(&collections, "Work", "work").await;
68
        assert_eq!(label, "Work");
69
        assert_eq!(alias, "work");
70
    }
71

            
72
    #[tokio::test]
73
    async fn has_alias_finds_match() {
74
        let setup = TestServiceSetup::plain_session(true).await.unwrap();
75
        let collections = setup.server.collections.lock().await;
76
        assert!(has_alias(&collections, "default").await);
77
        assert!(!has_alias(&collections, "nonexistent").await);
78
    }
79

            
80
    #[tokio::test]
81
    async fn appends_counter_on_alias_conflict() {
82
        let setup = TestServiceSetup::plain_session(true).await.unwrap();
83
        let collections = setup.server.collections.lock().await;
84

            
85
        let (label, alias) = make_unique_label_and_alias(&collections, "Fresh", "default").await;
86
        assert_eq!(label, "Fresh2");
87
        assert_eq!(alias, "default2");
88
    }
89

            
90
    #[tokio::test]
91
    async fn appends_counter_on_path_conflict() {
92
        let setup = TestServiceSetup::plain_session(true).await.unwrap();
93
        let collections = setup.server.collections.lock().await;
94

            
95
        let (label, alias) = make_unique_label_and_alias(&collections, "Login", "fresh").await;
96
        assert_eq!(label, "Login2");
97
        assert_eq!(alias, "fresh2");
98
    }
99
}