use std::fmt;
use serde::Serialize;
use serde::de::DeserializeOwned;
use serde_saphyr::options::MergeKeyPolicy;
#[derive(Debug)]
pub struct Error(Inner);
#[derive(Debug)]
enum Inner {
Read(serde_saphyr::Error),
Write(serde_saphyr::ser::Error),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.0 {
Inner::Read(e) => e.fmt(f),
Inner::Write(e) => e.fmt(f),
}
}
}
impl std::error::Error for Error {}
pub fn from_str<T: DeserializeOwned>(yaml: &str) -> Result<T, Error> {
let options = serde_saphyr::options! {
strict_booleans: true,
merge_keys: MergeKeyPolicy::Error,
reject_unsupported_tags: true,
ignore_binary_tag_for_string: true,
with_snippet: false,
};
serde_saphyr::from_str_with_options(yaml, options).map_err(|e| Error(Inner::Read(e)))
}
pub fn to_string<T: Serialize>(value: &T) -> Result<String, Error> {
serde_saphyr::to_string(value).map_err(|e| Error(Inner::Write(e)))
}
#[cfg(test)]
mod tests;