use crate::error::YamlError;
use crate::{BufferSource, Node, parse};
pub fn parse_yaml(input: impl AsRef<[u8]>) -> Node {
let config = crate::parser::config::ParserConfig::strict();
crate::parse_with_config(
std::str::from_utf8(input.as_ref()).expect("Invalid UTF-8"),
config,
)
.expect("YAML parse failed")
}
pub fn assert_nodes_eq(expected: &Node, actual: &Node) {
assert_eq!(
expected, actual,
"Nodes are not equal.\nExpected: {:#?}\nActual: {:#?}",
expected, actual
);
}
pub fn assert_parse_error(input: impl AsRef<[u8]>, expected_msg: &str) {
let config = crate::parser::config::ParserConfig::strict();
let result = crate::parse_with_config(
std::str::from_utf8(input.as_ref()).expect("Invalid UTF-8"),
config,
);
assert!(result.is_err(), "Expected parse error, but got Ok");
let err = result.unwrap_err();
let err_str = err.to_string();
assert!(
err_str.contains(expected_msg),
"Error message did not contain expected substring.\nExpected: {}\nActual: {}",
expected_msg,
err_str
);
}
#[cfg(feature = "stringify")]
pub fn node_to_yaml_string(node: &Node) -> String {
use crate::BufferDestination;
use crate::stringify;
let mut buf = BufferDestination::new();
stringify(node, &mut buf).expect("Stringify failed");
buf.to_string()
}
#[cfg(feature = "stringify")]
pub fn roundtrip_node(node: &Node) -> Result<Node, YamlError> {
use crate::BufferDestination;
use crate::stringify;
let mut buf = BufferDestination::new();
stringify(node, &mut buf)?;
let yaml = buf.to_string();
let mut source = BufferSource::new(yaml.as_bytes());
parse(&mut source)
}
#[cfg(feature = "stringify")]
pub fn assert_roundtrip_eq(node: &Node) {
match roundtrip_node(node) {
Ok(roundtripped) => {
assert_nodes_eq(node, &roundtripped);
}
Err(err) => {
panic!("Round-trip failed: {}", err);
}
}
}