1
//! How a lookup key reaches an entity, by the rules of [`Directory`].
2

            
3
use uuid::Uuid;
4

            
5
use super::{Directory, Pick, pick};
6

            
7
#[derive(Debug, Clone, PartialEq, Eq)]
8
pub(crate) enum Resolution {
9
    Id,
10
    Path,
11
    Symbol,
12
    /// A unique account name that is not the full path; holds the path.
13
    Bare(String),
14
    Ambiguous(usize),
15
    Unknown,
16
}
17

            
18
39
fn resolution(found: &Pick, key: &str, otherwise: Resolution) -> Option<Resolution> {
19
39
    match found {
20
19
        Pick::One(id) if Uuid::parse_str(key) == Ok(*id) => Some(Resolution::Id),
21
16
        Pick::One(_) => Some(otherwise),
22
3
        Pick::Many(ids) => Some(Resolution::Ambiguous(ids.len())),
23
17
        Pick::Missing => None,
24
    }
25
39
}
26

            
27
impl Directory {
28
32
    pub(crate) fn classify_account(&self, key: &str) -> Resolution {
29
180
        let same = |name: &str, key: &str| name == key;
30
32
        let by_path = pick(&self.account_paths, key, same);
31
32
        if let Some(found) = resolution(&by_path, key, Resolution::Path) {
32
17
            return found;
33
15
        }
34
15
        match pick(&self.accounts, key, same) {
35
6
            Pick::One(id) => Resolution::Bare(self.account_path(id)),
36
3
            Pick::Many(ids) => Resolution::Ambiguous(ids.len()),
37
6
            Pick::Missing => Resolution::Unknown,
38
        }
39
32
    }
40

            
41
7
    pub(crate) fn classify_commodity(&self, key: &str) -> Resolution {
42
7
        let found = pick(&self.commodities, key, str::eq_ignore_ascii_case);
43
7
        resolution(&found, key, Resolution::Symbol).unwrap_or(Resolution::Unknown)
44
7
    }
45
}
46

            
47
#[cfg(test)]
48
mod tests;