use std::borrow::Cow;
use std::fmt::Debug;
use ctl_input::Limits;
use ctl_input::de::deserialize;
use ctl_input::format::{Format, Reading, Refusal, Resolved};
use ctl_input::tree::{Node, NodeKind};
use serde::de::DeserializeOwned;
pub(crate) fn read(format: &dyn Format, text: &str) -> Node {
format
.read(text, &Limits::NONE, Reading::Strict)
.unwrap_or_else(|refusal| panic!("{refusal}"))
}
pub(crate) fn borrowed(text: &str) -> Resolved<'_> {
Resolved::Str(Cow::Borrowed(text))
}
pub(crate) fn source<'a>(text: &'a str, node: &Node) -> &'a str {
&text[node.span()]
}
pub(crate) fn entries(node: &Node) -> &[(Node, Node)] {
let NodeKind::Mapping(entries) = node.kind() else {
panic!("{:?} is not a mapping", node.kind());
};
entries
}
pub(crate) fn items(node: &Node) -> &[Node] {
let NodeKind::Sequence(items) = node.kind() else {
panic!("{:?} is not a sequence", node.kind());
};
items
}
pub(crate) fn value(node: &Node) -> &str {
let NodeKind::Scalar(scalar) = node.kind() else {
panic!("{:?} is not a scalar", node.kind());
};
scalar.value()
}
pub(crate) fn get<'a>(mapping: &'a Node, key: &str) -> &'a Node {
entries(mapping)
.iter()
.find(|(name, _)| value(name) == key)
.map_or_else(|| panic!("no key {key}"), |(_, found)| found)
}
pub(crate) fn typed<T: DeserializeOwned>(
format: &'static dyn Format,
text: &str,
) -> Result<T, Refusal> {
let tree = format.read(text, &Limits::NONE, Reading::Strict)?;
deserialize(&tree, format)
}
pub(crate) fn refused<T: DeserializeOwned + Debug>(
format: &'static dyn Format,
text: &str,
) -> Refusal {
match typed::<T>(format, text) {
Ok(value) => panic!("read {value:?} from {text:?}"),
Err(refusal) => refusal,
}
}
pub(crate) fn at<'a>(text: &'a str, refusal: &Refusal) -> &'a str {
let span = refusal
.span()
.unwrap_or_else(|| panic!("{refusal} has no span"));
&text[span]
}