config: Make YAML/JSON modular and optional (#21)
This commit is contained in:
committed by
Kenny Levinsen
parent
c02937d8b6
commit
c695288d1e
@@ -0,0 +1,61 @@
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
use std::io::BufRead;
|
||||
|
||||
pub enum ConfigFmt {
|
||||
#[cfg(feature = "yaml-cfg")]
|
||||
Yaml,
|
||||
#[cfg(feature = "json-cfg")]
|
||||
Json,
|
||||
}
|
||||
|
||||
pub const CONFIG_NAMES: &'static [&'static str] = &[
|
||||
#[cfg(feature = "yaml-cfg")]
|
||||
"config.yaml",
|
||||
#[cfg(feature = "json-cfg")]
|
||||
"config.json",
|
||||
];
|
||||
|
||||
impl ConfigFmt {
|
||||
#[inline]
|
||||
pub fn new(ext: &str) -> Option<Self> {
|
||||
match ext {
|
||||
#[cfg(feature = "yaml-cfg")]
|
||||
"yaml" => Some(ConfigFmt::Yaml),
|
||||
#[cfg(feature = "json-cfg")]
|
||||
"json" => Some(ConfigFmt::Json),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
pub fn from_reader<B: BufRead, T: DeserializeOwned>(&self, r: B) -> T {
|
||||
match self {
|
||||
#[cfg(feature = "yaml-cfg")]
|
||||
ConfigFmt::Yaml => serde_yaml::from_reader(r).unwrap(),
|
||||
#[cfg(feature = "json-cfg")]
|
||||
ConfigFmt::Json => serde_json::from_reader(r).unwrap(),
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
pub fn to_string<T: Serialize + ?Sized>(&self, src: &T) -> String {
|
||||
match self {
|
||||
#[cfg(feature = "yaml-cfg")]
|
||||
ConfigFmt::Yaml => serde_yaml::to_string(&src).unwrap(),
|
||||
#[cfg(feature = "json-cfg")]
|
||||
ConfigFmt::Json => serde_json::to_string_pretty(&src).unwrap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ConfigFmt {
|
||||
fn default() -> Self {
|
||||
// attributes require curly braces to work
|
||||
#[cfg(feature = "yaml-cfg")]
|
||||
{
|
||||
ConfigFmt::Yaml
|
||||
}
|
||||
#[cfg(all(feature = "json-cfg", not(feature = "yaml-cfg")))]
|
||||
{
|
||||
ConfigFmt::Json
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user