-
Notifications
You must be signed in to change notification settings - Fork 265
Expand file tree
/
Copy pathmod.rs
More file actions
188 lines (150 loc) · 4.68 KB
/
mod.rs
File metadata and controls
188 lines (150 loc) · 4.68 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use std::error::Error;
use crate::map::Map;
use crate::{Format, file::FileStoredFormat, value::Value};
#[cfg(feature = "toml")]
mod toml;
#[cfg(feature = "json")]
mod json;
#[cfg(feature = "yaml")]
mod yaml;
#[cfg(feature = "ini")]
mod ini;
#[cfg(feature = "ron")]
mod ron;
#[cfg(feature = "json5")]
mod json5;
#[cfg(feature = "corn")]
mod corn;
#[cfg(feature = "dotenv")]
mod dotenv;
/// File formats provided by the library.
///
/// Although it is possible to define custom formats using [`Format`] trait it is recommended to use `FileFormat` if possible.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
#[non_exhaustive]
pub enum FileFormat {
/// TOML (parsed with toml)
#[cfg(feature = "toml")]
Toml,
/// JSON (parsed with `serde_json`)
#[cfg(feature = "json")]
Json,
/// YAML (parsed with `yaml_rust2`)
#[cfg(feature = "yaml")]
Yaml,
/// INI (parsed with `rust_ini`)
#[cfg(feature = "ini")]
Ini,
/// RON (parsed with ron)
#[cfg(feature = "ron")]
Ron,
/// JSON5 (parsed with json5)
#[cfg(feature = "json5")]
Json5,
/// Corn (parsed with `libcorn`)
#[cfg(feature = "corn")]
Corn,
/// Dotenv (parsed with `dotenvy`)
#[cfg(feature = "dotenv")]
Dotenv,
}
impl FileFormat {
pub(crate) fn all() -> &'static [FileFormat] {
&[
#[cfg(feature = "toml")]
FileFormat::Toml,
#[cfg(feature = "json")]
FileFormat::Json,
#[cfg(feature = "yaml")]
FileFormat::Yaml,
#[cfg(feature = "ini")]
FileFormat::Ini,
#[cfg(feature = "ron")]
FileFormat::Ron,
#[cfg(feature = "json5")]
FileFormat::Json5,
#[cfg(feature = "corn")]
FileFormat::Corn,
#[cfg(feature = "dotenv")]
FileFormat::Dotenv,
]
}
pub(crate) fn extensions(&self) -> &'static [&'static str] {
match self {
#[cfg(feature = "toml")]
FileFormat::Toml => &["toml"],
#[cfg(feature = "json")]
FileFormat::Json => &["json"],
#[cfg(feature = "yaml")]
FileFormat::Yaml => &["yaml", "yml"],
#[cfg(feature = "ini")]
FileFormat::Ini => &["ini"],
#[cfg(feature = "ron")]
FileFormat::Ron => &["ron"],
#[cfg(feature = "json5")]
FileFormat::Json5 => &["json5"],
#[cfg(feature = "corn")]
FileFormat::Corn => &["corn"],
#[cfg(feature = "dotenv")]
FileFormat::Dotenv => &["dotenv"],
#[cfg(all(
not(feature = "toml"),
not(feature = "json"),
not(feature = "yaml"),
not(feature = "ini"),
not(feature = "ron"),
not(feature = "json5"),
not(feature = "dotenv"),
))]
_ => unreachable!("No features are enabled, this library won't work without features"),
}
}
pub(crate) fn parse(
&self,
uri: Option<&String>,
text: &str,
) -> Result<Map<String, Value>, Box<dyn Error + Send + Sync>> {
match self {
#[cfg(feature = "toml")]
FileFormat::Toml => toml::parse(uri, text),
#[cfg(feature = "json")]
FileFormat::Json => json::parse(uri, text),
#[cfg(feature = "yaml")]
FileFormat::Yaml => yaml::parse(uri, text),
#[cfg(feature = "ini")]
FileFormat::Ini => ini::parse(uri, text),
#[cfg(feature = "ron")]
FileFormat::Ron => ron::parse(uri, text),
#[cfg(feature = "json5")]
FileFormat::Json5 => json5::parse(uri, text),
#[cfg(feature = "corn")]
FileFormat::Corn => corn::parse(uri, text),
#[cfg(feature = "dotenv")]
FileFormat::Dotenv => dotenv::parse(uri, text),
#[cfg(all(
not(feature = "toml"),
not(feature = "json"),
not(feature = "yaml"),
not(feature = "ini"),
not(feature = "ron"),
not(feature = "json5"),
not(feature = "dotenv"),
))]
_ => unreachable!("No features are enabled, this library won't work without features"),
}
}
}
impl Format for FileFormat {
fn parse(
&self,
uri: Option<&String>,
text: &str,
) -> Result<Map<String, Value>, Box<dyn Error + Send + Sync>> {
self.parse(uri, text)
}
}
impl FileStoredFormat for FileFormat {
fn file_extensions(&self) -> &'static [&'static str] {
self.extensions()
}
}