use std::collections::HashMap;
use serde_json::{Map, Value};
use crate::js::{js_trim, num};
use crate::pattern::Pattern;
use crate::types::KeyOrder;
use std::sync::LazyLock;
pub type LoadError = String;
const YAML_TAG: &str = "tag:yaml.org,2002:";
const MAX_DEPTH: usize = 256;
pub(crate) const SECOND_DOCUMENT: &str = "a second YAML document (a `---` line) is not allowed here";
struct Core {
null: Pattern,
boolean: Pattern,
int_oct: Pattern,
int: Pattern,
int_hex: Pattern,
float_nan: Pattern,
float_exp: Pattern,
float: Pattern,
}
static CORE: LazyLock<Core> = LazyLock::new(|| Core {
null: Pattern::must(r"^(?:~|[Nn]ull|NULL)?$"),
boolean: Pattern::must(r"^(?:[Tt]rue|TRUE|[Ff]alse|FALSE)$"),
int_oct: Pattern::must(r"^0o[0-7]+$"),
int: Pattern::must(r"^[-+]?[0-9]+$"),
int_hex: Pattern::must(r"^0x[0-9a-fA-F]+$"),
float_nan: Pattern::must(r"^(?:[-+]?\.(?:inf|Inf|INF)|\.nan|\.NaN|\.NAN)$"),
float_exp: Pattern::must(r"^[-+]?(?:\.[0-9]+|[0-9]+(?:\.[0-9]*)?)[eE][-+]?[0-9]+$"),
float: Pattern::must(r"^[-+]?(?:\.[0-9]+|[0-9]+\.[0-9]*)$"),
});
#[derive(Clone, Copy, Debug, PartialEq)]
enum Style {
Plain,
Quoted,
Block,
}
#[derive(Debug)]
enum Kind {
Scalar(String, Style),
Seq(Vec<YNode>),
Map(Vec<(YNode, YNode)>),
Alias,
}
#[derive(Debug)]
struct YNode {
kind: Kind,
tag: Option<String>,
anchor: bool,
}
impl YNode {
fn scalar(s: String, style: Style) -> YNode {
YNode { kind: Kind::Scalar(s, style), tag: None, anchor: false }
}
fn empty() -> YNode {
YNode::scalar(String::new(), Style::Plain)
}
}
#[derive(Clone, Debug)]
enum Scalar {
Null,
Bool(bool),
Num(f64),
Str(String),
}
impl Scalar {
fn same(&self, other: &Scalar) -> bool {
match (self, other) {
(Scalar::Null, Scalar::Null) => true,
(Scalar::Bool(a), Scalar::Bool(b)) => a == b,
(Scalar::Num(a), Scalar::Num(b)) => a == b,
(Scalar::Str(a), Scalar::Str(b)) => a == b,
_ => false,
}
}
}
pub(crate) fn load_yaml(text: &str) -> Result<(Value, KeyOrder), LoadError> {
if yaml_documents(text) > 1 {
return Err(SECOND_DOCUMENT.into());
}
let norm = text.strip_prefix('\u{feff}').unwrap_or(text).replace("\r\n", "\n").replace('\r', "\n");
let mut p = Parser::new(&norm);
let Some(top) = p.stream()? else {
return Ok((Value::Null, KeyOrder::default()));
};
if duplicate_key(&top) {
return Err("Map keys must be unique".into());
}
if let Some(problem) = yaml_problem(&top) {
return Err(problem);
}
if let Some(tag) = unresolved_tag(&top) {
return Err(format!("Unresolved tag: {tag}"));
}
Ok((yaml_value(&top), yaml_key_order(&top)))
}
pub fn parse_yaml(text: &str) -> Result<Value, LoadError> {
load_yaml(text).map(|(v, _)| v)
}
fn yaml_documents(text: &str) -> usize {
let text = text.strip_prefix('\u{feff}').unwrap_or(text);
let mut n = 0;
let mut open = false;
for line in text.split_inclusive('\n') {
let content = line.trim_end_matches(['\r', '\n']);
if document_marker(content, "---") {
n += 1;
open = true;
} else if document_marker(content, "...") {
if !open {
n += 1;
}
open = false;
} else if open || content.starts_with('%') {
} else {
let t = content.trim_start_matches([' ', '\t']);
if !t.is_empty() && !t.starts_with('#') {
n += 1;
open = true;
}
}
}
n
}
fn document_marker(line: &str, marker: &str) -> bool {
line.starts_with(marker) && matches!(line.as_bytes().get(marker.len()), None | Some(b' ') | Some(b'\t'))
}
struct Parser {
c: Vec<char>,
col: Vec<u32>,
line: Vec<u32>,
i: usize,
handles: HashMap<String, String>,
depth: usize,
inline_value: bool,
min_col: i64,
}
type R<T> = Result<T, LoadError>;
fn blank_char(c: char) -> bool {
c == ' ' || c == '\t'
}
fn flow_indicator(c: char) -> bool {
matches!(c, ',' | '[' | ']' | '{' | '}')
}
impl Parser {
fn new(text: &str) -> Parser {
let c: Vec<char> = text.chars().collect();
let mut col = Vec::with_capacity(c.len() + 1);
let mut line = Vec::with_capacity(c.len() + 1);
let (mut x, mut y) = (0u32, 1u32);
for &ch in &c {
col.push(x);
line.push(y);
if ch == '\n' {
x = 0;
y += 1;
} else {
x += 1;
}
}
col.push(x);
line.push(y);
let mut handles = HashMap::new();
handles.insert("!".to_string(), "!".to_string());
handles.insert("!!".to_string(), YAML_TAG.to_string());
Parser { c, col, line, i: 0, handles, depth: 0, inline_value: false, min_col: -1 }
}
fn at(&self, k: usize) -> char {
self.c.get(self.i + k).copied().unwrap_or('\0')
}
fn eof(&self) -> bool {
self.i >= self.c.len()
}
fn col_of(&self, i: usize) -> i64 {
self.col.get(i).copied().unwrap_or(0) as i64
}
fn col(&self) -> i64 {
self.col_of(self.i)
}
fn err<T>(&self, what: &str) -> R<T> {
let line = self.line.get(self.i).copied().unwrap_or(0);
Err(format!("{what} at line {line}, column {}", self.col() + 1))
}
fn blank_or_end(&self, k: usize) -> bool {
let c = self.at(k);
c == '\0' && self.i + k >= self.c.len() || c == ' ' || c == '\t' || c == '\n'
}
fn skip_blanks(&mut self) {
while !self.eof() && blank_char(self.at(0)) {
self.i += 1;
}
}
fn skip_comment(&mut self) {
if self.at(0) == '#' {
while !self.eof() && self.at(0) != '\n' {
self.i += 1;
}
}
}
fn skip_space(&mut self, block: bool) -> R<()> {
loop {
let line_start = self.col() == 0;
let from = self.i;
self.skip_blanks();
if self.eof() {
return Ok(());
}
match self.at(0) {
'#' if self.i == from || line_start || blank_char(self.c[self.i - 1]) => self.skip_comment(),
'\n' => {
self.i += 1;
continue;
}
_ => {
if block && line_start && self.c[from..self.i].contains(&'\t') {
return self.err("a tab cannot indent block content");
}
return Ok(());
}
}
if self.at(0) == '\n' {
self.i += 1;
}
}
}
fn at_marker(&self) -> bool {
self.col() == 0
&& (self.at(0) == '-' && self.at(1) == '-' && self.at(2) == '-' || self.at(0) == '.' && self.at(1) == '.' && self.at(2) == '.')
&& self.blank_or_end(3)
}
fn stream(&mut self) -> R<Option<YNode>> {
loop {
self.skip_space(true)?;
if self.col() == 0 && self.at(0) == '%' {
self.directive()?;
continue;
}
break;
}
let mut started = false;
if self.at_marker() && self.at(0) == '-' {
self.i += 3;
started = true;
}
let node = if self.at_marker() || self.eof() {
if started { Some(YNode::empty()) } else { None }
} else {
let save = self.i;
self.skip_space(true)?;
if self.eof() || self.at_marker() {
if started { Some(YNode::empty()) } else { None }
} else {
self.i = save;
Some(self.block_node(-1, false)?)
}
};
self.skip_space(true)?;
if self.at_marker() && self.at(0) == '.' {
self.i += 3;
loop {
self.skip_space(true)?;
if self.col() == 0 && self.at(0) == '%' {
self.directive()?;
continue;
}
break;
}
}
if !self.eof() {
return self.err("did not find the end of the document");
}
Ok(node)
}
fn directive(&mut self) -> R<()> {
let start = self.i;
while !self.eof() && self.at(0) != '\n' {
self.i += 1;
}
let line: String = self.c[start..self.i].iter().collect();
let line = line.split(" #").next().unwrap_or("").to_string();
let parts: Vec<&str> = line.split([' ', '\t']).filter(|s| !s.is_empty()).collect();
if parts.first() == Some(&"%TAG") {
if parts.len() != 3 || !parts[1].starts_with('!') || !parts[1].ends_with('!') {
return self.err("a %TAG directive names a handle and a prefix");
}
self.handles.insert(parts[1].to_string(), parts[2].to_string());
}
Ok(())
}
fn properties(&mut self, flow: bool) -> R<(Option<String>, bool, bool)> {
let mut tag = None;
let mut anchor = false;
let mut any = false;
loop {
match self.at(0) {
'!' if tag.is_none() => {
tag = Some(self.tag(flow)?);
any = true;
}
'&' if !anchor => {
self.i += 1;
if self.name(flow) == 0 {
return self.err("an anchor needs a name");
}
anchor = true;
any = true;
}
_ => return Ok((tag, anchor, any)),
}
self.skip_blanks();
}
}
fn name(&mut self, _flow: bool) -> usize {
let start = self.i;
while !self.eof() && !self.blank_or_end(0) && !flow_indicator(self.at(0)) {
self.i += 1;
}
self.i - start
}
fn tag(&mut self, flow: bool) -> R<String> {
let start = self.i;
self.i += 1;
if self.at(0) == '<' {
let from = self.i + 1;
while !self.eof() && self.at(0) != '>' && self.at(0) != '\n' {
self.i += 1;
}
if self.at(0) != '>' {
return self.err("a verbatim tag is not closed");
}
let s: String = self.c[from..self.i].iter().collect();
self.i += 1;
return Ok(s);
}
while !self.eof() && !self.blank_or_end(0) && !(flow && flow_indicator(self.at(0))) {
self.i += 1;
}
let written: String = self.c[start..self.i].iter().collect();
if written == "!" {
return Ok("!".into());
}
let rest = &written[1..];
let (handle, suffix) = match rest.find('!') {
Some(k) => (format!("!{}", &rest[..=k]), &rest[k + 1..]),
None => ("!".to_string(), rest),
};
match self.handles.get(&handle) {
Some(prefix) => Ok(format!("{prefix}{}", percent_decode(suffix))),
None => self.err(&format!("the tag handle {handle} is not declared")),
}
}
fn enter(&mut self) -> R<()> {
self.depth += 1;
if self.depth > MAX_DEPTH {
return self.err("the YAML is nested too deeply");
}
Ok(())
}
fn block_node(&mut self, n: i64, seq_at_n: bool) -> R<YNode> {
self.enter()?;
let r = self.block_node_inner(n, seq_at_n);
self.depth -= 1;
r
}
fn block_node_inner(&mut self, n: i64, seq_at_n: bool) -> R<YNode> {
let inline = std::mem::take(&mut self.inline_value);
let line_before = self.line.get(self.i).copied();
self.skip_space(true)?;
let crossed = self.line.get(self.i).copied() != line_before;
if self.eof() || self.at_marker() || crossed && (self.col() < n || self.col() == n && !(seq_at_n && self.seq_entry())) {
return Ok(YNode::empty());
}
if !crossed && self.col() <= n {
return Ok(YNode::empty());
}
let props_at = self.i;
let (tag, anchor, any) = self.properties(false)?;
if any {
self.skip_blanks();
if self.at(0) == '#' || self.at(0) == '\n' || self.eof() {
self.skip_comment();
let save = self.i;
self.skip_space(true)?;
if self.eof() || self.at_marker() || self.col() < n || self.col() == n && !(seq_at_n && self.seq_entry()) {
self.i = save;
return Ok(YNode { kind: Kind::Scalar(String::new(), Style::Plain), tag, anchor });
}
let mut node = self.block_content(n, seq_at_n)?;
node.tag = tag;
node.anchor = anchor;
return Ok(node);
}
if self.implicit_key_ahead() {
if inline {
return self.err("mapping values are not allowed in this context");
}
let col = self.col_of(props_at);
return self.block_mapping(col, Some((tag, anchor)));
}
if inline && (self.seq_entry() || self.at(0) == '?' && self.blank_or_end(1)) {
return self.err("a block collection cannot start on its key's line");
}
let mut node = self.block_content(n, seq_at_n)?;
node.tag = tag;
node.anchor = anchor;
return Ok(node);
}
if inline && (self.seq_entry() || self.at(0) == '?' && self.blank_or_end(1) || self.implicit_key_ahead()) {
return self.err("mapping values are not allowed in this context");
}
self.block_content(n, seq_at_n)
}
fn seq_entry(&self) -> bool {
self.at(0) == '-' && self.blank_or_end(1)
}
fn block_content(&mut self, n: i64, _seq_at_n: bool) -> R<YNode> {
let col = self.col();
match self.at(0) {
'-' if self.blank_or_end(1) => return self.block_sequence(col),
'?' if self.blank_or_end(1) => return self.block_mapping(col, None),
':' if self.blank_or_end(1) => return self.block_mapping(col, None),
'|' | '>' => return self.block_scalar(n),
_ => {}
}
if self.implicit_key_ahead() {
return self.block_mapping(col, None);
}
let outer = std::mem::replace(&mut self.min_col, n);
let node = match self.at(0) {
'*' => {
self.i += 1;
if self.name(false) == 0 {
return self.err("an alias needs a name");
}
Ok(YNode { kind: Kind::Alias, tag: None, anchor: false })
}
'[' | '{' => self.flow_node(),
'"' => self.double_quoted().map(|s| YNode::scalar(s, Style::Quoted)),
'\'' => self.single_quoted().map(|s| YNode::scalar(s, Style::Quoted)),
_ => self.plain(n, false).map(|s| YNode::scalar(s, Style::Plain)),
};
self.min_col = outer;
let node = node?;
self.end_of_line()?;
Ok(node)
}
fn end_of_line(&mut self) -> R<()> {
self.skip_blanks();
if self.at(0) == '#' {
self.skip_comment();
}
if !self.eof() && self.at(0) != '\n' {
if self.at(0) == ':' {
return self.err("mapping values are not allowed in this context");
}
return self.err("did not find the expected end of the line");
}
Ok(())
}
fn implicit_key_ahead(&self) -> bool {
let mut k = self.i;
let at = |k: usize| self.c.get(k).copied().unwrap_or('\0');
let end_or_blank = |k: usize| k >= self.c.len() || matches!(self.c[k], ' ' | '\t' | '\n');
match at(k) {
'"' | '\'' => {
let q = at(k);
k += 1;
while k < self.c.len() && self.c[k] != '\n' {
if q == '"' && self.c[k] == '\\' {
k += 2;
continue;
}
if self.c[k] == q {
if q == '\'' && at(k + 1) == '\'' {
k += 2;
continue;
}
break;
}
k += 1;
}
if at(k) != q {
return false;
}
k += 1;
}
'[' | '{' => {
let mut depth = 0i64;
while k < self.c.len() && self.c[k] != '\n' {
match self.c[k] {
'[' | '{' => depth += 1,
']' | '}' => {
depth -= 1;
if depth == 0 {
k += 1;
break;
}
}
'"' | '\'' => {
let q = self.c[k];
k += 1;
while k < self.c.len() && self.c[k] != q && self.c[k] != '\n' {
k += if q == '"' && self.c[k] == '\\' { 2 } else { 1 };
}
}
_ => {}
}
k += 1;
}
if depth != 0 {
return false;
}
}
'*' => {
k += 1;
while k < self.c.len() && !end_or_blank(k) && !flow_indicator(self.c[k]) {
k += 1;
}
}
c if c == '\0' || c == '\n' || c == '#' => return false,
'-' | '?' | ':' if end_or_blank(k + 1) => return false,
_ => {
while k < self.c.len() && self.c[k] != '\n' {
if self.c[k] == ':' && end_or_blank(k + 1) {
return true;
}
if self.c[k] == '#' && k > 0 && blank_char(self.c[k - 1]) {
return false;
}
k += 1;
}
return false;
}
}
while k < self.c.len() && blank_char(self.c[k]) {
k += 1;
}
at(k) == ':' && end_or_blank(k + 1)
}
fn block_mapping(&mut self, m: i64, mut first: Option<(Option<String>, bool)>) -> R<YNode> {
self.enter()?;
let mut pairs = Vec::new();
loop {
let key = if self.at(0) == '?' && self.blank_or_end(1) {
self.i += 1;
let k = self.block_node(m, false)?;
self.skip_space(true)?;
if !(self.col() == m && self.at(0) == ':' && self.blank_or_end(1)) {
pairs.push((k, YNode::empty()));
if self.next_entry(m)? {
continue;
}
break;
}
k
} else if self.at(0) == ':' && self.blank_or_end(1) {
YNode::empty()
} else {
let (tag, anchor) = match first.take() {
Some(p) => p,
None => {
let (t, a, _) = self.properties(false)?;
(t, a)
}
};
let mut k = match self.at(0) {
'"' => YNode::scalar(self.double_quoted()?, Style::Quoted),
'\'' => YNode::scalar(self.single_quoted()?, Style::Quoted),
'[' | '{' => self.flow_node()?,
'*' => {
self.i += 1;
self.name(false);
YNode { kind: Kind::Alias, tag: None, anchor: false }
}
_ => YNode::scalar(self.plain_key()?, Style::Plain),
};
k.tag = tag.or(k.tag);
k.anchor = anchor || k.anchor;
self.skip_blanks();
if !(self.at(0) == ':' && self.blank_or_end(1)) {
return self.err("could not find the expected ':'");
}
k
};
self.i += 1;
self.skip_blanks();
let value = if self.at(0) == '#' || self.at(0) == '\n' || self.eof() {
self.skip_comment();
self.block_node(m, true)?
} else {
self.inline_value = true;
self.block_node(m, false)?
};
pairs.push((key, value));
if !self.next_entry(m)? {
break;
}
}
self.depth -= 1;
Ok(YNode { kind: Kind::Map(pairs), tag: None, anchor: false })
}
fn next_entry(&mut self, m: i64) -> R<bool> {
let save = self.i;
self.skip_space(true)?;
if self.eof() || self.at_marker() || self.col() < m {
self.i = save;
return Ok(false);
}
if self.col() > m {
return self.err("bad indentation of a mapping entry");
}
if self.seq_entry() {
return self.err("a sequence entry where a mapping key was expected");
}
Ok(true)
}
fn block_sequence(&mut self, s: i64) -> R<YNode> {
self.enter()?;
let mut items = Vec::new();
loop {
self.i += 1; self.skip_blanks();
let item = if self.at(0) == '#' || self.at(0) == '\n' || self.eof() {
self.skip_comment();
self.block_node(s, false)?
} else {
self.block_node(s, false)?
};
items.push(item);
let save = self.i;
self.skip_space(true)?;
if self.eof() || self.at_marker() || self.col() < s {
self.i = save;
break;
}
if self.col() > s {
return self.err("bad indentation of a sequence entry");
}
if !self.seq_entry() {
self.i = save;
break;
}
}
self.depth -= 1;
Ok(YNode { kind: Kind::Seq(items), tag: None, anchor: false })
}
fn plain_key(&mut self) -> R<String> {
if self.at(0) == '@' || self.at(0) == '`' {
return self.err("a plain scalar cannot start with a reserved character");
}
let start = self.i;
while !self.eof() && self.at(0) != '\n' {
if self.at(0) == ':' && self.blank_or_end(1) {
break;
}
if self.at(0) == '#' && self.i > start && blank_char(self.c[self.i - 1]) {
break;
}
self.i += 1;
}
let s: String = self.c[start..self.i].iter().collect();
Ok(s.trim_end_matches([' ', '\t']).to_string())
}
fn plain(&mut self, n: i64, flow: bool) -> R<String> {
let c0 = self.at(0);
if matches!(c0, '@' | '`' | '%' | '|' | '>' | '!' | '&' | '#' | '\'' | '"')
|| flow_indicator(c0)
|| matches!(c0, '-' | '?' | ':') && (self.blank_or_end(1) || flow && flow_indicator(self.at(1)))
{
return self.err("found a character that cannot start a plain scalar");
}
let mut out = String::new();
let mut first = true;
loop {
let start = self.i;
while !self.eof() && self.at(0) != '\n' {
let c = self.at(0);
if c == ':' && (self.blank_or_end(1) || flow && flow_indicator(self.at(1))) {
break;
}
if c == '#' && self.i > start && blank_char(self.c[self.i - 1]) {
break;
}
if flow && flow_indicator(c) {
break;
}
self.i += 1;
}
let mut end = self.i;
while end > start && blank_char(self.c[end - 1]) {
end -= 1;
}
let text: String = self.c[start..end].iter().collect();
if !first {
out.push_str(&text);
} else {
out = text;
first = false;
}
if self.at(0) != '\n' {
break;
}
let save = self.i;
let mut breaks = 0;
let mut k = self.i;
loop {
k += 1;
let line_start = k;
while k < self.c.len() && blank_char(self.c[k]) {
k += 1;
}
if k < self.c.len() && self.c[k] == '\n' {
breaks += 1;
continue;
}
let indent = (k - line_start) as i64;
let content = self.c.get(k).copied().unwrap_or('\0');
let tabbed = self.c[line_start..k].contains(&'\t');
let lead_spaces = self.c[line_start..k].iter().take_while(|&&c| c == ' ').count() as i64;
let stop = k >= self.c.len()
|| content == '#'
|| lead_spaces == 0 && {
let three: String = self.c[line_start..(line_start + 3).min(self.c.len())].iter().collect();
(three == "---" || three == "...") && matches!(self.c.get(line_start + 3), None | Some(' ' | '\t' | '\n'))
}
|| !flow && (indent <= n || tabbed && lead_spaces <= n)
|| flow && lead_spaces <= self.min_col
|| flow && flow_indicator(content) && content != '[' && content != '{'
|| content == ':' && matches!(self.c.get(k + 1), None | Some(' ' | '\t' | '\n'));
if stop {
self.i = save;
return Ok(out);
}
self.i = k;
break;
}
if breaks == 0 {
out.push(' ');
} else {
out.push_str(&"\n".repeat(breaks));
}
}
Ok(out)
}
fn fold_quoted(&mut self, out: &mut String) -> R<()> {
while out.ends_with([' ', '\t']) {
out.pop();
}
let mut breaks = 0;
while self.at(0) == '\n' {
self.i += 1;
if self.at_marker() {
return self.err("a document marker inside a quoted scalar");
}
let line_start = self.i;
while self.at(0) == ' ' {
self.i += 1;
}
if !self.eof() && self.at(0) != '\n' && ((self.i - line_start) as i64) <= self.min_col {
return self.err("missing the closing quote: a quoted scalar's lines are indented past its block collection");
}
self.skip_blanks();
if self.at(0) == '\n' {
breaks += 1;
}
}
if breaks == 0 {
out.push(' ');
} else {
out.push_str(&"\n".repeat(breaks));
}
Ok(())
}
fn single_quoted(&mut self) -> R<String> {
self.i += 1;
let mut out = String::new();
loop {
if self.eof() {
return self.err("found the end of the stream inside a single-quoted scalar");
}
match self.at(0) {
'\'' if self.at(1) == '\'' => {
out.push('\'');
self.i += 2;
}
'\'' => {
self.i += 1;
return Ok(out);
}
'\n' => self.fold_quoted(&mut out)?,
c => {
out.push(c);
self.i += 1;
}
}
}
}
fn double_quoted(&mut self) -> R<String> {
self.i += 1;
let mut out = String::new();
loop {
if self.eof() {
return self.err("found the end of the stream inside a double-quoted scalar");
}
match self.at(0) {
'"' => {
self.i += 1;
return Ok(out);
}
'\n' => self.fold_quoted(&mut out)?,
'\\' => {
self.i += 1;
let e = self.at(0);
self.i += 1;
let simple = match e {
'0' => Some('\0'),
'a' => Some('\u{7}'),
'b' => Some('\u{8}'),
't' | '\t' => Some('\t'),
'n' => Some('\n'),
'v' => Some('\u{b}'),
'f' => Some('\u{c}'),
'r' => Some('\r'),
'e' => Some('\u{1b}'),
' ' => Some(' '),
'"' => Some('"'),
'/' => Some('/'),
'\\' => Some('\\'),
'N' => Some('\u{85}'),
'_' => Some('\u{a0}'),
'L' => Some('\u{2028}'),
'P' => Some('\u{2029}'),
_ => None,
};
if let Some(c) = simple {
out.push(c);
continue;
}
match e {
'\n' => {
self.skip_blanks();
while self.at(0) == '\n' {
out.push('\n');
self.i += 1;
self.skip_blanks();
}
}
'x' | 'u' | 'U' => {
let len = match e {
'x' => 2,
'u' => 4,
_ => 8,
};
let mut code: u32 = 0;
for _ in 0..len {
let Some(d) = self.at(0).to_digit(16) else {
return self.err("a malformed escape in a double-quoted scalar");
};
code = code * 16 + d;
self.i += 1;
}
if (0xd800..0xdc00).contains(&code) && self.at(0) == '\\' && self.at(1) == 'u' {
let mut low: u32 = 0;
let mut ok = true;
for k in 0..4 {
match self.at(2 + k).to_digit(16) {
Some(d) => low = low * 16 + d,
None => ok = false,
}
}
if ok && (0xdc00..0xe000).contains(&low) {
self.i += 6;
code = 0x10000 + ((code - 0xd800) << 10) + (low - 0xdc00);
}
}
out.push(char::from_u32(code).unwrap_or('\u{fffd}'));
}
_ => return self.err("an unknown escape in a double-quoted scalar"),
}
}
c => {
out.push(c);
self.i += 1;
}
}
}
}
fn block_scalar(&mut self, n: i64) -> R<YNode> {
let folded = self.at(0) == '>';
self.i += 1;
let mut chomp = 0i8; let mut increment: Option<i64> = None;
for _ in 0..2 {
match self.at(0) {
'+' | '-' if chomp == 0 => {
chomp = if self.at(0) == '+' { 1 } else { -1 };
self.i += 1;
}
d @ '1'..='9' if increment.is_none() => {
increment = Some(d as i64 - '0' as i64);
self.i += 1;
}
_ => {}
}
}
self.skip_blanks();
if self.at(0) == '#' && blank_char(self.c[self.i - 1]) {
self.skip_comment();
}
if !self.eof() && self.at(0) != '\n' {
return self.err("did not find the expected comment or line break after a block scalar's header");
}
let base = n.max(-1);
let mut indent = match increment {
Some(k) => base.max(0) + k - if base < 0 { 1 } else { 0 },
None => -1,
};
let mut out = String::new();
let mut leading_break = String::new();
let mut trailing_breaks = String::new();
let mut leading_blank = false;
if self.at(0) == '\n' {
self.i += 1;
} else {
return Ok(YNode::scalar(String::new(), Style::Block));
}
if indent < 0 {
let mut max_seen = 0i64;
let mut k = self.i;
loop {
let start = k;
while k < self.c.len() && self.c[k] == ' ' {
k += 1;
}
let spaces = (k - start) as i64;
if k < self.c.len() && self.c[k] == '\n' {
max_seen = max_seen.max(spaces);
k += 1;
continue;
}
indent = if k >= self.c.len() { max_seen.max(base + 1) } else { spaces.max(max_seen) };
if indent <= base {
indent = base + 1;
}
break;
}
if indent < 1 && base >= 0 {
indent = 1;
}
if indent < 0 {
indent = 0;
}
}
self.block_breaks(indent, &mut trailing_breaks);
while self.col() == indent && !self.eof() && !(indent == 0 && self.at_marker()) && self.at(0) != '\n' {
let trailing_blank = blank_char(self.at(0));
if folded && leading_break == "\n" && !leading_blank && !trailing_blank {
if trailing_breaks.is_empty() {
out.push(' ');
}
leading_break.clear();
}
out.push_str(&leading_break);
out.push_str(&trailing_breaks);
leading_break.clear();
trailing_breaks.clear();
leading_blank = blank_char(self.at(0));
while !self.eof() && self.at(0) != '\n' {
out.push(self.at(0));
self.i += 1;
}
if self.eof() {
break;
}
self.i += 1;
leading_break.push('\n');
self.block_breaks(indent, &mut trailing_breaks);
}
if chomp != -1 {
out.push_str(&leading_break);
}
if chomp == 1 {
out.push_str(&trailing_breaks);
}
Ok(YNode::scalar(out, Style::Block))
}
fn block_breaks(&mut self, indent: i64, breaks: &mut String) {
loop {
while self.col() < indent && self.at(0) == ' ' {
self.i += 1;
}
if self.at(0) == '\n' && !self.eof() {
breaks.push('\n');
self.i += 1;
continue;
}
if self.col() >= indent {
let mut k = self.i;
while k < self.c.len() && blank_char(self.c[k]) {
k += 1;
}
if k < self.c.len() && self.c[k] == '\n' && self.col() < indent {
self.i = k;
continue;
}
}
return;
}
}
fn skip_flow_space(&mut self) -> R<()> {
loop {
self.skip_blanks();
match self.at(0) {
'#' if self.i == 0 || blank_char(self.c[self.i - 1]) || self.col() == 0 => self.skip_comment(),
'\n' if !self.eof() => {
self.i += 1;
if self.at_marker() {
return self.err("a document marker inside a flow collection");
}
let line_start = self.i;
while self.at(0) == ' ' {
self.i += 1;
}
let col = (self.i - line_start) as i64;
let c = self.at(0);
if !self.eof()
&& !matches!(c, '\n' | '#' | '\t')
&& (col < self.min_col || col == self.min_col && !matches!(c, ']' | '}'))
{
return self.err("a flow collection in a block collection must be indented past it");
}
}
_ => return Ok(()),
}
}
}
fn flow_node(&mut self) -> R<YNode> {
self.enter()?;
let r = self.flow_node_inner();
self.depth -= 1;
r
}
fn flow_node_inner(&mut self) -> R<YNode> {
let (tag, anchor, _) = self.properties(true)?;
self.skip_flow_space()?;
let mut node = match self.at(0) {
'[' => self.flow_sequence()?,
'{' => self.flow_mapping()?,
'"' => YNode::scalar(self.double_quoted()?, Style::Quoted),
'\'' => YNode::scalar(self.single_quoted()?, Style::Quoted),
'*' => {
self.i += 1;
if self.name(true) == 0 {
return self.err("an alias needs a name");
}
YNode { kind: Kind::Alias, tag: None, anchor: false }
}
',' | ']' | '}' | ':' if !(self.at(0) == ':' && !self.blank_or_end(1) && !flow_indicator(self.at(1))) => YNode::empty(),
_ if self.eof() => return self.err("found the end of the stream inside a flow collection"),
_ => YNode::scalar(self.plain(-1, true)?, Style::Plain),
};
node.tag = tag.or(node.tag);
node.anchor = anchor || node.anchor;
Ok(node)
}
fn flow_colon(&self, adjacent: bool, key_line: u32) -> R<bool> {
if self.at(0) != ':' {
return Ok(false);
}
if !adjacent && !(self.blank_or_end(1) || flow_indicator(self.at(1))) {
return Ok(false);
}
if self.line_here() != key_line {
return self.err("an implicit key stands on one line: did not find expected ',' or '}'");
}
Ok(true)
}
fn line_here(&self) -> u32 {
self.line.get(self.i).copied().unwrap_or(0)
}
fn flow_sequence(&mut self) -> R<YNode> {
self.i += 1;
let mut items = Vec::new();
loop {
self.skip_flow_space()?;
if self.eof() {
return self.err("found the end of the stream inside a flow sequence");
}
if self.at(0) == ']' {
self.i += 1;
break;
}
let explicit = self.at(0) == '?' && self.blank_or_end(1);
if explicit {
self.i += 1;
self.skip_flow_space()?;
}
if self.at(0) == ',' {
return self.err("did not find expected node content");
}
let adjacent = matches!(self.at(0), '"' | '\'' | '[' | '{');
let key_line = self.line_here();
let item = self.flow_node()?;
self.skip_flow_space()?;
let item = if self.flow_colon(adjacent, if explicit { self.line_here() } else { key_line })? {
self.i += 1;
self.skip_flow_space()?;
let value = if matches!(self.at(0), ',' | ']') { YNode::empty() } else { self.flow_node()? };
YNode { kind: Kind::Map(vec![(item, value)]), tag: None, anchor: false }
} else if explicit {
YNode { kind: Kind::Map(vec![(item, YNode::empty())]), tag: None, anchor: false }
} else {
item
};
items.push(item);
self.skip_flow_space()?;
match self.at(0) {
',' => self.i += 1,
']' => {}
_ => return self.err("did not find the expected ',' or ']'"),
}
}
Ok(YNode { kind: Kind::Seq(items), tag: None, anchor: false })
}
fn flow_mapping(&mut self) -> R<YNode> {
self.i += 1;
let mut pairs = Vec::new();
loop {
self.skip_flow_space()?;
if self.eof() {
return self.err("found the end of the stream inside a flow mapping");
}
if self.at(0) == '}' {
self.i += 1;
break;
}
if self.at(0) == ',' {
return self.err("did not find expected node content");
}
let explicit = self.at(0) == '?' && self.blank_or_end(1);
if explicit {
self.i += 1;
self.skip_flow_space()?;
}
let adjacent = matches!(self.at(0), '"' | '\'' | '[' | '{');
let key_line = self.line_here();
let key =
if self.at(0) == ':' && (self.blank_or_end(1) || flow_indicator(self.at(1))) { YNode::empty() } else { self.flow_node()? };
self.skip_flow_space()?;
let value = if self.flow_colon(adjacent, if explicit { self.line_here() } else { key_line })? {
self.i += 1;
self.skip_flow_space()?;
if matches!(self.at(0), ',' | '}') { YNode::empty() } else { self.flow_node()? }
} else {
YNode::empty()
};
pairs.push((key, value));
self.skip_flow_space()?;
match self.at(0) {
',' => self.i += 1,
'}' => {}
_ => return self.err("did not find the expected ',' or '}'"),
}
}
Ok(YNode { kind: Kind::Map(pairs), tag: None, anchor: false })
}
}
fn percent_decode(s: &str) -> String {
if !s.contains('%') {
return s.to_string();
}
let b = s.as_bytes();
let mut out = Vec::with_capacity(b.len());
let mut i = 0;
while i < b.len() {
if b[i] == b'%'
&& i + 2 < b.len()
&& let (Some(h), Some(l)) = ((b[i + 1] as char).to_digit(16), (b[i + 2] as char).to_digit(16))
{
out.push((h * 16 + l) as u8);
i += 3;
continue;
}
out.push(b[i]);
i += 1;
}
String::from_utf8(out).unwrap_or_else(|_| s.to_string())
}
fn short_tag(tag: &str) -> &str {
tag.strip_prefix(YAML_TAG).unwrap_or(tag)
}
fn core_tag(tag: &str) -> bool {
matches!(tag, "str" | "int" | "float" | "bool" | "null" | "seq" | "map")
}
fn scalar(n: &YNode) -> (Scalar, bool) {
let Kind::Scalar(s, style) = &n.kind else {
return (Scalar::Null, false);
};
let tag = match &n.tag {
Some(t) if t == "!" => return (Scalar::Str(s.clone()), true),
Some(t) => short_tag(t).to_string(),
None if *style != Style::Plain => return (Scalar::Str(s.clone()), true),
None => String::new(),
};
let fits = |t: &str| tag.is_empty() || tag == t;
let core = &*CORE;
if tag == "str" {
return (Scalar::Str(s.clone()), true);
}
if fits("null") && core.null.is_match(s) {
return (Scalar::Null, true);
}
if fits("bool") && core.boolean.is_match(s) {
return (Scalar::Bool(s.starts_with(['t', 'T'])), true);
}
if fits("int") && core.int_oct.is_match(s) {
return (Scalar::Num(integer(&s[2..], 8)), true);
}
if fits("int") && core.int.is_match(s) {
return (Scalar::Num(plus_zero(s.parse().unwrap_or(0.0))), true);
}
if fits("int") && core.int_hex.is_match(s) {
return (Scalar::Num(integer(&s[2..], 16)), true);
}
if fits("float") && core.float_nan.is_match(s) {
if s.to_ascii_lowercase().ends_with("nan") {
return (Scalar::Num(f64::NAN), true);
}
return (Scalar::Num(if s.starts_with('-') { f64::NEG_INFINITY } else { f64::INFINITY }), true);
}
if fits("float") && (core.float_exp.is_match(s) || core.float.is_match(s)) {
return (Scalar::Num(plus_zero(s.parse().unwrap_or(0.0))), true);
}
(Scalar::Str(s.clone()), tag.is_empty() || !core_tag(&tag))
}
fn plus_zero(f: f64) -> f64 {
if f == 0.0 { 0.0 } else { f }
}
fn integer(digits: &str, base: u32) -> f64 {
let mut exact: Option<u128> = Some(0);
let mut approx = 0.0f64;
for c in digits.chars() {
let d = c.to_digit(base).unwrap_or(0);
exact = exact.and_then(|v| v.checked_mul(base as u128)).and_then(|v| v.checked_add(d as u128));
approx = approx * base as f64 + d as f64;
}
match exact {
Some(v) => v as f64,
None => approx,
}
}
fn duplicate_key(n: &YNode) -> bool {
match &n.kind {
Kind::Map(pairs) => {
let mut keys: Vec<Scalar> = Vec::new();
for (k, _) in pairs {
if let Kind::Scalar(..) = k.kind {
let (v, _) = scalar(k);
if keys.iter().any(|seen| seen.same(&v)) {
return true;
}
keys.push(v);
}
}
pairs.iter().any(|(k, v)| duplicate_key(k) || duplicate_key(v))
}
Kind::Seq(items) => items.iter().any(duplicate_key),
_ => false,
}
}
fn yaml_problem(n: &YNode) -> Option<String> {
if let Kind::Alias = n.kind {
return Some("YAML aliases are not allowed".into());
}
if n.anchor {
return Some("YAML anchors are not allowed".into());
}
if let Some(t) = &n.tag
&& t != "!"
&& !core_tag(short_tag(t))
{
return Some(format!("the YAML tag {t} is not allowed"));
}
if let Kind::Scalar(..) = n.kind
&& let (Scalar::Num(f), _) = scalar(n)
&& !f.is_finite()
{
return Some("non-finite numbers are not allowed".into());
}
match &n.kind {
Kind::Map(pairs) => {
for (k, v) in pairs {
let is_string = matches!(k.kind, Kind::Scalar(..)) && matches!(scalar(k).0, Scalar::Str(_));
if !is_string {
return Some("mapping keys must be strings".into());
}
if let Some(p) = yaml_problem(k).or_else(|| yaml_problem(v)) {
return Some(p);
}
}
None
}
Kind::Seq(items) => items.iter().find_map(yaml_problem),
_ => None,
}
}
fn unresolved_tag(n: &YNode) -> Option<String> {
let tag = n.tag.as_deref().map(short_tag).unwrap_or("");
match &n.kind {
Kind::Scalar(..) => {
if !scalar(n).1 {
return n.tag.clone();
}
}
_ if n.tag.is_none() || !core_tag(tag) => {}
Kind::Seq(_) if tag != "seq" => return n.tag.clone(),
Kind::Map(_) if tag != "map" => return n.tag.clone(),
_ => {}
}
match &n.kind {
Kind::Map(pairs) => pairs.iter().find_map(|(k, v)| unresolved_tag(k).or_else(|| unresolved_tag(v))),
Kind::Seq(items) => items.iter().find_map(unresolved_tag),
_ => None,
}
}
fn key_string(k: &YNode) -> String {
match scalar(k).0 {
Scalar::Str(s) => s,
_ => String::new(),
}
}
fn yaml_value(n: &YNode) -> Value {
match &n.kind {
Kind::Map(pairs) => {
let mut m = Map::new();
for (k, v) in pairs {
m.insert(key_string(k), yaml_value(v));
}
Value::Object(m)
}
Kind::Seq(items) => Value::Array(items.iter().map(yaml_value).collect()),
Kind::Scalar(..) => match scalar(n).0 {
Scalar::Null => Value::Null,
Scalar::Bool(b) => Value::Bool(b),
Scalar::Num(f) => num(f),
Scalar::Str(s) => Value::String(s),
},
Kind::Alias => Value::Null,
}
}
fn yaml_key_order(n: &YNode) -> KeyOrder {
let mut order = KeyOrder::default();
if let Kind::Map(pairs) = &n.kind {
for (k, v) in pairs {
let key = key_string(k);
order.0.entry(String::new()).or_default().push(key.clone());
deeper_key_order(v, key, &mut order);
}
}
order
}
fn deeper_key_order(v: &YNode, path: String, order: &mut KeyOrder) {
if let Kind::Map(sub) = &v.kind {
for (sk, _) in sub {
order.0.entry(path.clone()).or_default().push(key_string(sk));
}
for (sk, sv) in sub {
if matches!(sv.kind, Kind::Map(_)) {
deeper_key_order(sv, format!("{path}\u{1f}{}", key_string(sk)), order);
}
}
}
}
pub(crate) fn load_json(text: &str) -> Result<(Value, KeyOrder), LoadError> {
let v: Value = match serde_json::from_str(text) {
Ok(v) => v,
Err(e) => {
let message = e.to_string();
if message.starts_with("number out of range") {
return Err("non-finite numbers are not allowed".into());
}
return Err(js_trim(message.lines().next().unwrap_or("")).to_string());
}
};
let mut scan = JsonScan { b: text.as_bytes(), i: 0, order: KeyOrder::default(), dup: None };
scan.value("", true, 0);
if let Some(dup) = scan.dup {
return Err(format!("the JSON key \"{dup}\" is repeated"));
}
Ok((normalize_numbers(v), scan.order))
}
fn normalize_numbers(v: Value) -> Value {
match v {
Value::Number(n) => num(n.as_f64().unwrap_or(0.0)),
Value::Array(list) => Value::Array(list.into_iter().map(normalize_numbers).collect()),
Value::Object(m) => Value::Object(m.into_iter().map(|(k, v)| (k, normalize_numbers(v))).collect()),
v => v,
}
}
struct JsonScan<'a> {
b: &'a [u8],
i: usize,
order: KeyOrder,
dup: Option<String>,
}
impl JsonScan<'_> {
fn space(&mut self) {
while self.i < self.b.len() && matches!(self.b[self.i], b' ' | b'\t' | b'\r' | b'\n') {
self.i += 1;
}
}
fn string(&mut self) -> String {
let start = self.i;
self.i += 1;
while self.i < self.b.len() && self.b[self.i] != b'"' {
self.i += if self.b[self.i] == b'\\' { 2 } else { 1 };
}
self.i += 1;
let raw = std::str::from_utf8(&self.b[start..self.i.min(self.b.len())]).unwrap_or("\"\"");
serde_json::from_str::<String>(raw).unwrap_or_default()
}
fn value(&mut self, path: &str, keep: bool, depth: usize) -> bool {
self.space();
if self.i >= self.b.len() || depth > 512 {
return false;
}
match self.b[self.i] {
b'{' => {
self.i += 1;
let mut keys: Vec<String> = Vec::new();
loop {
self.space();
if self.i >= self.b.len() {
return false;
}
if self.b[self.i] == b'}' {
self.i += 1;
return false;
}
if self.b[self.i] == b',' {
self.i += 1;
continue;
}
let key = self.string();
if keys.contains(&key) {
self.dup = Some(key);
return true;
}
keys.push(key.clone());
if keep {
self.order.0.entry(path.to_string()).or_default().push(key.clone());
}
self.space();
self.i += 1; let deeper = keep && path.is_empty() && !key.is_empty();
if self.value(&key, deeper, depth + 1) {
return true;
}
}
}
b'[' => {
self.i += 1;
loop {
self.space();
if self.i >= self.b.len() {
return false;
}
match self.b[self.i] {
b']' => {
self.i += 1;
return false;
}
b',' => self.i += 1,
_ => {
if self.value("", false, depth + 1) {
return true;
}
}
}
}
}
b'"' => {
self.string();
false
}
_ => {
while self.i < self.b.len() && !matches!(self.b[self.i], b' ' | b'\t' | b'\r' | b'\n' | b',' | b']' | b'}') {
self.i += 1;
}
false
}
}
}
}
pub(crate) fn load_csv(text: &str) -> Result<(Vec<String>, Vec<Vec<String>>), LoadError> {
let src = text.replace("\r\n", "\n").replace('\r', "\n");
let mut records: Vec<Vec<String>> = Vec::new();
let mut record: Vec<String> = Vec::new();
let mut cell = String::new();
let mut quoted = false;
let mut chars = src.chars().peekable();
while let Some(c) = chars.next() {
if quoted {
if c == '"' {
if chars.peek() == Some(&'"') {
cell.push('"');
chars.next();
continue;
}
quoted = false;
continue;
}
cell.push(c);
continue;
}
match c {
'"' => {
if !cell.is_empty() {
return Err(format!("a quote inside an unquoted CSV field (record {})", records.len() + 1));
}
quoted = true;
}
',' => record.push(std::mem::take(&mut cell)),
'\n' => {
record.push(std::mem::take(&mut cell));
records.push(std::mem::take(&mut record));
}
c => cell.push(c),
}
}
if quoted {
return Err("an unclosed quoted CSV field".into());
}
if !cell.is_empty() || !record.is_empty() {
record.push(cell);
records.push(record);
}
if records.is_empty() {
return Err("a CSV body needs a header record".into());
}
let header = records.remove(0);
if header.iter().any(String::is_empty) {
return Err("CSV header names must be nonempty".into());
}
let mut names: Vec<&String> = header.iter().collect();
names.sort();
names.dedup();
if names.len() != header.len() {
return Err("CSV header names must be unique".into());
}
for (i, r) in records.iter().enumerate() {
if r.len() != header.len() {
return Err(format!("CSV record {} has {} fields; the header has {}", i + 2, r.len(), header.len()));
}
}
Ok((header, records))
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
fn same(a: &Value, b: &Value) -> bool {
match (a, b) {
(Value::Number(x), Value::Number(y)) => x.as_f64() == y.as_f64(),
(Value::Array(x), Value::Array(y)) => x.len() == y.len() && x.iter().zip(y).all(|(a, b)| same(a, b)),
(Value::Object(x), Value::Object(y)) => x.len() == y.len() && x.iter().all(|(k, v)| y.get(k).is_some_and(|w| same(v, w))),
_ => a == b,
}
}
#[test]
fn core_schema() {
let cases: Vec<(&str, Value)> = vec![
("x: 0x1F\n", json!({"x": 31})),
("x: 0o17\n", json!({"x": 15})),
("x: 010\n", json!({"x": 10})),
("x: 1_000\n", json!({"x": "1_000"})),
("x: yes\n", json!({"x": "yes"})),
("x: 2026-10-26\n", json!({"x": "2026-10-26"})),
("x: 1e3\n", json!({"x": 1000})),
("x: -0\n", json!({"x": 0})),
("x: !!str 1\n", json!({"x": "1"})),
("x: !!int 0x10\n", json!({"x": 16})),
("<<: {a: 1}\n", json!({"<<": {"a": 1}})),
("x: ~\ny: Null\nz:\n", json!({"x": null, "y": null, "z": null})),
("", Value::Null),
("# only a comment\n", Value::Null),
("- a\n- b: c\n", json!(["a", {"b": "c"}])),
("x: '12:30'\ny: 12:30\n", json!({"x": "12:30", "y": "12:30"})),
("? !!str 1\n: v\n", json!({"1": "v"})),
("x: !!null ''\n", json!({"x": null})),
("x: \"\\u00e9\"\n", json!({"x": "\u{e9}"})),
("x: !!seq [a]\n", json!({"x": ["a"]})),
("x: !!map {a: 1}\n", json!({"x": {"a": 1}})),
("x: 0b11\ny: 0X1F\n", json!({"x": "0b11", "y": "0X1F"})),
("x: !!int 0o17\n", json!({"x": 15})),
("x: !!int +12\n", json!({"x": 12})),
("x: !!float 1.5e3\n", json!({"x": 1500})),
("x: !!bool True\n", json!({"x": true})),
("x: !!str null\n", json!({"x": "null"})),
("x: !!null ~\n", json!({"x": null})),
("x: !!null null\n", json!({"x": null})),
("x: !!null Null\n", json!({"x": null})),
("x: !!null NULL\n", json!({"x": null})),
("x: !!float 1e3\n", json!({"x": 1000})),
("x: !!float -1.\n", json!({"x": -1})),
("x: !!float +.5\n", json!({"x": 0.5})),
("x: !!float .5e-3\n", json!({"x": 0.0005})),
("x: !!float 0.5\n", json!({"x": 0.5})),
("x: !!float -0.0\n", json!({"x": 0})),
("x: !!float 00.5\n", json!({"x": 0.5})),
("x: !!float 5.\n", json!({"x": 5})),
("x: !!float +5.e5\n", json!({"x": 500000})),
("x: !!float 5.e+5\n", json!({"x": 500000})),
("x: !!float 5.E-5\n", json!({"x": 5e-05})),
("x: !!float -5E5\n", json!({"x": -500000})),
("%YAML 1.2\n---\nx: 1\n", json!({"x": 1})),
("%YAML 1.1\n---\nx: yes\n", json!({"x": "yes"})),
("x: 1\n...\n", json!({"x": 1})),
("x: a\u{2028}b\n", json!({"x": "a\u{2028}b"})),
("x: 'a\u{85}b'\n", json!({"x": "a\u{85}b"})),
("- \u{2029}\n", json!(["\u{2029}"])),
("a:\n b: 1\n c: [x, y]\nd:\n- 1\n- 2\n", json!({"a": {"b": 1, "c": ["x", "y"]}, "d": [1, 2]})),
("a: |\n one\n two\nb: >-\n folded\n text\n\n para\n", json!({"a": "one\ntwo\n", "b": "folded text\npara"})),
("a: long\n plain text\n", json!({"a": "long plain text"})),
("- a: 1\n b: 2\n- - x\n - y\n", json!([{"a": 1, "b": 2}, ["x", "y"]])),
("x: \"a\n b\"\n", json!({"x": "a b"})),
("x: {a: 1, b: [2, {c: d}]}\n", json!({"x": {"a": 1, "b": [2, {"c": "d"}]}})),
("description: \"Day, °C\" # note\n", json!({"description": "Day, °C"})),
("x: https://example.org/a#b\n", json!({"x": "https://example.org/a#b"})),
("list:\n - { uri: ./a.md, as: a }\n", json!({"list": [{"uri": "./a.md", "as": "a"}]})),
("a: |+\n x\n\nb: 1\n", json!({"a": "x\n\n", "b": 1})),
("a: >\n one\n two\n three\n", json!({"a": "one\n two\nthree\n"})),
("t: { u: https://example.org/a?b }\n", json!({"t": {"u": "https://example.org/a?b"}})),
("t: { values:[warm, plain] }\n", json!({"t": {"values": ["warm", "plain"]}})),
("t: {summary:? C}\n", json!({"t": {"summary:? C": null}})),
("k: [\n a,\n]\n", json!({"k": ["a"]})),
("k: [a,\n b]\n", json!({"k": ["a", "b"]})),
("[a,\nb]\n", json!(["a", "b"])),
];
for (src, want) in cases {
match load_yaml(src) {
Ok((got, _)) => assert!(same(&got, &want), "{src:?}: {got:?}, want {want:?}"),
Err(e) => panic!("{src:?}: {e}"),
}
}
}
#[test]
fn refusals() {
let cases = [
("a: 1\na: 2\n", "Map keys must be unique"),
("x: !!int abc\n", "Unresolved tag: tag:yaml.org,2002:int"),
("x: !!float 1\n", "Unresolved tag: tag:yaml.org,2002:float"),
("x: !foo bar\n", "the YAML tag !foo is not allowed"),
("x: !!binary aGk=\n", "the YAML tag tag:yaml.org,2002:binary is not allowed"),
("1: a\n", "mapping keys must be strings"),
("true: a\n", "mapping keys must be strings"),
("? [a]\n: b\n", "mapping keys must be strings"),
("a: 1\n---\n", SECOND_DOCUMENT),
("a: [1, 2\n---\nb: 1\n", SECOND_DOCUMENT),
("a: \"x\n---\ny\"\n", SECOND_DOCUMENT),
("a: b: c\n...\nb: 1\n", SECOND_DOCUMENT),
("...\n---\n", SECOND_DOCUMENT),
("x: .inf\n", "non-finite numbers are not allowed"),
("x: 1e400\n", "non-finite numbers are not allowed"),
("x: &a 1\ny: *a\n", "YAML anchors are not allowed"),
("notes: &a [1, *a]\n", "YAML anchors are not allowed"),
("a: {x: 1, x: 2}\n", "Map keys must be unique"),
("null: a\n~: b\n", "Map keys must be unique"),
("k: [1, {a: .nan}]\n", "non-finite numbers are not allowed"),
("? !!int 1\n: v\n", "mapping keys must be strings"),
("x: !!bool yes\n", "Unresolved tag: tag:yaml.org,2002:bool"),
("x: !!set {a: ~}\n", "the YAML tag tag:yaml.org,2002:set is not allowed"),
("x: !!omap [a: 1]\n", "the YAML tag tag:yaml.org,2002:omap is not allowed"),
("x: !!pairs [a: 1]\n", "the YAML tag tag:yaml.org,2002:pairs is not allowed"),
("x: !!merge <<\n", "the YAML tag tag:yaml.org,2002:merge is not allowed"),
("x: !<!bar> baz\n", "the YAML tag !bar is not allowed"),
("x: !local:t v\n", "the YAML tag !local:t is not allowed"),
("x: +.inf\n", "non-finite numbers are not allowed"),
("x: -.Inf\n", "non-finite numbers are not allowed"),
("x: .NaN\n", "non-finite numbers are not allowed"),
("x: !!float .nan\n", "non-finite numbers are not allowed"),
("x: !!str [a]\n", "Unresolved tag: tag:yaml.org,2002:str"),
("x: !!int [a]\n", "Unresolved tag: tag:yaml.org,2002:int"),
("x: !!null {}\n", "Unresolved tag: tag:yaml.org,2002:null"),
("x: !!str {a: 1}\n", "Unresolved tag: tag:yaml.org,2002:str"),
("x: !!binary [a]\n", "the YAML tag tag:yaml.org,2002:binary is not allowed"),
("x: !!float 0x1F\n", "Unresolved tag: tag:yaml.org,2002:float"),
("x: !!int .5\n", "Unresolved tag: tag:yaml.org,2002:int"),
("x: !!null 0\n", "Unresolved tag: tag:yaml.org,2002:null"),
("x: !!bool 1\n", "Unresolved tag: tag:yaml.org,2002:bool"),
("x: !!int ''\n", "Unresolved tag: tag:yaml.org,2002:int"),
("x: !!float ''\n", "Unresolved tag: tag:yaml.org,2002:float"),
("x: !!bool ''\n", "Unresolved tag: tag:yaml.org,2002:bool"),
("x: !!int null\n", "Unresolved tag: tag:yaml.org,2002:int"),
("x: !!null nUll\n", "Unresolved tag: tag:yaml.org,2002:null"),
("x: !!bool tRue\n", "Unresolved tag: tag:yaml.org,2002:bool"),
("x: !!int 0x\n", "Unresolved tag: tag:yaml.org,2002:int"),
("x: !!int 0o\n", "Unresolved tag: tag:yaml.org,2002:int"),
("x: !!int 0o8\n", "Unresolved tag: tag:yaml.org,2002:int"),
("x: !!int 0xG\n", "Unresolved tag: tag:yaml.org,2002:int"),
("x: !!int 1e3\n", "Unresolved tag: tag:yaml.org,2002:int"),
("x: !!float .\n", "Unresolved tag: tag:yaml.org,2002:float"),
("x: !!float 1.5.5\n", "Unresolved tag: tag:yaml.org,2002:float"),
("x: !!float 1e\n", "Unresolved tag: tag:yaml.org,2002:float"),
("x: !!float e3\n", "Unresolved tag: tag:yaml.org,2002:float"),
("x: !!float -.inf\n", "non-finite numbers are not allowed"),
("x: !!float .NAN\n", "non-finite numbers are not allowed"),
("x: !!float .Nan\n", "Unresolved tag: tag:yaml.org,2002:float"),
("x: !!float +.nan\n", "Unresolved tag: tag:yaml.org,2002:float"),
("x: !!float -.INF\n", "non-finite numbers are not allowed"),
("x: !!float .iNf\n", "Unresolved tag: tag:yaml.org,2002:float"),
("x: !!float 1_0.5\n", "Unresolved tag: tag:yaml.org,2002:float"),
("x: !!float 5e5.5\n", "Unresolved tag: tag:yaml.org,2002:float"),
("x: !!float ++5.5\n", "Unresolved tag: tag:yaml.org,2002:float"),
("x: !!float 5..5\n", "Unresolved tag: tag:yaml.org,2002:float"),
("x: !!float 0x1.8p1\n", "Unresolved tag: tag:yaml.org,2002:float"),
];
for (src, want) in cases {
match load_yaml(src) {
Ok((v, _)) => panic!("{src:?}: {v:?}, want {want:?}"),
Err(e) => assert_eq!(e, want, "{src:?}"),
}
}
for src in [
"x: [*a]\n",
"x: [a, b\n",
"x: 'a\n",
"a: b: c\n",
"a:\n - b\n c: d\n",
&"[".repeat(5000),
"k: [a,\nb]\n",
"k: \"a\nb\"\n",
"- \"a\nb\"\n",
"k:\n x: [a,\n b]\n",
"t: { ur\ni: x }\n",
"t: [, b]\n",
"t: [a,, b]\n",
] {
assert!(load_yaml(src).is_err(), "{src:?}");
}
}
#[test]
fn json_and_csv() {
assert_eq!(load_json(r#"{"a": {"b": 1, "b": 2}}"#).unwrap_err(), r#"the JSON key "b" is repeated"#);
assert!(load_json(r#"[{"a": 1}, {"a": 2}]"#).is_ok());
for (src, want) in [
("a,b\n1\n", "CSV record 2 has 1 fields; the header has 2"),
("a,a\n1,2\n", "CSV header names must be unique"),
("a,\n1,2\n", "CSV header names must be nonempty"),
("", "a CSV body needs a header record"),
("a\n\"x\n", "an unclosed quoted CSV field"),
("a,b\n1,x\"y\"\n", "a quote inside an unquoted CSV field (record 2)"),
] {
assert_eq!(load_csv(src).unwrap_err(), want, "{src:?}");
}
let (_, rows) = load_csv("a,b\r\n1,\"x,\"\"y\"\"\"\r\n").unwrap();
assert_eq!(rows, vec![vec!["1".to_string(), "x,\"y\"".to_string()]]);
}
}