use std::borrow::Cow;
use std::ops::Range;
use std::sync::OnceLock;
use serde_saphyr::granit_parser::ScalarStyle as Written;
use serde_saphyr::located::{self, MappingNode, ScalarNode, SequenceNode};
use serde_saphyr::scalars::{Inferred, Rules, Scalar as Spelled};
use serde_saphyr::{
DefaultMessageFormatter, DuplicateKeyPolicy, Error, Localizer, Location, MergeKeyPolicy,
MessageFormatter, NonFiniteFloatPolicy, Options,
};
use super::exceeded::Exceeded;
use super::format::{Fault, Format, Mismatch, Raise, Reading, Refusal, Resolved, Wanted};
use super::limits::Limits;
use super::messages::printable;
use super::tree::{Node, Scalar, ScalarStyle, Tag};
#[derive(Clone, Copy, Debug)]
pub struct Yaml;
pub static YAML: Yaml = Yaml;
impl Format for Yaml {
fn name(&self) -> &'static str {
"yaml"
}
fn extensions(&self) -> &'static [&'static str] {
&["yaml", "yml"]
}
fn read(&self, text: &str, limits: &Limits, reading: Reading) -> Result<Node, Refusal> {
let bounds = limits.bounds();
if text.len() > bounds.input_bytes {
let exceeded = Exceeded::input_bytes(bounds.input_bytes, Raise::FOR_TEXT);
return Err(Refusal::new(exceeded.fault(), None, None));
}
located::read(text, options(limits, reading))
.map(|node| convert(text, &node))
.map_err(|error| refusal(text, limits, &error))
}
fn resolve<'a>(
&self,
scalar: &'a Scalar,
tag: Option<&Tag>,
wanted: Wanted,
) -> Result<Resolved<'a>, Mismatch> {
let spelled = Spelled {
value: scalar.value(),
style: spelled_style(scalar.style()),
tag: tag.map(Tag::as_str),
};
let rules = rules();
let at = Location::UNKNOWN;
let mismatch = |error: Error| {
let detail = if let Error::NonFiniteFloat { value, .. } = &error {
printable(&format!("non-finite float `{value}` is not allowed"))
} else {
plain(&DefaultMessageFormatter.with_localizer(&Unplaced), &error)
};
Mismatch::new(wanted).with_detail(detail)
};
match wanted {
Wanted::Any => match rules.infer(&spelled, at).map_err(mismatch)? {
Inferred::Null => Ok(Resolved::Null),
Inferred::Bool(value) => Ok(Resolved::Bool(value)),
Inferred::Signed(value) => Ok(Resolved::Int(i128::from(value))),
Inferred::Unsigned(value) => Ok(Resolved::Int(i128::from(value))),
Inferred::Float(value) => Ok(Resolved::Float(value)),
Inferred::String(Cow::Owned(text)) => Ok(Resolved::Str(Cow::Owned(text))),
Inferred::String(Cow::Borrowed(_)) => {
Ok(Resolved::Str(Cow::Borrowed(scalar.value())))
}
},
Wanted::Bool => rules
.boolean(&spelled, at)
.map(Resolved::Bool)
.map_err(mismatch),
Wanted::Int => match rules.signed::<i128>(&spelled, at) {
Ok(value) => Ok(Resolved::Int(value)),
Err(_) => rules
.unsigned::<u128>(&spelled, at)
.map(Resolved::Uint)
.map_err(mismatch),
},
Wanted::Float => rules
.float64(&spelled, at)
.map(Resolved::Float)
.map_err(mismatch),
Wanted::Null => rules
.is_null(&spelled)
.then_some(Resolved::Null)
.ok_or_else(|| Mismatch::new(wanted)),
Wanted::Str => match rules.string(&spelled, at).map_err(mismatch)? {
Cow::Owned(text) => Ok(Resolved::Str(Cow::Owned(text))),
Cow::Borrowed(_) => Ok(Resolved::Str(Cow::Borrowed(scalar.value()))),
},
}
}
}
fn rules() -> &'static Rules {
static RULES: OnceLock<Rules> = OnceLock::new();
RULES.get_or_init(|| Rules::new(&options(&Limits::NONE, Reading::Strict)))
}
fn spelled_style(style: ScalarStyle) -> Written {
match style {
ScalarStyle::Plain => Written::Plain,
ScalarStyle::SingleQuoted | ScalarStyle::MultilineLiteral => Written::SingleQuoted,
ScalarStyle::DoubleQuoted | ScalarStyle::MultilineBasic => Written::DoubleQuoted,
ScalarStyle::Literal => Written::Literal,
ScalarStyle::Folded => Written::Folded,
}
}
fn style(written: Written) -> ScalarStyle {
match written {
Written::Plain => ScalarStyle::Plain,
Written::SingleQuoted => ScalarStyle::SingleQuoted,
Written::DoubleQuoted => ScalarStyle::DoubleQuoted,
Written::Literal => ScalarStyle::Literal,
Written::Folded => ScalarStyle::Folded,
}
}
fn bytes(text: &str, location: Location) -> Option<Range<usize>> {
let span = location.span();
if let (Some(start), Some(length)) = (span.byte_offset(), span.byte_len()) {
let start = usize::try_from(start).ok()?;
let length = usize::try_from(length).ok()?;
return Some(start..start.saturating_add(length));
}
let offset = offset_of(text, location)?;
Some(offset..offset)
}
fn offset_of(text: &str, location: Location) -> Option<usize> {
let line = usize::try_from(location.line()).ok()?.checked_sub(1)?;
let column = usize::try_from(location.column()).ok()?.checked_sub(1)?;
let start: usize = text.split_inclusive('\n').take(line).map(str::len).sum();
let content = text.get(start..)?.split('\n').next()?;
let (bom, counted) = match content.strip_prefix('\u{feff}') {
Some(rest) if start == 0 => (content.len() - rest.len(), rest),
_ => (0, content),
};
let within = counted
.char_indices()
.nth(column)
.map_or(counted.len(), |(index, _)| index);
Some(start + bom + within)
}
fn placed(text: &str, location: Location) -> Range<usize> {
bytes(text, location).unwrap_or_default()
}
fn convert(text: &str, node: &located::Node) -> Node {
let locations = node.locations();
let span = placed(text, locations.defined_location);
let built = match node {
located::Node::Scalar(ScalarNode {
value,
style: written,
..
}) => Node::scalar(span, Scalar::new(value.as_str(), style(*written))),
located::Node::Sequence(SequenceNode { items, .. }) => {
Node::sequence(span, items.iter().map(|item| convert(text, item)).collect())
}
located::Node::Mapping(MappingNode { entries, .. }) => Node::mapping(
span,
entries
.iter()
.map(|(key, value)| (convert(text, key), convert(text, value)))
.collect(),
),
};
let built = match node.tag() {
Some(tag) => built.with_tag(Tag::new(tag)),
None => built,
};
if locations.reference_location == locations.defined_location {
built
} else {
built.with_used_at(placed(text, locations.reference_location))
}
}
fn refusal(text: &str, limits: &Limits, error: &Error) -> Refusal {
let mut error = error;
while let Error::WithSnippet { error: inner, .. } = error {
error = inner;
}
let Error::AliasError {
error: inner,
locations,
..
} = error
else {
let span = error
.location()
.filter(|at| *at != Location::UNKNOWN)
.and_then(|at| bytes(text, at));
return Refusal::new(fault(text, limits, error), span, None);
};
let mut failing = inner.as_ref();
while let Error::AliasError { error, .. } | Error::WithSnippet { error, .. } = failing {
failing = error;
}
let defined = failing
.location()
.filter(|at| *at != Location::UNKNOWN)
.unwrap_or(locations.defined_location);
Refusal::new(
fault(text, limits, failing),
bytes(text, locations.reference_location),
bytes(text, defined),
)
}
#[expect(
clippy::wildcard_enum_match_arm,
reason = "Error is non_exhaustive: any other error is a syntax fault with serde-saphyr's wording"
)]
fn fault(text: &str, limits: &Limits, error: &Error) -> Fault {
match error {
Error::DuplicateMappingKey { key, location } => Fault::DuplicateKey {
key: key.clone().unwrap_or_else(|| {
bytes(text, *location)
.and_then(|range| text.get(range))
.unwrap_or_default()
.to_owned()
}),
},
Error::MergeKeyNotAllowed { .. } => Fault::MergeKey,
Error::MultipleDocuments { .. } => Fault::Syntax {
detail: "the text holds more than one YAML document".to_owned(),
},
_ => Exceeded::of(
error,
&limits.budget(),
&limits.alias_limits(),
Raise::FOR_TEXT,
)
.map_or_else(
|| Fault::Syntax {
detail: plain(&DefaultMessageFormatter.with_localizer(&Unplaced), error),
},
|exceeded| exceeded.fault(),
),
}
}
fn options(limits: &Limits, reading: Reading) -> Options {
let (duplicates, merges, floats) = match reading {
Reading::Strict => (
DuplicateKeyPolicy::Error,
MergeKeyPolicy::Error,
NonFiniteFloatPolicy::Default,
),
Reading::Listing => (
DuplicateKeyPolicy::PassThrough,
MergeKeyPolicy::AsOrdinary,
NonFiniteFloatPolicy::PassThrough,
),
};
serde_saphyr::options! {
budget: Some(limits.budget()),
alias_limits: limits.alias_limits(),
strict_booleans: true,
duplicate_keys: duplicates,
merge_keys: merges,
non_finite_float_policy: floats,
with_snippet: false,
}
}
fn plain(formatter: &dyn MessageFormatter, mut error: &Error) -> String {
while let Error::AliasError { error: inner, .. } | Error::WithSnippet { error: inner, .. } =
error
{
error = inner;
}
printable(&formatter.format_message(error))
}
struct Unplaced;
impl Localizer for Unplaced {
fn attach_location<'a>(&self, base: Cow<'a, str>, _: Location) -> Cow<'a, str> {
base
}
fn alias_used_at(&self, _: Location) -> String {
String::new()
}
}