use serde::{Deserialize, Serialize};
use sysexits::{ExitCode, Result};
pub trait FromMd: Sized {
fn from_md(md: &str) -> Result<Self>;
}
pub trait FromRon<'a>: Deserialize<'a> {
fn from_ron(ron: &'a str) -> Result<Self>;
}
impl<'a, T: Deserialize<'a>> FromRon<'a> for T {
fn from_ron(ron: &'a str) -> Result<Self> {
ron::de::from_str(ron).map_or(Err(ExitCode::DataErr), Ok)
}
}
pub trait FromRst: Sized {
fn from_rst(rst: &str) -> Result<Self>;
}
pub trait FromXml<'a>: Deserialize<'a> {
fn from_xml(xml: &'a str) -> Result<Self>;
}
impl<'a, T: Deserialize<'a>> FromXml<'a> for T {
fn from_xml(xml: &'a str) -> Result<Self> {
quick_xml::de::from_str(xml).map_or(Err(ExitCode::DataErr), Ok)
}
}
pub trait ToMd: Sized {
fn to_md(&self, header_level: u8) -> Result<String>;
}
pub trait ToRon: Serialize {
fn to_ron(&self, indentation_width: usize) -> Result<String>;
}
impl<T: Serialize> ToRon for T {
fn to_ron(&self, indentation_width: usize) -> Result<String> {
ron::ser::to_string_pretty(
self,
ron::ser::PrettyConfig::default()
.indentor(" ".repeat(indentation_width)),
)
.map_or(Err(ExitCode::DataErr), |mut s| {
s.push('\n');
Ok(s)
})
}
}
pub trait ToRst: Sized {
fn to_rst(&self, header_level: u8) -> Result<String>;
}
pub trait ToXml: Serialize {
fn to_xml(&self) -> Result<String>;
}
impl<T: Serialize> ToXml for T {
fn to_xml(&self) -> Result<String> {
quick_xml::se::to_string(&self).map_or(Err(ExitCode::DataErr), |s| {
let mut result = String::from("<?xml version=\"1.0\"?>\n");
result.push_str(&s);
result.push('\n');
Ok(result)
})
}
}