Skip to main content

rusticity_term/ec2/
tag.rs

1use crate::common::{translate_column, ColumnId};
2use crate::ui::table::Column as TableColumn;
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6pub fn init(i18n: &mut HashMap<String, String>) {
7    for col in Column::all() {
8        i18n.entry(col.id().to_string())
9            .or_insert_with(|| col.default_name().to_string());
10    }
11}
12
13#[derive(Debug, Clone, Serialize, Deserialize)]
14pub struct InstanceTag {
15    pub key: String,
16    pub value: String,
17}
18
19#[derive(Debug, Clone, Copy, PartialEq)]
20pub enum Column {
21    Key,
22    Value,
23}
24
25impl Column {
26    const ID_KEY: &'static str = "column.ec2.tag.key";
27    const ID_VALUE: &'static str = "column.ec2.tag.value";
28
29    pub const fn id(&self) -> ColumnId {
30        match self {
31            Column::Key => Self::ID_KEY,
32            Column::Value => Self::ID_VALUE,
33        }
34    }
35
36    pub const fn default_name(&self) -> &'static str {
37        match self {
38            Column::Key => "Key",
39            Column::Value => "Value",
40        }
41    }
42
43    pub fn name(&self) -> String {
44        translate_column(self.id(), self.default_name())
45    }
46
47    pub fn from_id(id: &str) -> Option<Self> {
48        match id {
49            Self::ID_KEY => Some(Column::Key),
50            Self::ID_VALUE => Some(Column::Value),
51            _ => None,
52        }
53    }
54
55    pub const fn all() -> [Column; 2] {
56        [Column::Key, Column::Value]
57    }
58
59    pub fn ids() -> Vec<ColumnId> {
60        Self::all().iter().map(|c| c.id()).collect()
61    }
62}
63
64impl TableColumn<InstanceTag> for Column {
65    fn name(&self) -> &str {
66        Box::leak(translate_column(self.id(), self.default_name()).into_boxed_str())
67    }
68
69    fn width(&self) -> u16 {
70        let translated = translate_column(self.id(), self.default_name());
71        translated.len().max(20) as u16
72    }
73
74    fn render(&self, item: &InstanceTag) -> (String, ratatui::style::Style) {
75        let text = match self {
76            Column::Key => item.key.clone(),
77            Column::Value => item.value.clone(),
78        };
79        (text, ratatui::style::Style::default())
80    }
81}
82
83#[cfg(test)]
84mod tests {
85    use super::*;
86
87    #[test]
88    fn test_column_ids() {
89        let ids = Column::ids();
90        assert_eq!(ids.len(), 2);
91        assert_eq!(ids[0], "column.ec2.tag.key");
92        assert_eq!(ids[1], "column.ec2.tag.value");
93    }
94
95    #[test]
96    fn test_column_from_id() {
97        assert_eq!(Column::from_id("column.ec2.tag.key"), Some(Column::Key));
98        assert_eq!(Column::from_id("column.ec2.tag.value"), Some(Column::Value));
99        assert_eq!(Column::from_id("invalid"), None);
100    }
101
102    #[test]
103    fn test_column_names() {
104        assert_eq!(Column::Key.default_name(), "Key");
105        assert_eq!(Column::Value.default_name(), "Value");
106    }
107}