Skip to main content

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    const ID_KEY: &'static str = "column.sqs.tag.key";
27    const ID_VALUE: &'static str = "column.sqs.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<QueueTag> 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: &QueueTag) -> (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}