use serde::{Deserialize, Serialize};
use std::fmt;
use std::fmt::Display;
use std::path::Path;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ConfigFormat {
#[cfg(feature = "json")]
Json,
#[cfg(feature = "json5")]
Json5,
#[cfg(feature = "toml")]
Toml,
}
impl ConfigFormat {
pub fn guess_from_path(path: impl AsRef<Path>) -> Option<Self> {
match path.as_ref().extension()?.to_str()? {
#[cfg(feature = "json")]
"json" => Some(Self::Json),
#[cfg(feature = "json5")]
"json5" => Some(Self::Json5),
#[cfg(feature = "toml")]
"toml" => Some(Self::Toml),
_ => None,
}
}
}
impl Display for ConfigFormat {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
#[cfg(feature = "json")]
Self::Json => f.write_str("json"),
#[cfg(feature = "json5")]
Self::Json5 => f.write_str("json5"),
#[cfg(feature = "toml")]
Self::Toml => f.write_str("toml"),
}
}
}
#[cfg(all(test, feature = "json"))]
#[test]
fn test_path_format_guessing_json() {
assert_eq!(
ConfigFormat::guess_from_path("test-data/test.json"),
Some(ConfigFormat::Json)
);
assert_eq!(
ConfigFormat::guess_from_path("test-data/.test.json"),
Some(ConfigFormat::Json)
);
assert_eq!(
ConfigFormat::guess_from_path("test-data/.test.foo.json"),
Some(ConfigFormat::Json)
);
}
#[cfg(all(test, feature = "json5"))]
#[test]
fn test_path_format_guessing_json5() {
assert_eq!(
ConfigFormat::guess_from_path("test-data/test.json5"),
Some(ConfigFormat::Json5)
);
}
#[cfg(all(test, feature = "toml"))]
#[test]
fn test_path_format_guessing_toml() {
assert_eq!(
ConfigFormat::guess_from_path("test-data/test.toml"),
Some(ConfigFormat::Toml)
);
}
#[cfg(test)]
#[test]
fn test_path_format_guessing_none() {
assert_eq!(ConfigFormat::guess_from_path("test-data/test.txt"), None);
assert_eq!(ConfigFormat::guess_from_path("test-data/test"), None);
assert_eq!(ConfigFormat::guess_from_path("test-data/"), None);
}