1
//! Templates tab: the user's templates, from `form-options "templates"`.
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 template as the server lists it: its id and unique name.
10
#[derive(Debug, Clone, PartialEq, Eq)]
11
pub struct TemplateRow {
12
    pub id: String,
13
    pub name: String,
14
}
15

            
16
/// The templates view: exactly the templates the user holds.
17
pub type TemplatesTab = SelectList<TemplateRow>;
18

            
19
impl SelectRow for TemplateRow {
20
    const HEADERS: &'static [&'static str] = &["Name"];
21
    const WIDTHS: &'static [Constraint] = &[Constraint::Fill(1)];
22

            
23
25
    fn parse(plist: &Value) -> Result<Self, String> {
24
1
        let malformed = || format!("malformed template: {}", format_value(plist));
25
50
        let text = |name| plist_text(plist, name).ok_or_else(malformed);
26
        Ok(Self {
27
25
            id: text(":id")?,
28
25
            name: text(":label")?,
29
        })
30
25
    }
31

            
32
11
    fn key(&self) -> &str {
33
11
        &self.id
34
11
    }
35

            
36
1
    fn cells(&self) -> Vec<String> {
37
1
        vec![self.name.clone()]
38
1
    }
39
}
40

            
41
#[cfg(test)]
42
mod tests;