use serde::{de::DeserializeOwned, Deserializer};
pub fn default_as_true() -> bool {
true
}
static ERROR_LOCATION_DESCRIPTION_LABEL: &str = "error occurred when expecting";
pub fn deserialize_enum_helper<'de, D, T>(
expected: &str,
yaml: serde_yaml::Value,
) -> Result<T, D::Error>
where
D: Deserializer<'de>,
T: DeserializeOwned,
{
use serde::de::Error;
match serde_yaml::from_value::<T>(yaml.clone()) {
Ok(val) => Ok(val),
Err(err) => {
let mut indented_source = " ".to_string();
indented_source.push_str(
&serde_yaml::to_string(&yaml)
.expect("error serializing YAML for error")
.replace('\n', "\n "),
);
indented_source = indented_source.trim_end().to_owned();
indented_source.push_str("\n ---");
let old_msg = format!("{err}");
let new_msg = if old_msg.contains(ERROR_LOCATION_DESCRIPTION_LABEL) {
if old_msg.contains(&indented_source) {
old_msg
} else {
format!("{old_msg}\n...which appeared when expecting {expected} in:\n{indented_source}")
}
} else {
format!(
"{old_msg}\n{ERROR_LOCATION_DESCRIPTION_LABEL} {expected} in:\n{indented_source}"
)
};
Err(D::Error::custom(new_msg))
}
}
}