rusticity_term/ec2/
tag.rs1use 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 pub fn id(&self) -> ColumnId {
27 match self {
28 Column::Key => "column.ec2.tag.key",
29 Column::Value => "column.ec2.tag.value",
30 }
31 }
32
33 pub fn default_name(&self) -> &'static str {
34 match self {
35 Column::Key => "Key",
36 Column::Value => "Value",
37 }
38 }
39
40 pub fn name(&self) -> String {
41 translate_column(self.id(), self.default_name())
42 }
43
44 pub fn from_id(id: &str) -> Option<Self> {
45 match id {
46 "column.ec2.tag.key" => Some(Column::Key),
47 "column.ec2.tag.value" => Some(Column::Value),
48 _ => None,
49 }
50 }
51
52 pub fn all() -> [Column; 2] {
53 [Column::Key, Column::Value]
54 }
55
56 pub fn ids() -> Vec<ColumnId> {
57 Self::all().iter().map(|c| c.id()).collect()
58 }
59}
60
61impl TableColumn<InstanceTag> for Column {
62 fn name(&self) -> &str {
63 Box::leak(translate_column(self.id(), self.default_name()).into_boxed_str())
64 }
65
66 fn width(&self) -> u16 {
67 let translated = translate_column(self.id(), self.default_name());
68 translated.len().max(20) as u16
69 }
70
71 fn render(&self, item: &InstanceTag) -> (String, ratatui::style::Style) {
72 let text = match self {
73 Column::Key => item.key.clone(),
74 Column::Value => item.value.clone(),
75 };
76 (text, ratatui::style::Style::default())
77 }
78}
79
80#[cfg(test)]
81mod tests {
82 use super::*;
83
84 #[test]
85 fn test_column_ids() {
86 let ids = Column::ids();
87 assert_eq!(ids.len(), 2);
88 assert_eq!(ids[0], "column.ec2.tag.key");
89 assert_eq!(ids[1], "column.ec2.tag.value");
90 }
91
92 #[test]
93 fn test_column_from_id() {
94 assert_eq!(Column::from_id("column.ec2.tag.key"), Some(Column::Key));
95 assert_eq!(Column::from_id("column.ec2.tag.value"), Some(Column::Value));
96 assert_eq!(Column::from_id("invalid"), None);
97 }
98
99 #[test]
100 fn test_column_names() {
101 assert_eq!(Column::Key.default_name(), "Key");
102 assert_eq!(Column::Value.default_name(), "Value");
103 }
104}