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
150
151
152
153
154
use crate::configuration_provider::ConfigurationProvider;
use crate::value::number::Number;
use crate::value::Value;
use std::collections::HashMap;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use thiserror::Error;

#[derive(Debug)]
pub struct YamlProvider(Value);

#[derive(Error, Debug)]
pub enum CreateYamlProviderFromFileError {
    /// Failed to read file
    ///
    /// Example: File is missing
    #[error("Failed to read configuration file [file_path = `{file_path}`]")]
    FailedToReadFile {
        file_path: PathBuf,
        #[source]
        error: io::Error,
    },

    /// Failed to parse file
    ///
    /// Example: Invalid formatting
    #[error("Failed to parse configuration file [file_path = `{file_path}`]")]
    FailedToDeserialize {
        file_path: PathBuf,
        #[source]
        error: serde_yaml::Error,
    },

    /// Invalid configuration
    ///
    /// Example: Key is not a string
    #[error("Invalid configuration [file_path = `{file_path}`]")]
    InvalidConfiguration {
        file_path: PathBuf,
        #[source]
        error: InvalidConfigurationError,
    },
}

#[derive(Error, Debug)]
pub enum CreateYamlProviderFromStringError {
    /// Failed to parse file
    #[error("Failed to parse config")]
    FailedToDeserialize(#[from] serde_yaml::Error),

    /// Invalid configuration
    ///
    /// Example: Key is not a string
    #[error("Invalid configuration")]
    InvalidConfiguration(#[from] InvalidConfigurationError),
}

#[derive(Error, Debug)]
pub enum InvalidConfigurationError {
    #[error("Key is not a string")]
    KeyIsNotAString,

    #[error("Tags not supported")]
    TagsNotSupported,

    #[error("Internal error")]
    InternalError,
}

impl YamlProvider {
    pub fn from_str(config: &str) -> Result<Self, CreateYamlProviderFromStringError> {
        let value = serde_yaml::from_str::<serde_yaml::value::Value>(config)?;

        Ok(YamlProvider(map_yaml_value_into_value(value)?))
    }

    pub fn from_path(file_path: impl AsRef<Path>) -> Result<Self, CreateYamlProviderFromFileError> {
        let configuration = match fs::read_to_string(file_path.as_ref()) {
            Ok(v) => v,
            Err(error) => {
                let fp = file_path.as_ref().to_path_buf();
                return Err(CreateYamlProviderFromFileError::FailedToReadFile {
                    file_path: fp.canonicalize().unwrap_or(fp),
                    error,
                });
            }
        };

        Self::from_str(&configuration).map_err(|err| {
            let file_path = file_path.as_ref().to_path_buf();

            match err {
                CreateYamlProviderFromStringError::FailedToDeserialize(error) => {
                    CreateYamlProviderFromFileError::FailedToDeserialize { file_path, error }
                }
                CreateYamlProviderFromStringError::InvalidConfiguration(error) => {
                    CreateYamlProviderFromFileError::InvalidConfiguration { file_path, error }
                }
            }
        })
    }
}

impl ConfigurationProvider for YamlProvider {
    fn provide(&self) -> Value {
        self.0.clone()
    }
}

fn map_yaml_value_into_value(
    value: serde_yaml::value::Value,
) -> Result<Value, InvalidConfigurationError> {
    match value {
        serde_yaml::Value::Null => Ok(Value::None),
        serde_yaml::Value::Bool(bool) => Ok(Value::Bool(bool)),
        serde_yaml::Value::Number(number) => {
            if let Some(number) = number.as_i64() {
                return Ok(Value::Number(Number::Integer(number)));
            }

            if let Some(number) = number.as_u64() {
                return Ok(Value::Number(Number::UInteger(number)));
            }

            if let Some(number) = number.as_f64() {
                return Ok(Value::Number(Number::Float(number)));
            }

            Err(InvalidConfigurationError::InternalError)
        }
        serde_yaml::Value::String(string) => Ok(Value::String(string)),
        serde_yaml::Value::Sequence(seq) => Ok(Value::Array(
            seq.into_iter()
                .map(map_yaml_value_into_value)
                .collect::<Result<Vec<_>, InvalidConfigurationError>>()?,
        )),
        serde_yaml::Value::Mapping(map) => map
            .into_iter()
            .map(|(k, v)| {
                let key = match k {
                    serde_yaml::Value::String(string) => string,
                    _ => return Err(InvalidConfigurationError::KeyIsNotAString),
                };

                Ok((key, map_yaml_value_into_value(v)?))
            })
            .collect::<Result<HashMap<String, Value>, InvalidConfigurationError>>()
            .map(Value::Map),
        serde_yaml::Value::Tagged(_) => {
            return Err(InvalidConfigurationError::TagsNotSupported);
        }
    }
}