Skip to main content

rusticity_term/
lib.rs

1pub mod apig;
2pub mod app;
3pub mod aws;
4pub mod cfn;
5pub mod clipboard;
6pub mod cloudtrail;
7pub mod common;
8pub mod cw;
9pub mod dynamodb;
10pub mod ec2;
11pub mod ecr;
12pub mod efs;
13pub mod event;
14pub mod finder;
15pub mod fsx;
16pub mod iam;
17pub mod inventory;
18pub mod keymap;
19pub mod kms;
20pub mod lambda;
21pub mod preferences;
22pub mod rds;
23pub mod s3;
24pub mod session;
25pub mod sqs;
26pub mod table;
27pub mod tags;
28pub mod ui;
29pub mod vpc;
30
31pub use app::{
32    App, DetailTab, EventColumn, EventFilterFocus, LogGroupColumn, Service, StreamSort, ViewMode,
33};
34pub use cw::insights::{DateRangeType, InsightsFocus, InsightsState, QueryLanguage, TimeUnit};
35use std::collections::HashMap;
36
37/// Initialize all services (i18n, etc.)
38pub fn init() {
39    // Load column customizations from config.toml
40    let mut i18n = HashMap::new();
41    if let Some(home) = std::env::var_os("HOME") {
42        let config_path = std::path::Path::new(&home)
43            .join(".config")
44            .join("rusticity")
45            .join("config.toml");
46
47        if let Ok(contents) = std::fs::read_to_string(&config_path) {
48            if let Ok(toml_map) = contents.parse::<toml::Table>() {
49                if let Some(columns_section) = toml_map.get("columns").and_then(|v| v.as_table()) {
50                    flatten_toml(columns_section, "column", &mut i18n);
51                }
52            }
53        }
54    }
55
56    // Initialize each service to populate their column defaults
57    apig::init(&mut i18n);
58    lambda::init(&mut i18n);
59    ec2::init(&mut i18n);
60    ecr::init(&mut i18n);
61    efs::init(&mut i18n);
62    fsx::init(&mut i18n);
63    dynamodb::init(&mut i18n);
64    rds::init(&mut i18n);
65    vpc::init(&mut i18n);
66    s3::init(&mut i18n);
67    cw::init(&mut i18n);
68    sqs::init(&mut i18n);
69    cfn::init(&mut i18n);
70    cloudtrail::init(&mut i18n);
71    iam::init(&mut i18n);
72    kms::init(&mut i18n);
73
74    // Set shared i18n map after all defaults are added
75    common::set_i18n(i18n);
76}
77
78fn flatten_toml(
79    table: &toml::Table,
80    prefix: &str,
81    map: &mut std::collections::HashMap<String, String>,
82) {
83    for (key, value) in table {
84        let full_key = format!("{}.{}", prefix, key);
85        match value {
86            toml::Value::String(s) => {
87                map.insert(full_key, s.clone());
88            }
89            toml::Value::Table(t) => {
90                flatten_toml(t, &full_key, map);
91            }
92            _ => {}
93        }
94    }
95}
96
97pub use event::EventHandler;
98pub use session::Session;
99
100#[cfg(test)]
101mod config_tests {
102    use super::*;
103
104    #[test]
105    fn toml_column_overrides_preserve_nested_unicode_and_quoted_keys() {
106        let config: toml::Table = r#"
107            [columns.lambda.function]
108            name = "Function Name"
109            description = "説明"
110            [columns.dynamodb.table]
111            "partition_key" = "Order identifier"
112            ignored_number = 3
113        "#
114        .parse()
115        .unwrap();
116        let mut overrides = HashMap::new();
117        flatten_toml(
118            config["columns"].as_table().unwrap(),
119            "column",
120            &mut overrides,
121        );
122        assert_eq!(overrides.len(), 3);
123        assert_eq!(overrides["column.lambda.function.name"], "Function Name");
124        assert_eq!(overrides["column.lambda.function.description"], "説明");
125        assert_eq!(
126            overrides["column.dynamodb.table.partition_key"],
127            "Order identifier"
128        );
129        // Service defaults must not replace user customizations.
130        dynamodb::init(&mut overrides);
131        assert_eq!(
132            overrides["column.dynamodb.table.partition_key"],
133            "Order identifier"
134        );
135    }
136
137    #[test]
138    fn malformed_toml_still_returns_an_error() {
139        assert!("[columns\nname =".parse::<toml::Table>().is_err());
140    }
141}