pub mod json;
pub mod toml;
pub mod yaml;
#[cfg(feature = "ini")]
pub mod ini;
#[cfg(feature = "xml")]
pub mod xml;
use crate::error::Result;
use crate::value::ConfigValue;
use std::path::Path;
pub trait Formatter: Send + Sync + 'static {
fn provides(&self, identifier: &str) -> bool;
fn extensions(&self) -> &[&str];
fn deserialize(&self, content: &str) -> Result<ConfigValue>;
fn serialize(&self, value: &ConfigValue) -> Result<String>;
fn name(&self) -> &str;
}
pub fn extension_matches(identifier: &str, extensions: &[&str]) -> bool {
let path = Path::new(identifier);
let ext = match path.extension().and_then(|e| e.to_str()) {
Some(e) => e,
None => return false,
};
extensions.contains(&ext)
}
pub(crate) fn escape_quotes(s: &str) -> String {
s.replace('\\', "\\\\").replace('"', "\\\"")
}
pub fn hint_matches(hint: &str, extensions: &[&str]) -> bool {
extensions.contains(&hint)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_extension_matches() {
assert!(extension_matches("config.json", &["json", "json5"]));
assert!(extension_matches("config.json5", &["json", "json5"]));
assert!(!extension_matches("config.toml", &["json", "json5"]));
assert!(!extension_matches("no_extension", &["json"]));
}
#[test]
fn test_hint_matches() {
assert!(hint_matches("json", &["json", "json5", "jsonc"]));
assert!(hint_matches("toml", &["toml"]));
assert!(!hint_matches("bson", &["json", "toml"]));
assert!(!hint_matches("", &["json"]));
}
}