use std::path::Path;
use anyhow::anyhow;
use serde::Serialize;
use crate::functions::extension;
pub trait ToFile: Serialize + Sized {
fn write_to_file(&self, filename: impl AsRef<Path>) -> anyhow::Result<()> {
match extension(filename.as_ref())? {
#[cfg(feature = "json")]
"json" => <Self as crate::ToJson>::write_to_json_file(self, filename),
#[cfg(feature = "toml")]
"toml" => <Self as crate::ToToml>::write_to_toml_file(self, filename),
#[cfg(feature = "xml")]
"xml" => <Self as crate::ToXml>::write_to_xml_file(self, filename),
#[cfg(feature = "yaml")]
"yml" | "yaml" => <Self as crate::ToYaml>::write_to_yaml_file(self, filename),
extension => Err(anyhow!("Unsupported extension: '{extension}'")),
}
}
fn write_to_file_pretty(&self, filename: impl AsRef<Path>) -> anyhow::Result<()> {
match extension(filename.as_ref())? {
#[cfg(feature = "json")]
"json" => <Self as crate::ToJson>::write_to_json_file_pretty(self, filename),
#[cfg(feature = "xml")]
"xml" => <Self as crate::ToXml>::write_to_xml_file_pretty(self, filename, ' ', 4),
extension => Err(anyhow!("Unsupported extension: '{extension}'")),
}
}
}
impl<T> ToFile for T where T: Serialize {}
#[cfg(feature = "json")]
impl<T> crate::ToJson for T where T: ToFile {}
#[cfg(feature = "toml")]
impl<T> crate::ToToml for T where T: ToFile {}
#[cfg(feature = "xml")]
impl<T> crate::ToXml for T where T: ToFile {}
#[cfg(feature = "yaml")]
impl<T> crate::ToYaml for T where T: ToFile {}