use crate::functions::extension;
use crate::Error;
use serde::Deserialize;
use std::path::Path;
pub trait FromFile
where
for<'de> Self: Deserialize<'de>,
{
fn from_file(filename: impl AsRef<Path>) -> Result<Self, Error> {
match extension(filename.as_ref())? {
#[cfg(feature = "json")]
"json" => <Self as crate::FromJson>::from_json_file(filename),
#[cfg(feature = "toml")]
"toml" => <Self as crate::FromToml>::from_toml_file(filename),
#[cfg(feature = "xml")]
"xml" => <Self as crate::FromXml>::from_xml_file(filename),
#[cfg(feature = "yaml")]
"yml" | "yaml" => <Self as crate::FromYaml>::from_yaml_file(filename),
extension => Err(Error::InvalidExtension(Some(extension.to_string()))),
}
}
}
impl<T> FromFile for T where T: for<'de> Deserialize<'de> {}
#[cfg(feature = "json")]
impl<T> crate::FromJson for T where T: FromFile {}
#[cfg(feature = "toml")]
impl<T> crate::FromToml for T where T: FromFile {}
#[cfg(feature = "xml")]
impl<T> crate::FromXml for T where T: FromFile {}
#[cfg(feature = "yaml")]
impl<T> crate::FromYaml for T where T: FromFile {}