-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlib.rs
427 lines (393 loc) · 14.9 KB
/
lib.rs
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#![doc = include_str!("../README.md")]
pub mod error;
pub mod error_messages;
mod extensions;
mod format_dependant;
mod utils;
use serde::{Deserialize, Serialize};
use std::ffi::OsStr;
use std::fmt::{Display, Formatter};
use std::fs;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
#[cfg(not(any(feature = "json", feature = "json5", feature = "toml", feature = "yaml")))]
compile_error!("You must install at least one format feature: `json`, `json5`, `toml`, or `yaml`");
// ^ --- HEY, user! --- ^
// To do this, you can replace `fast_config = ".."` with
// `fast_config = { version = "..", features = ["json"] }` in your cargo.toml file.
// You can simply replace that "json" with any of the stated above if you want other formats.
// Bug testing
#[cfg(test)]
mod tests;
// Separated things
#[allow(unused)]
pub use error_messages::*;
/// Enum used to configure the [`Config`]s file format.
///
/// You can use it in a [`ConfigSetupOptions`], inside [`Config::from_options`]
///
/// ## ⚠️ Make sure to enable the feature flag for a format before using it!
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum ConfigFormat {
JSON,
JSON5,
TOML,
YAML,
}
impl ConfigFormat {
/// Mainly used to convert file extensions into [`ConfigFormat`]s <br/>
/// Also chooses the correct extension for both JSON types based on the enabled feature. _(ex: if JSON5 is enabled, it chooses it for the "json" file extension)_ <br/>
/// Returns [`None`] if the string/extension doesn't match any known format.
///
/// # Example:
/// ```
/// # use std::ffi::OsStr;
/// # use fast_config::ConfigFormat;
/// if cfg!(feature = "json") {
/// assert_eq!(
/// ConfigFormat::from_extension(OsStr::new("json")).unwrap(),
/// ConfigFormat::JSON
/// );
/// } else if cfg!(feature = "json5") {
/// assert_eq!(
/// ConfigFormat::from_extension(OsStr::new("json5")).unwrap(),
/// ConfigFormat::JSON5
/// );
/// }
/// ```
pub fn from_extension(ext: &OsStr) -> Option<Self> {
let ext = ext
.to_ascii_lowercase()
.to_string_lossy()
.replace('\u{FFFD}', "");
// Special case for JSON5 since it shares a format with JSON
if cfg!(feature = "json5") && !cfg!(feature = "json") && (ext == "json" || ext == "json5") {
return Some(ConfigFormat::JSON5);
}
// Matching
match ext.as_str() {
"json" => Some(ConfigFormat::JSON),
"toml" => Some(ConfigFormat::TOML),
"yaml" | "yml" => Some(ConfigFormat::YAML),
_ => None,
}
}
}
impl Display for ConfigFormat {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let output = match self {
ConfigFormat::JSON => "json",
ConfigFormat::JSON5 => "json5",
ConfigFormat::TOML => "toml",
ConfigFormat::YAML => "yaml",
};
write!(f, "{output}")
}
}
impl Default for ConfigFormat {
fn default() -> Self {
format_dependant::get_first_enabled_feature()
}
}
/// Used to configure the [`Config`] object
///
/// [`UnknownFormatError`]: error::UnknownFormatError
///
/// # Attributes
/// - `pretty` - Makes the contents of the config file more humanly-readable.
/// When `false`, it will try to compact down the config file data so it takes up less storage space.
/// I recommend you keep it on unless you know what you're doing as most modern systems have enough
/// space to handle spaces and newline characters even at scale.
///
/// - `format` - An [`Option`] containing an enum of type [`ConfigFormat`].
/// Used to specify the format language to use *(ex: JSON, TOML, etc.)* <br/>
/// If you don't select a format *(Option::None)* it will try to guess the format
/// based on the file extension and enabled features. <br/>
/// If this step fails, an [`UnknownFormatError`] will be returned.
///
/// # More options are to be added later!
/// Pass `.. `[`Default::default()`] at the end of your construction
/// to prevent yourself from getting errors in the future!
///
/// # Examples:
/// ```
/// use fast_config::{ConfigSetupOptions, ConfigFormat, Config};
/// use serde::{Serialize, Deserialize};
///
/// // Creating a config struct to store our data
/// #[derive(Serialize, Deserialize)]
/// pub struct MyData {
/// pub some_data: i32
/// }
///
/// // Creating the options
/// let options = ConfigSetupOptions {
/// pretty: false,
/// format: Some(ConfigFormat::JSON),
/// .. Default::default()
/// };
///
/// // Creating the data and setting it's default values
/// let data = MyData {
/// some_data: 12345
/// };
///
/// // Creating the config itself
/// let mut config = Config::from_options("./config/myconfig", options, data).unwrap();
/// // [.. do stuff here]
/// # // Cleanup
/// # match std::fs::remove_dir_all("./config/") {
/// # Err(e) => {
/// # log::error!("{e}");
/// # },
/// # Ok(_) => {}
/// # }
/// ```
#[derive(Clone, Copy)]
pub struct ConfigSetupOptions {
pub pretty: bool,
pub format: Option<ConfigFormat>,
#[allow(deprecated)]
#[deprecated(note = "This option can result in I/O during program exit and can potentially corrupt config files!\nUse [`Config::save`] while your program is exiting instead!")]
pub save_on_drop: bool,
}
impl Default for ConfigSetupOptions {
fn default() -> Self {
#[allow(deprecated)]
Self {
pretty: true,
format: None,
save_on_drop: false,
}
}
}
/// The internally-stored settings type for [`Config`] <br/>
/// Works and looks like [`ConfigSetupOptions`], with a few internally-required key differences.
pub struct InternalOptions {
pub pretty: bool,
pub format: ConfigFormat,
pub save_on_drop: bool,
}
impl TryFrom<ConfigSetupOptions> for InternalOptions {
/// This function converts a [`ConfigSetupOptions`] into an internally-used [`InternalOptions`].
///
/// This function is not recommended to be used outside the `fast_config` source code
/// unless you know what you're doing and accept the risks. <br/>
/// The signature or behaviour of the function may be modified in the future.
type Error = String;
fn try_from(options: ConfigSetupOptions) -> Result<Self, Self::Error> {
// Getting the formatting language.
let format = match options.format {
Some(format) => format,
None => Err("The file format could not be guessed! It appears to be None!")?,
};
// Constructing a converted type
Ok(Self {
pretty: options.pretty,
format,
#[allow(deprecated)] save_on_drop: options.save_on_drop,
})
}
}
/// The main class you use to create/access your configuration files!
///
/// # Construction
/// See [`Config::new`] and [`Config::from_options`] if you wish to construct a new `Config`!
///
/// # Data
/// This class stores data within a data struct you define yourself.
/// This allows for the most amount of performance and safety,
/// while also allowing you to add additional features by adding `impl` blocks on your struct.
///
/// Your data struct needs to implement [`Serialize`] and [`Deserialize`].
/// In most cases you can just use `#[derive(Serialize, Deserialize)]` to derive them.
///
/// # Examples
/// Here is a code example on how you could define the data to pass into the constructors on this class:
/// ```
/// use serde::{Serialize, Deserialize};
///
/// // Creating a config struct to store our data
/// #[derive(Serialize, Deserialize)]
/// struct MyData {
/// pub student_debt: i32,
/// }
///
/// // Making our data and setting its default values
/// let data = MyData {
/// student_debt: 20
/// };
/// // ..
/// ```
/// Implementing [`Serialize`] and [`Deserialize`] yourself is quite complicated but will provide the most flexibility.
///
/// *If you wish to implement them yourself I'd recommend reading the Serde docs on it*
///
pub struct Config<D>
where
for<'a> D: Deserialize<'a> + Serialize,
{
pub data: D,
pub path: PathBuf,
pub options: InternalOptions,
}
impl<D> Config<D>
where
for<'a> D: Deserialize<'a> + Serialize,
{
/// Constructs and returns a new config object using the default options.
///
/// If there is a file at `path`, the file will be opened. <br/>
///
/// - `path`: Takes in a path to where the config file is or should be located.
/// If the file has no extension, the crate will attempt to guess the extension from one available format `feature`.
///
/// - `data`: Takes in a struct that inherits [`Serialize`] and [`Deserialize`]
/// You have to make this struct yourself, construct it, and pass it in.
/// More info about it is provided at [`Config`].
///
/// If you'd like to configure this object, you should take a look at using [`Config::from_options`] instead.
pub fn new(path: impl AsRef<Path>, data: D) -> Result<Config<D>, error::ConfigError> {
Self::construct(path, ConfigSetupOptions::default(), data)
}
/// Constructs and returns a new config object from a set of custom options.
///
/// - `path`: Takes in a path to where the config file is or should be located. <br/>
/// If the file has no extension, and there is no `format` selected in your `options`,
/// the crate will attempt to guess the extension from one available format `feature`s.
//
/// - `options`: Takes in a [`ConfigSetupOptions`],
/// used to configure the format language, styling of the data, and other things. <br/>
/// Remember to add `..` [`Default::default()`] at the end of your `options` as more options are
/// going to be added to the crate later on.
///
/// - `data`: Takes in a struct that inherits [`Serialize`] and [`Deserialize`]
/// You have to make this struct yourself, construct it, and pass it in.
/// More info is provided at [`Config`].
pub fn from_options(
path: impl AsRef<Path>,
options: ConfigSetupOptions,
data: D,
) -> Result<Config<D>, error::ConfigError> {
Self::construct(path, options, data)
}
// Main, private constructor
fn construct(
path: impl AsRef<Path>,
mut options: ConfigSetupOptions,
mut data: D,
) -> Result<Config<D>, error::ConfigError> {
let mut path = PathBuf::from(path.as_ref());
// Setting up variables
let enabled_features = format_dependant::get_enabled_features();
let first_enabled_feature = format_dependant::get_first_enabled_feature();
let guess_from_feature = || {
if enabled_features.len() > 1 {
Err(error::ConfigError::UnknownFormat(
error::UnknownFormatError::new(None, enabled_features.clone()),
))
} else {
Ok(Some(first_enabled_feature))
}
};
// Manual format option > file extension > guessed feature
if options.format.is_none() {
options.format = match path.extension() {
Some(extension) => {
// - Based on the extension
match ConfigFormat::from_extension(extension) {
Some(value) => Some(value),
None => guess_from_feature()?,
}
}
_ => {
// - Guessing based on the enabled features
guess_from_feature()?
}
};
}
// Converting the user options into a more convenient internally-used type
let options: InternalOptions = match InternalOptions::try_from(options) {
Ok(value) => value,
Err(message) => {
return Err(error::ConfigError::UnknownFormat(
error::UnknownFormatError::new(Some(message), enabled_features),
));
}
};
// Setting the file format
if path.extension().is_none() {
path.set_extension(options.format.to_string());
}
// Reading from the file if a file was found
if let Ok(mut file) = fs::File::open(&path) {
let mut content = String::new();
if let Err(err) = file.read_to_string(&mut content) {
return Err(error::ConfigError::InvalidFileEncoding(err, path));
};
// Deserialization
// (Getting data from a string)
if let Ok(value) = format_dependant::from_string(&content, &options.format) {
data = value;
} else {
return Err(error::ConfigError::DataParseError(
error::DataParseError::Deserialize(options.format, content),
));
};
}
// Returning the Config object
Ok(Self {
data,
path,
options,
})
}
/// Saves the config file to the disk.
///
/// It uses the [`Config`]'s object own internal `path` property to get the path required to save the file
/// so there is no need to pass in the path to save it at.
///
/// If you wish to specify the path to save it at
/// you can change the path yourself by setting the Config's `path` property.
/// <br/> <br/>
/// ## save_at method
/// There used to be a built-in function called `save_at` while i was developing the crate,
/// but I ended up removing it due to the fact i didn't see anyone actually using it,
/// and it might've ended up in some users getting confused, as well as a tiny bit of performance overhead.
///
/// If you'd like this feature to be back feel free to open an issue and I'll add it back right away!
pub fn save(&self) -> Result<(), error::ConfigSaveError> {
let to_string = format_dependant::to_string(&self.data, &self.options);
match to_string {
// If the conversion was successful
Ok(data) => {
if let Some(parent_dir) = self.path.parent() {
fs::create_dir_all(parent_dir)?;
};
let mut file = fs::File::create(&self.path)?;
write!(file, "{data}")?;
}
// If the conversion failed
Err(e) => {
// This error triggering sometimes seems to mean a data type you're using in your
// custom data struct isn't supported, but I haven't fully tested it.
return Err(error::ConfigSaveError::SerializationError(e));
}
};
Ok(())
}
/// Gets the name of the config file
pub fn filename(&self) -> String {
self.path.file_name().unwrap().to_string_lossy().to_string()
}
}
impl<D> Drop for Config<D>
where
for<'a> D: Deserialize<'a> + Serialize,
{
fn drop(&mut self) {
if self.options.save_on_drop {
let _ = self.save();
}
}
}