Skip to main content

tui/tabs/
templates.rs

1//! Templates tab: the user's templates, from `form-options "templates"`.
2
3use ratatui::layout::Constraint;
4use scripting::nomiscript::{Value, format_value};
5
6use crate::tabs::plist::plist_text;
7use 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)]
11pub struct TemplateRow {
12    pub id: String,
13    pub name: String,
14}
15
16/// The templates view: exactly the templates the user holds.
17pub type TemplatesTab = SelectList<TemplateRow>;
18
19impl SelectRow for TemplateRow {
20    const HEADERS: &'static [&'static str] = &["Name"];
21    const WIDTHS: &'static [Constraint] = &[Constraint::Fill(1)];
22
23    fn parse(plist: &Value) -> Result<Self, String> {
24        let malformed = || format!("malformed template: {}", format_value(plist));
25        let text = |name| plist_text(plist, name).ok_or_else(malformed);
26        Ok(Self {
27            id: text(":id")?,
28            name: text(":label")?,
29        })
30    }
31
32    fn key(&self) -> &str {
33        &self.id
34    }
35
36    fn cells(&self) -> Vec<String> {
37        vec![self.name.clone()]
38    }
39}
40
41#[cfg(test)]
42mod tests;