1
//! Completions a rows list offers to its tag name and value cells.
2

            
3
use std::collections::HashMap;
4

            
5
use forms::Suggest;
6

            
7
use super::RowsWidget;
8

            
9
/// A lookup the sub-list wants answered; a value lookup names its tag.
10
#[derive(Debug, Clone, PartialEq, Eq)]
11
pub enum Hint {
12
    Names(Suggest),
13
    Values(Suggest, String),
14
}
15

            
16
impl Hint {
17
    /// The `form-options` source that serves the lookup.
18
    #[must_use]
19
117
    pub fn source(&self) -> String {
20
117
        match self {
21
87
            Self::Names(suggest) => suggest.name().to_string(),
22
30
            Self::Values(suggest, tag) => format!("{}:{tag}", suggest.name()),
23
        }
24
117
    }
25
}
26

            
27
impl Hint {
28
    /// The lookup the focused cell of `rows` needs, if it suggests anything.
29
    #[must_use]
30
97
    pub fn of_focused(rows: &RowsWidget) -> Option<Self> {
31
97
        let suggest = rows.focused_suggest()?;
32
72
        match suggest {
33
55
            Suggest::TagNames(_) => Some(Self::Names(suggest)),
34
17
            Suggest::TagValues(_) => match rows.focused_text("name") {
35
2
                "" => None,
36
15
                name => Some(Self::Values(suggest, name.to_string())),
37
            },
38
        }
39
97
    }
40
}
41

            
42
/// The answers so far per source and the `seq` of each request; a reply of
43
/// any other `seq` is stale.
44
#[derive(Debug, Default)]
45
pub struct Hints {
46
    answers: HashMap<String, (u64, Vec<String>)>,
47
}
48

            
49
impl Hints {
50
    /// Whether `hint` has not been asked yet.
51
    #[must_use]
52
36
    pub fn wants(&self, hint: &Hint) -> bool {
53
36
        !self.answers.contains_key(&hint.source())
54
36
    }
55

            
56
21
    pub fn asked(&mut self, hint: &Hint, seq: u64) {
57
21
        self.answers.insert(hint.source(), (seq, Vec::new()));
58
21
    }
59

            
60
16
    pub fn arrived(&mut self, hint: &Hint, seq: u64, strings: Vec<String>) {
61
16
        if let Some(answer) = self.answers.get_mut(&hint.source())
62
16
            && answer.0 == seq
63
        {
64
15
            answer.1 = strings;
65
1
        }
66
16
    }
67

            
68
    /// The answer to `hint`, empty until it arrives.
69
    #[must_use]
70
34
    pub fn answer(&self, hint: &Hint) -> &[String] {
71
34
        self.answers
72
34
            .get(&hint.source())
73
34
            .map_or(&[], |(_, strings)| strings)
74
34
    }
75
}
76

            
77
/// The candidates that start with `typed`, ignoring case.
78
34
pub fn matching<'a>(all: &'a [String], typed: &str) -> Vec<&'a str> {
79
34
    let typed = typed.to_lowercase();
80
34
    all.iter()
81
34
        .map(String::as_str)
82
58
        .filter(|candidate| candidate.to_lowercase().starts_with(&typed))
83
        .collect()
84
34
}
85

            
86
/// What completing `typed` yields: the candidate after the one typed in full,
87
/// else the first.
88
10
pub fn completion<'a>(candidates: &[&'a str], typed: &str) -> Option<&'a str> {
89
16
    match candidates.iter().position(|candidate| *candidate == typed) {
90
2
        Some(at) => candidates.get((at + 1) % candidates.len()).copied(),
91
8
        None => candidates.first().copied(),
92
    }
93
10
}