1
//! The suggestions of a cell and what a pick writes.
2

            
3
use serde::Deserialize;
4

            
5
/// Either a shared form option or a tag suggestion (`name` stands for the
6
/// label).
7
#[derive(Deserialize)]
8
struct Wire {
9
    id: Option<String>,
10
    label: Option<String>,
11
    alias: Option<String>,
12
    path: Option<String>,
13
    commodity: Option<String>,
14
    name: Option<String>,
15
}
16

            
17
#[derive(Debug, Clone, Default, PartialEq, Eq)]
18
pub(super) struct Item {
19
    pub(super) id: Option<String>,
20
    pub(super) label: String,
21
    pub(super) alias: Option<String>,
22
    pub(super) path: Option<String>,
23
    pub(super) commodity: Option<String>,
24
}
25

            
26
impl Item {
27
3
    fn from_wire(wire: Wire) -> Option<Self> {
28
3
        let label = wire.label.or_else(|| wire.name.clone())?;
29
3
        Some(Self {
30
3
            id: wire.id,
31
3
            label,
32
3
            alias: wire.alias,
33
3
            path: wire.path,
34
3
            commodity: wire.commodity,
35
3
        })
36
3
    }
37

            
38
    /// `query` is lowercase.
39
6
    pub(super) fn matches(&self, query: &str) -> bool {
40
10
        let has = |text: &str| text.to_lowercase().contains(query);
41
6
        has(&self.label)
42
4
            || [&self.path, &self.alias]
43
4
                .into_iter()
44
7
                .any(|text| text.as_deref().is_some_and(has))
45
6
    }
46

            
47
    /// The text shown beside the label: the whole path when it differs.
48
2
    pub(super) fn context(&self) -> Option<&str> {
49
2
        self.path.as_deref().filter(|path| *path != self.label)
50
2
    }
51
}
52

            
53
2
pub(super) fn parse_items(body: &str) -> Result<Vec<Item>, serde_json::Error> {
54
2
    let wires: Vec<Wire> = serde_json::from_str(body)?;
55
2
    Ok(wires.into_iter().filter_map(Item::from_wire).collect())
56
2
}
57

            
58
/// The id of the one option whose label is exactly `text`.
59
5
pub(super) fn labelled_id<'a>(items: &'a [Item], text: &str) -> Option<&'a str> {
60
5
    let text = text.trim();
61
15
    let mut found = items.iter().filter(|item| item.label == text);
62
5
    match (found.next(), found.next()) {
63
2
        (Some(only), None) => only.id.as_deref(),
64
3
        _ => None,
65
    }
66
5
}
67

            
68
/// The id of the option `text` names: its id, else its alias or label. A step matching several options leaves the text unresolved.
69
9
pub(super) fn resolve_id<'a>(items: &'a [Item], text: &str) -> Option<&'a str> {
70
9
    let text = text.trim();
71
17
    let named = |name: &str| name.eq_ignore_ascii_case(text);
72
18
    let steps: [&dyn Fn(&Item) -> bool; 2] = [&|item| item.id.as_deref() == Some(text), &|item| {
73
14
        item.alias.as_deref().is_some_and(named) || named(&item.label)
74
14
    }];
75
9
    for matches in steps {
76
32
        let mut found = items.iter().filter(|item| matches(item));
77
16
        match (found.next(), found.next()) {
78
7
            (Some(only), None) => return only.id.as_deref(),
79
1
            (Some(_), Some(_)) => return None,
80
8
            _ => {}
81
        }
82
    }
83
1
    None
84
9
}