1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
use std::cmp::Ordering;
use std::path::PathBuf;
use std::{
    collections::{BTreeMap, HashMap},
    path::Path,
};

use fs_err as fs;
use serde_derive::{Deserialize, Serialize};
use teller_providers::config::{PathMap, ProviderCfg, KV};
use teller_providers::providers::ProviderKind;
use tera::{Context, Tera};

use crate::Result;

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Config {
    pub providers: BTreeMap<String, ProviderCfg>,
}

#[derive(Serialize)]
pub struct RenderTemplate {
    pub providers: Vec<ProviderKind>,
}

fn apply_eqeq(config: &mut Config) {
    config.providers.iter_mut().for_each(|(_name, provider)| {
        provider.maps.iter_mut().for_each(|pm| {
            pm.keys.iter_mut().for_each(|(k, v)| {
                // THINK: replace with:
                // 1. templating: {{id}} (identity), {{snake_case}} (snake case it)
                // 2. other symbols: == id, ^^ capitalize, snake case __ lower snake case
                if v == "==" {
                    v.clone_from(k);
                }
            });
        });
    });
}

impl Config {
    /// Config from text
    ///
    /// # Errors
    ///
    /// This function will return an error if serialization fails
    pub fn with_vars(text: &str, vars: &HashMap<String, String>) -> Result<Self> {
        let rendered_text = Tera::one_off(text, &Context::from_serialize(vars)?, false)?;
        let mut config: Self = serde_yaml::from_str(&rendered_text)?;

        apply_eqeq(&mut config);

        Ok(config)
    }

    /// Config from text
    ///
    /// # Errors
    ///
    /// This function will return an error if serialization fails
    pub fn from_text(text: &str) -> Result<Self> {
        Self::with_vars(text, &HashMap::new())
    }

    /// Config from file
    ///
    /// # Errors
    ///
    /// This function will return an error if IO fails
    pub fn from_path(path: &Path) -> Result<Self> {
        Self::from_text(&fs::read_to_string(path)?)
    }

    /// Create configuration template file
    ///
    /// # Errors
    /// When could not convert config to string
    pub fn render_template(data: &RenderTemplate) -> Result<String> {
        let res: BTreeMap<String, ProviderCfg> = data
            .providers
            .iter()
            .map(|p| {
                (
                    format!("{p}_1"),
                    ProviderCfg {
                        kind: p.clone(),
                        maps: vec![PathMap::from_path("example/dev")],
                        ..ProviderCfg::default()
                    },
                )
            })
            .collect();

        let config = Self { providers: res };

        let a: String = serde_yaml::to_string(&config)?;
        Ok(a)
    }
}

#[derive(Debug, Clone, Serialize, Eq, PartialEq)]
pub struct Match {
    pub path: PathBuf,
    pub position: Option<(usize, usize)>,
    pub offset: usize,
    pub query: KV,
}

impl PartialOrd for Match {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Match {
    fn cmp(&self, other: &Self) -> Ordering {
        let query_cmp = self.query.cmp(&other.query);

        if query_cmp != Ordering::Equal {
            return query_cmp;
        }

        self.offset.cmp(&other.offset)
    }
}

#[cfg(test)]
mod tests {
    use insta::assert_yaml_snapshot;

    use super::*;
    #[test]
    fn load_config() {
        std::env::set_var("TEST_LOAD_1", "DEV");
        let config = Config::from_path(Path::new("fixtures/config.yml")).unwrap();
        assert_eq!(config.providers.len(), 2);
        assert_yaml_snapshot!(config);
    }

    #[test]
    fn can_render_template_config() {
        let data = RenderTemplate {
            providers: vec![ProviderKind::Inmem, ProviderKind::Dotenv],
        };

        let config = Config::render_template(&data).unwrap();
        assert_yaml_snapshot!(config);
    }
}