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
use crate::{
    entity::ConfigurationEntity,
    loader::{ConfigurationLoadError, ConfigurationLoader},
};
use std::{
    collections::HashMap,
    fmt::{Debug, Formatter},
};
use url::Url;

pub type BoxedLoaderFn = Box<
    dyn Fn(
            Url,
            Option<&[String]>,
        ) -> Result<HashMap<String, ConfigurationEntity>, ConfigurationLoadError>
        + Send
        + Sync,
>;

pub struct ConfigurationLoaderFn {
    name: &'static str,
    loader: BoxedLoaderFn,
    scheme_list: Vec<String>,
}

impl Debug for ConfigurationLoaderFn {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ConfigurationLoaderFn")
            .field("name", &self.name)
            .field(
                "loader",
                &stringify!(Box<dyn Fn(&str, Option<&[String]>) -> Result<HashMap<String, Configuration>, ConfigurationLoadError> + Send + Sync),
            )
            .finish()
    }
}

impl ConfigurationLoaderFn {
    pub fn new<S: AsRef<str>>(name: &'static str, loader: BoxedLoaderFn, scheme: S) -> Self {
        Self {
            name,
            loader,
            scheme_list: [scheme.as_ref().to_string()].to_vec(),
        }
    }

    pub fn set_name(&mut self, name: &'static str) {
        self.name = name
    }

    pub fn with_name(mut self, name: &'static str) -> Self {
        self.set_name(name);
        self
    }

    pub fn set_loader(&mut self, loader: BoxedLoaderFn) {
        self.loader = loader
    }

    pub fn with_loader(mut self, loader: BoxedLoaderFn) -> Self {
        self.set_loader(loader);
        self
    }

    pub fn set_scheme_list<S: AsRef<str>>(&mut self, scheme_list: Vec<S>) {
        self.scheme_list = scheme_list
            .into_iter()
            .map(|scheme| scheme.as_ref().to_string())
            .collect();
    }

    pub fn with_scheme_list<S: AsRef<str>>(mut self, scheme_list: Vec<S>) -> Self {
        self.set_scheme_list(scheme_list);
        self
    }
}

impl ConfigurationLoader for ConfigurationLoaderFn {
    fn name(&self) -> &'static str {
        self.name
    }

    fn scheme_list(&self) -> Vec<String> {
        self.scheme_list.clone()
    }

    fn try_load(
        &self,
        source: Url,
        maybe_whitelist: Option<&[String]>,
    ) -> Result<HashMap<String, ConfigurationEntity>, ConfigurationLoadError> {
        (self.loader)(source, maybe_whitelist)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::logging::enable_logging;

    #[test]
    fn load() {
        enable_logging();
        // let mut l = ConfigurationLoaderEnv::new("FOO")
        //     .unwrap()
        //     .with_key_separator("_");
        // println!("{l:?}");
        // let loaded = l.try_load().unwrap();
        // println!("{loaded:#?}");
        // for (p, r) in loaded {
        //     println!("{p}: {:?}\n\n\n\n", r.deserialize());
        // }
    }
}