use std::ffi::OsStr;
use std::path::Path;
use serde::Deserialize;
use crate::Error;
pub trait FromFile
where
for<'de> Self: Deserialize<'de>,
{
fn from_file(filename: impl AsRef<Path>) -> crate::Result<Self> {
let extension = filename
.as_ref()
.extension()
.map(OsStr::to_ascii_lowercase)
.ok_or(Error::NoFileExtensionsSpecified)?;
match extension.as_encoded_bytes() {
#[cfg(feature = "json")]
b"json" => <Self as crate::FromJson>::from_json_file(filename),
#[cfg(feature = "toml")]
b"toml" => <Self as crate::FromToml>::from_toml_file(filename),
#[cfg(feature = "xml")]
b"xml" => <Self as crate::FromXml>::from_xml_file(filename),
#[cfg(feature = "yaml")]
b"yml" | b"yaml" => <Self as crate::FromYaml>::from_yaml_file(filename),
_ => Err(Error::UnsupportedFileExtension(extension)),
}
}
}
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 {}