rm_config/
categories.rs

1use std::collections::HashMap;
2
3use intuitils::config::IntuiConfig;
4use ratatui::style::Color;
5use serde::Deserialize;
6
7#[derive(Deserialize)]
8pub struct CategoriesConfig {
9    #[serde(default)]
10    pub categories: Vec<Category>,
11    #[serde(skip)]
12    pub map: HashMap<String, Category>,
13    #[serde(skip)]
14    pub max_name_len: u8,
15    #[serde(skip)]
16    pub max_icon_len: u8,
17}
18
19impl IntuiConfig for CategoriesConfig {
20    fn app_name() -> &'static str {
21        "rustmission"
22    }
23
24    fn filename() -> &'static str {
25        "categories.toml"
26    }
27
28    fn default_config() -> &'static str {
29        include_str!("../defaults/categories.toml")
30    }
31
32    fn should_exit_if_not_found() -> bool {
33        false
34    }
35
36    fn message_if_not_found() -> Option<String> {
37        None
38    }
39
40    fn post_init(&mut self) {
41        self.populate_hashmap();
42        self.set_lengths();
43    }
44}
45
46#[derive(Deserialize, Clone)]
47pub struct Category {
48    pub name: String,
49    pub icon: String,
50    pub color: Color,
51    pub default_dir: String,
52}
53
54impl CategoriesConfig {
55    pub fn is_empty(&self) -> bool {
56        self.categories.is_empty()
57    }
58
59    fn populate_hashmap(&mut self) {
60        for category in &self.categories {
61            self.map.insert(category.name.clone(), category.clone());
62        }
63    }
64
65    fn set_lengths(&mut self) {
66        let mut max_icon_len = 0u8;
67        let mut max_name_len = 0u8;
68
69        for category in &self.categories {
70            let name_len = u8::try_from(category.name.chars().count()).unwrap_or(u8::MAX);
71            let icon_len = u8::try_from(category.icon.chars().count()).unwrap_or(u8::MAX);
72
73            if name_len > max_name_len {
74                max_name_len = name_len;
75            }
76
77            if icon_len > max_icon_len {
78                max_icon_len = icon_len
79            }
80        }
81
82        self.max_name_len = max_name_len;
83        self.max_icon_len = max_icon_len;
84    }
85}