use crate::error::{Error, Result};
use serde::de::DeserializeOwned;
pub trait FromYaml: DeserializeOwned + Default {
fn from_yml(text: &str) -> Result<Self> {
if text.trim_start().is_empty() {
return Ok(Self::default());
}
match serde_yaml::from_str(text) {
Ok(config) => Ok(config),
Err(e) => {
if format!("{:?}", e) == "EndOfStream" {
Ok(Self::default())
} else {
Err(error!("Invalid YAML: {}", e))
}
}
}
}
}
pub trait FromStr<'a> {
fn from_str(text: &'a str) -> Result<Self>
where
Self: std::marker::Sized;
}
pub trait IntoStr {
#[allow(clippy::wrong_self_convention)]
fn into_str(&self) -> &str;
}
pub trait FromMessagePack: DeserializeOwned {
fn from_mp(bytes: &[u8]) -> Result<Self> {
match rmp_serde::from_slice(bytes) {
Ok(r) => Ok(r),
Err(e) => Err(error!("{}", e)),
}
}
}