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
use crate::{configuration::ConfigurationTree, error::ConfigurationError};

#[cfg(feature = "ini")]
mod ini;
#[cfg(feature = "json")]
mod json;
#[cfg(feature = "msgpack")]
mod msgpack;
#[cfg(feature = "serde_json5")]
mod serde_json5;
#[cfg(feature = "experimental_serde_ron")]
mod serde_ron;
#[cfg(feature = "serde_toml")]
mod serde_toml;
#[cfg(feature = "yaml")]
mod yaml;

#[cfg(feature = "ini")]
pub use ini::Ini;
#[cfg(feature = "json")]
pub use json::Json;
#[cfg(feature = "msgpack")]
pub use msgpack::Msgpack;
#[cfg(feature = "serde_json5")]
pub use serde_json5::Json5;
#[cfg(feature = "serde_toml")]
pub use serde_toml::Toml;
#[cfg(feature = "yaml")]
pub use yaml::Yaml;

/// Represents data format
pub trait Format {
    /// Transforms raw data into `ConfigurationTree`
    fn transform(&self, input: Vec<u8>) -> Result<ConfigurationTree, ConfigurationError>;
    /// Describes this `Format`.
    fn describe(&self) -> String;
}

impl<T> Format for T
where
    T: Fn(Vec<u8>) -> Result<ConfigurationTree, ConfigurationError>,
{
    fn transform(&self, input: Vec<u8>) -> Result<ConfigurationTree, ConfigurationError> {
        self(input)
    }

    fn describe(&self) -> String {
        "custom".into()
    }
}

/// Utility function to create `json` format deserializer.
#[cfg(feature = "json")]
pub fn json() -> Json {
    Json::default()
}

/// Utility function to create `yaml` format deserializer.
#[cfg(feature = "yaml")]
pub fn yaml() -> Yaml {
    Yaml::default()
}

/// Utility function to create `toml` format deserializer.
#[cfg(feature = "serde_toml")]
pub fn toml() -> Toml {
    Toml::default()
}

/// Utility function to create `json5` format deserializer.
#[cfg(feature = "serde_json5")]
pub fn json5() -> Json5 {
    Json5::default()
}

/// Utility function to create `message pack` format deserializer.
#[cfg(feature = "msgpack")]
pub fn msgpack() -> Msgpack {
    Msgpack::default()
}

/// Utility function to create `ini` format deserializer.
#[cfg(feature = "ini")]
pub fn ini() -> Ini {
    Ini::default()
}