1
//! Config tab: every key of the user with its value, from `list-config`.
2

            
3
use ratatui::layout::Constraint;
4
use scripting::nomiscript::{Value, format_value};
5

            
6
use crate::tabs::plist::plist_text;
7
use crate::tabs::select_list::{SelectList, SelectRow};
8

            
9
/// One stored config key with its value.
10
pub type ConfigEntry = (String, String);
11

            
12
/// The `list-config` view: exactly the user's stored keys.
13
pub type ConfigTab = SelectList<ConfigEntry>;
14

            
15
impl SelectList<ConfigEntry> {
16
    /// Key and stored value of the selected row.
17
    #[must_use]
18
15
    pub fn selected_edit(&self) -> Option<ConfigEntry> {
19
15
        self.selected_row().cloned()
20
15
    }
21
}
22

            
23
impl SelectRow for ConfigEntry {
24
    const HEADERS: &'static [&'static str] = &["Key", "Value"];
25
    const WIDTHS: &'static [Constraint] = &[Constraint::Percentage(40), Constraint::Percentage(60)];
26

            
27
63
    fn parse(plist: &Value) -> Result<Self, String> {
28
1
        let malformed = || format!("malformed config entry: {}", format_value(plist));
29
63
        let key = plist_text(plist, ":key").ok_or_else(malformed)?;
30
63
        let value = plist_text(plist, ":value").ok_or_else(malformed)?;
31
62
        Ok((key, value))
32
63
    }
33

            
34
5
    fn key(&self) -> &str {
35
5
        &self.0
36
5
    }
37

            
38
75
    fn cells(&self) -> Vec<String> {
39
75
        vec![self.0.clone(), self.1.clone()]
40
75
    }
41

            
42
7
    fn has_key(&self, key: &str) -> bool {
43
7
        self.0.eq_ignore_ascii_case(key)
44
7
    }
45
}
46

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