use crate::parser::Parser;
use anyhow::anyhow;
use cfg_if::cfg_if;
use plugx_input::Input;
use std::fmt::{Debug, Display, Formatter};
#[derive(Default, Debug, Copy, Clone)]
pub struct Yaml;
impl Display for Yaml {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str("YAML")
}
}
impl Yaml {
pub fn new() -> Self {
Default::default()
}
}
impl Parser for Yaml {
fn supported_format_list(&self) -> Vec<String> {
["yml".into(), "yaml".into()].into()
}
fn try_parse(&self, bytes: &[u8]) -> anyhow::Result<Input> {
serde_yaml::from_slice(bytes)
.map(|parsed: Input| {
cfg_if! {
if #[cfg(feature = "tracing")] {
tracing::trace!(
input=String::from_utf8_lossy(bytes).to_string(),
output=%parsed,
"Parsed YAML contents"
);
} else if #[cfg(feature = "logging")] {
log::trace!(
"msg=\"Parsed YAML contents\" input={:?} output={:?}",
String::from_utf8_lossy(bytes).to_string(),
parsed.to_string()
);
}
}
parsed
})
.map_err(|error| anyhow!(error))
}
fn is_format_supported(&self, bytes: &[u8]) -> Option<bool> {
Some(serde_yaml::from_slice::<serde_yaml::Value>(bytes).is_ok())
}
}