Skip to main content

rusticity_term/ecr/
repo.rs

1use crate::common::translate_column;
2use crate::common::{format_iso_timestamp, ColumnId, UTC_TIMESTAMP_WIDTH};
3use crate::ui::table::Column as TableColumn;
4use ratatui::prelude::*;
5use std::collections::HashMap;
6
7pub fn init(i18n: &mut HashMap<String, String>) {
8    for col in Column::all() {
9        i18n.entry(col.id().to_string())
10            .or_insert_with(|| col.default_name().to_string());
11    }
12}
13
14#[derive(Debug, Clone)]
15pub struct Repository {
16    pub name: String,
17    pub uri: String,
18    pub created_at: String,
19    pub tag_immutability: String,
20    pub encryption_type: String,
21}
22
23#[derive(Debug, Clone, Copy, PartialEq)]
24pub enum Column {
25    Name,
26    Uri,
27    CreatedAt,
28    TagImmutability,
29    EncryptionType,
30}
31
32impl Column {
33    const ID_NAME: &'static str = "column.ecr.repo.name";
34    const ID_URI: &'static str = "column.ecr.repo.uri";
35    const ID_CREATED_AT: &'static str = "column.ecr.repo.created_at";
36    const ID_TAG_IMMUTABILITY: &'static str = "column.ecr.repo.tag_immutability";
37    const ID_ENCRYPTION_TYPE: &'static str = "column.ecr.repo.encryption_type";
38
39    pub const fn id(&self) -> &'static str {
40        match self {
41            Column::Name => Self::ID_NAME,
42            Column::Uri => Self::ID_URI,
43            Column::CreatedAt => Self::ID_CREATED_AT,
44            Column::TagImmutability => Self::ID_TAG_IMMUTABILITY,
45            Column::EncryptionType => Self::ID_ENCRYPTION_TYPE,
46        }
47    }
48
49    pub const fn default_name(&self) -> &'static str {
50        match self {
51            Column::Name => "Repository name",
52            Column::Uri => "URI",
53            Column::CreatedAt => "Created at",
54            Column::TagImmutability => "Tag immutability",
55            Column::EncryptionType => "Encryption type",
56        }
57    }
58
59    pub const fn all() -> [Column; 5] {
60        [
61            Column::Name,
62            Column::Uri,
63            Column::CreatedAt,
64            Column::TagImmutability,
65            Column::EncryptionType,
66        ]
67    }
68
69    pub fn ids() -> Vec<ColumnId> {
70        Self::all().iter().map(|c| c.id()).collect()
71    }
72
73    pub fn from_id(id: &str) -> Option<Self> {
74        match id {
75            Self::ID_NAME => Some(Column::Name),
76            Self::ID_URI => Some(Column::Uri),
77            Self::ID_CREATED_AT => Some(Column::CreatedAt),
78            Self::ID_TAG_IMMUTABILITY => Some(Column::TagImmutability),
79            Self::ID_ENCRYPTION_TYPE => Some(Column::EncryptionType),
80            _ => None,
81        }
82    }
83
84    pub fn name(&self) -> String {
85        translate_column(self.id(), self.default_name())
86    }
87}
88
89impl TableColumn<Repository> for Column {
90    fn name(&self) -> &str {
91        Box::leak(translate_column(self.id(), self.default_name()).into_boxed_str())
92    }
93
94    fn width(&self) -> u16 {
95        let translated = translate_column(self.id(), self.default_name());
96        translated.len().max(match self {
97            Column::Name => 30,
98            Column::Uri => 50,
99            Column::CreatedAt => UTC_TIMESTAMP_WIDTH as usize,
100            Column::TagImmutability => 18,
101            Column::EncryptionType => 18,
102        }) as u16
103    }
104
105    fn render(&self, item: &Repository) -> (String, Style) {
106        let text = match self {
107            Column::Name => item.name.clone(),
108            Column::Uri => item.uri.clone(),
109            Column::CreatedAt => format_iso_timestamp(&item.created_at),
110            Column::TagImmutability => item.tag_immutability.clone(),
111            Column::EncryptionType => match item.encryption_type.as_str() {
112                "AES256" => "AES-256".to_string(),
113                "KMS" => "KMS".to_string(),
114                other => other.to_string(),
115            },
116        };
117        (text, Style::default())
118    }
119}
120
121#[cfg(test)]
122mod tests {
123    use super::*;
124
125    #[test]
126    fn test_column_ids_have_correct_prefix() {
127        for col in Column::all() {
128            assert!(
129                col.id().starts_with("column.ecr.repo."),
130                "Column ID '{}' should start with 'column.ecr.repo.'",
131                col.id()
132            );
133        }
134    }
135}