Skip to main content

rusticity_term/apig/
resource.rs

1use crate::ui::table::Column as TableColumn;
2use crate::ui::tree::TreeItem;
3use ratatui::prelude::*;
4pub use rusticity_core::apig::Resource;
5
6impl TreeItem for Resource {
7    fn id(&self) -> &str {
8        &self.id
9    }
10
11    fn display_name(&self) -> &str {
12        &self.display_name
13    }
14
15    fn is_expandable(&self) -> bool {
16        !self.methods.is_empty()
17    }
18
19    fn icon(&self) -> &str {
20        ""
21    }
22}
23
24#[derive(Debug, Clone, Copy, PartialEq)]
25pub enum Column {
26    Path,
27    Id,
28    Arn,
29}
30
31impl Column {
32    const ID_PATH: &'static str = "path";
33    const ID_ID: &'static str = "id";
34    const ID_ARN: &'static str = "arn";
35
36    pub const fn id(&self) -> &'static str {
37        match self {
38            Column::Path => Self::ID_PATH,
39            Column::Id => Self::ID_ID,
40            Column::Arn => Self::ID_ARN,
41        }
42    }
43
44    pub fn from_id(id: &str) -> Option<Self> {
45        match id {
46            Self::ID_PATH => Some(Column::Path),
47            Self::ID_ID => Some(Column::Id),
48            Self::ID_ARN => Some(Column::Arn),
49            _ => None,
50        }
51    }
52
53    pub const fn all() -> [Column; 3] {
54        [Column::Path, Column::Id, Column::Arn]
55    }
56
57    pub fn ids() -> Vec<&'static str> {
58        Self::all().iter().map(|c| c.id()).collect()
59    }
60}
61
62impl TableColumn<Resource> for Column {
63    fn name(&self) -> &str {
64        match self {
65            Column::Path => "Resource",
66            Column::Id => "ID",
67            Column::Arn => "ARN",
68        }
69    }
70
71    fn width(&self) -> u16 {
72        match self {
73            Column::Path => 40,
74            Column::Id => 20,
75            Column::Arn => 60,
76        }
77    }
78
79    fn render(&self, item: &Resource) -> (String, Style) {
80        let text = match self {
81            Column::Path => item.path.clone(),
82            Column::Id => item.id.clone(),
83            Column::Arn => item.arn.clone(),
84        };
85        (text, Style::default())
86    }
87}