config_maint/file/format/
mod.rs1#![allow(unused_mut)]
4
5use source::Source;
6use std::collections::HashMap;
7use std::error::Error;
8use value::Value;
9
10#[cfg(feature = "toml")]
11mod toml;
12
13#[cfg(feature = "json")]
14mod json;
15
16#[cfg(feature = "yaml")]
17mod yaml;
18
19#[cfg(feature = "hjson")]
20mod hjson;
21
22#[cfg(feature = "ini")]
23mod ini;
24
25#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
26pub enum FileFormat {
27 #[cfg(feature = "toml")]
29 Toml,
30
31 #[cfg(feature = "json")]
33 Json,
34
35 #[cfg(feature = "yaml")]
37 Yaml,
38
39 #[cfg(feature = "hjson")]
41 Hjson,
42 #[cfg(feature = "ini")]
44 Ini,
45}
46
47lazy_static! {
48 #[doc(hidden)]
49 pub static ref ALL_EXTENSIONS: HashMap<FileFormat, Vec<&'static str>> = {
51 let mut formats: HashMap<FileFormat, Vec<_>> = HashMap::new();
52
53 #[cfg(feature = "toml")]
54 formats.insert(FileFormat::Toml, vec!["toml"]);
55
56 #[cfg(feature = "json")]
57 formats.insert(FileFormat::Json, vec!["json"]);
58
59 #[cfg(feature = "yaml")]
60 formats.insert(FileFormat::Yaml, vec!["yaml", "yml"]);
61
62 #[cfg(feature = "hjson")]
63 formats.insert(FileFormat::Hjson, vec!["hjson"]);
64
65 #[cfg(feature = "ini")]
66 formats.insert(FileFormat::Ini, vec!["ini"]);
67
68 formats
69 };
70}
71
72impl FileFormat {
73 #[doc(hidden)]
75 pub fn extensions(self) -> &'static Vec<&'static str> {
76 ALL_EXTENSIONS.get(&self).unwrap()
80 }
81
82 #[doc(hidden)]
84 #[allow(unused_variables)]
85 pub fn parse(
86 self,
87 uri: Option<&String>,
88 text: &str,
89 ) -> Result<HashMap<String, Value>, Box<dyn Error + Send + Sync>> {
90 match self {
91 #[cfg(feature = "toml")]
92 FileFormat::Toml => toml::parse(uri, text),
93
94 #[cfg(feature = "json")]
95 FileFormat::Json => json::parse(uri, text),
96
97 #[cfg(feature = "yaml")]
98 FileFormat::Yaml => yaml::parse(uri, text),
99
100 #[cfg(feature = "hjson")]
101 FileFormat::Hjson => hjson::parse(uri, text),
102
103 #[cfg(feature = "ini")]
104 FileFormat::Ini => ini::parse(uri, text),
105 }
106 }
107}