rusticity_term/sqs/
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 QueueTag {
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.sqs.tag.key",
29            Column::Value => "column.sqs.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.sqs.tag.key" => Some(Column::Key),
47            "column.sqs.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<QueueTag> 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: &QueueTag) -> (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}