use std::collections::BTreeMap;
use std::fmt::{self, Write as _};
use std::iter;
use serde::de::{self, Deserializer, Visitor};
use serde::{Deserialize, Serialize};
use crate::Error;
use crate::index::{Index, Style};
use crate::text::{LF, indent, join, spaced, terminated};
mod scan;
#[cfg(test)]
mod tests;
use scan::Scan;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum TextStyle {
#[default]
Auto,
Folded,
Literal,
Line,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum Quotes {
#[default]
Auto,
Single,
Double,
}
impl Quotes {
fn style(self, scan: Scan) -> Style {
let prefers_single = match self {
Self::Auto => {
scan.apostrophes == 0
|| (scan.quotes > 0 && scan.apostrophes <= scan.quotes + scan.backslashes)
}
Self::Single => true,
Self::Double => false,
};
if prefers_single && !scan.double_only() {
Style::SingleQuoted
} else {
Style::DoubleQuoted
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Context {
Block,
Flow,
}
fn double_only(c: char) -> bool {
(c != '\t' && c.is_control())
|| matches!(
c,
'\u{2028}' | '\u{2029}' | '\u{feff}' | '\u{fffe}' | '\u{ffff}'
)
}
#[derive(Clone, Debug)]
pub(crate) enum Rendered {
Scalar { head: String, body: Vec<String> },
Sequence(Vec<String>),
Mapping(Vec<String>),
}
impl Rendered {
fn inline(head: impl Into<String>) -> Self {
Self::Scalar {
head: head.into(),
body: Vec::new(),
}
}
pub(crate) fn after_key(&self, key_column: usize, compact: bool) -> String {
self.after_key_at(key_column, key_column + 2, compact)
}
pub(crate) fn after_key_at(
&self,
key_column: usize,
body_column: usize,
compact: bool,
) -> String {
self.after_key_with(key_column, body_column, compact, "", "")
}
pub(crate) fn after_key_with(
&self,
key_column: usize,
body_column: usize,
compact: bool,
properties: &str,
comment: &str,
) -> String {
let (first, rest, column) = match self {
Self::Scalar { head, body } => {
(spaced(&[properties, head, comment]), body, body_column)
}
Self::Sequence(lines) if compact => (spaced(&[properties, comment]), lines, key_column),
Self::Sequence(lines) | Self::Mapping(lines) => {
(spaced(&[properties, comment]), lines, key_column + 2)
}
};
let first = if first.is_empty() {
first
} else {
format!(" {first}")
};
let lines: Vec<String> = iter::once(first).chain(indent(rest, column)).collect();
join(&lines)
}
pub(crate) fn after_dash(&self, dash_column: usize) -> String {
let (first, rest) = match self {
Self::Scalar { head, body } => (head.as_str(), body.as_slice()),
Self::Sequence(lines) | Self::Mapping(lines) => match lines.split_first() {
Some((first, rest)) => (first.as_str(), rest),
None => return String::new(),
},
};
let lines: Vec<String> = iter::once(first.to_owned())
.chain(indent(rest, dash_column + 2))
.collect();
join(&lines)
}
pub(crate) fn at_root(&self, column: usize) -> String {
match self {
Self::Scalar { .. } => self.after_dash(column),
Self::Sequence(lines) | Self::Mapping(lines) => match lines.split_first() {
Some((first, rest)) => {
let lines: Vec<String> = iter::once(first.clone())
.chain(indent(rest, column))
.collect();
join(&lines)
}
None => String::new(),
},
}
}
pub(crate) fn inline_head(&self) -> Option<&str> {
match self {
Self::Scalar { head, body } if body.is_empty() => Some(head),
Self::Scalar { .. } | Self::Sequence(_) | Self::Mapping(_) => None,
}
}
pub(crate) fn fits_core_tag(&self, name: &str) -> bool {
match (name, self) {
("seq", Self::Sequence(_)) | ("map", Self::Mapping(_)) => true,
("seq", Self::Scalar { head, body }) => body.is_empty() && head.starts_with('['),
("map", Self::Scalar { head, body }) => body.is_empty() && head.starts_with('{'),
("str", Self::Scalar { head, body }) => !body.is_empty() || read_string(head).is_some(),
(scalar, Self::Scalar { head, body }) if body.is_empty() => matches!(
(scalar, read_scalar(head)),
("int", Some(Captured::Int))
| ("float", Some(Captured::Float | Captured::Int))
| ("bool", Some(Captured::Bool))
| ("null", Some(Captured::Null))
),
_ => false,
}
}
pub(crate) fn has_indicator(&self) -> bool {
matches!(self, Self::Scalar { head, .. }
if head.starts_with(['>', '|']) && head.contains(|c: char| c.is_ascii_digit()))
}
}
pub(crate) fn value<T: Serialize + ?Sized>(
value: &T,
preferred: Option<Style>,
compact: bool,
quotes: Quotes,
context: Context,
) -> Result<Rendered, Error> {
let yaml = serialize(value, compact)?;
let index = Index::build(&yaml)?;
let root = index.nodes.first().ok_or_else(|| Error::Serialize {
message: "the value serialized to nothing".to_owned(),
})?;
let lines = || yaml.lines().map(str::to_owned).collect();
match root.style {
Style::BlockSequence => Ok(Rendered::Sequence(lines())),
Style::BlockMapping => Ok(Rendered::Mapping(lines())),
Style::FlowSequence | Style::FlowMapping | Style::Alias => {
Ok(Rendered::inline(yaml.trim()))
}
Style::Plain
| Style::SingleQuoted
| Style::DoubleQuoted
| Style::Literal
| Style::Folded => match read_string(&yaml) {
Some(decoded) => text(&decoded, TextStyle::Auto, quotes, preferred, context),
None => Ok(Rendered::inline(yaml.trim())),
},
}
}
pub(crate) fn flow_items<T: Serialize + ?Sized>(
value: &T,
quotes: Quotes,
) -> Result<Option<Vec<String>>, Error> {
let yaml = serialize(value, false)?;
let index = Index::build(&yaml)?;
let Some(root) = index.nodes.first() else {
return Ok(None);
};
match root.style {
Style::FlowSequence if root.children.is_empty() => Ok(Some(Vec::new())),
Style::BlockSequence => {
let mut items = Vec::with_capacity(root.children.len());
for &child in &root.children {
let node = &index.nodes[child];
let written = yaml[node.value.clone()].trim();
let item = match node.style {
Style::Plain | Style::SingleQuoted | Style::DoubleQuoted => {
match read_string(written) {
Some(decoded) => {
text(&decoded, TextStyle::Line, quotes, None, Context::Flow)?
.inline_head()
.map(str::to_owned)
}
None => Some(written.to_owned()),
}
}
Style::Literal
| Style::Folded
| Style::Alias
| Style::BlockSequence
| Style::FlowSequence
| Style::BlockMapping
| Style::FlowMapping => None,
};
match item {
Some(item) => items.push(item),
None => return Ok(None),
}
}
Ok(Some(items))
}
Style::Plain
| Style::SingleQuoted
| Style::DoubleQuoted
| Style::Literal
| Style::Folded
| Style::Alias
| Style::FlowSequence
| Style::BlockMapping
| Style::FlowMapping => Ok(None),
}
}
fn serialize<T: Serialize + ?Sized>(value: &T, compact: bool) -> Result<String, Error> {
let options = serde_saphyr::ser_options! {
indent_step: 2,
compact_list_indent: compact,
};
serde_saphyr::to_string_with_options(&value, options).map_err(|error| Error::Serialize {
message: error.to_string(),
})
}
pub(crate) fn text(
value: &str,
style: TextStyle,
quotes: Quotes,
preferred: Option<Style>,
context: Context,
) -> Result<Rendered, Error> {
let blocks = style != TextStyle::Line && context == Context::Block;
let requested = match style {
TextStyle::Auto | TextStyle::Line => None,
TextStyle::Folded => Some(Style::Folded),
TextStyle::Literal => Some(Style::Literal),
};
let block = |style: &Style| blocks || !matches!(style, Style::Literal | Style::Folded);
let preferred = preferred
.filter(|candidate| style != TextStyle::Line && candidate.is_scalar() && block(candidate));
let multiline = (blocks && value.contains(LF)).then_some(Style::Literal);
let scan = Scan::of(value, context);
requested
.filter(block)
.into_iter()
.chain(preferred)
.chain(multiline)
.chain([Style::Plain, quotes.style(scan), Style::DoubleQuoted])
.find_map(|candidate| written(value, candidate, scan, context))
.ok_or_else(|| Error::Serialize {
message: "no scalar style reads back as this string".to_owned(),
})
}
#[must_use]
pub fn line(value: &str, quotes: Quotes, flow: bool) -> String {
let context = if flow { Context::Flow } else { Context::Block };
text(value, TextStyle::Line, quotes, None, context)
.ok()
.and_then(|rendered| rendered.inline_head().map(str::to_owned))
.unwrap_or_default()
}
fn written(value: &str, style: Style, scan: Scan, context: Context) -> Option<Rendered> {
let holds = match style {
Style::Plain => scan.plain,
Style::SingleQuoted => !scan.double_only(),
Style::DoubleQuoted => true,
Style::Literal => scan.literal,
Style::Folded => scan.folded,
Style::Alias
| Style::BlockSequence
| Style::FlowSequence
| Style::BlockMapping
| Style::FlowMapping => false,
};
debug_assert_eq!(
holds,
proven(value, style, context),
"{style:?} in {context:?} for {value:?}"
);
if !holds {
return None;
}
laid_out(value, style)
}
fn proven(value: &str, style: Style, context: Context) -> bool {
let escaped = |c: char| double_only(c) && !(style == Style::Literal && c == LF);
let carried = style == Style::DoubleQuoted || !value.chars().any(escaped);
let block = matches!(style, Style::Literal | Style::Folded);
carried
&& !(block && context == Context::Flow)
&& laid_out(value, style).is_some_and(|rendered| reads_back(&rendered, value, context))
}
fn laid_out(value: &str, style: Style) -> Option<Rendered> {
let one_line = !value.contains(LF);
match style {
Style::Plain if one_line && !value.is_empty() => Some(Rendered::inline(value)),
Style::SingleQuoted => Some(Rendered::inline(single(value))),
Style::DoubleQuoted => Some(Rendered::inline(double(value))),
Style::Folded if one_line && !value.is_empty() => Some(Rendered::Scalar {
head: format!(">{}-", indicator(value)),
body: vec![value.to_owned()],
}),
Style::Literal if !value.is_empty() => Some(literal(value)),
Style::Plain
| Style::Literal
| Style::Folded
| Style::Alias
| Style::BlockSequence
| Style::FlowSequence
| Style::BlockMapping
| Style::FlowMapping => None,
}
}
fn literal(value: &str) -> Rendered {
let content = value.trim_end_matches(LF);
let trailing = value.len() - content.len();
let chomp = match trailing {
0 => "-",
1 => "",
_ => "+",
};
let body = content
.split(LF)
.map(str::to_owned)
.chain(iter::repeat_n(String::new(), trailing.saturating_sub(1)))
.collect();
Rendered::Scalar {
head: format!("|{}{chomp}", indicator(value)),
body,
}
}
fn indicator(value: &str) -> &'static str {
if value.starts_with(' ') { "2" } else { "" }
}
fn single(value: &str) -> String {
format!("'{}'", value.replace('\'', "''"))
}
fn double(value: &str) -> String {
format!("\"{}\"", escape(value))
}
fn escape(value: &str) -> String {
let mut out = String::with_capacity(value.len());
for c in value.chars() {
match c {
'\\' => out.push_str(r"\\"),
'"' => out.push_str(r#"\""#),
LF => out.push_str(r"\n"),
'\t' => out.push_str(r"\t"),
'\r' => out.push_str(r"\r"),
'\0' => out.push_str(r"\0"),
c if double_only(c) => {
let _ = write!(out, r"\u{:04X}", u32::from(c));
}
c => out.push(c),
}
}
out
}
fn reader() -> serde_saphyr::Options {
serde_saphyr::options! {
strict_booleans: true,
}
}
fn reads_back(rendered: &Rendered, expected: &str, context: Context) -> bool {
match context {
Context::Block => {
let probe = terminated(&format!("v:{}", rendered.after_key(0, false)));
serde_saphyr::from_str_with_options::<BTreeMap<String, Captured>>(&probe, reader())
.is_ok_and(|map| {
map.len() == 1
&& map.get("v").is_some_and(
|value| matches!(value, Captured::Text(text) if text == expected),
)
})
}
Context::Flow => rendered.inline_head().is_some_and(|head| {
let probe = terminated(&format!("v: [{head}]"));
serde_saphyr::from_str_with_options::<BTreeMap<String, Vec<Captured>>>(&probe, reader())
.is_ok_and(|map| {
map.len() == 1
&& matches!(
map.get("v").map(Vec::as_slice),
Some([Captured::Text(text)]) if text == expected
)
})
}),
}
}
fn read_string(yaml: &str) -> Option<String> {
match read_scalar(yaml)? {
Captured::Text(text) => Some(text),
Captured::Int | Captured::Float | Captured::Bool | Captured::Null => None,
}
}
fn read_scalar(yaml: &str) -> Option<Captured> {
serde_saphyr::from_str_with_options::<Captured>(yaml, reader()).ok()
}
enum Captured {
Text(String),
Int,
Float,
Bool,
Null,
}
impl<'de> Deserialize<'de> for Captured {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
struct Probe;
impl Visitor<'_> for Probe {
type Value = Captured;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("any scalar")
}
fn visit_str<E: de::Error>(self, value: &str) -> Result<Captured, E> {
Ok(Captured::Text(value.to_owned()))
}
fn visit_bool<E: de::Error>(self, _: bool) -> Result<Captured, E> {
Ok(Captured::Bool)
}
fn visit_i64<E: de::Error>(self, _: i64) -> Result<Captured, E> {
Ok(Captured::Int)
}
fn visit_u64<E: de::Error>(self, _: u64) -> Result<Captured, E> {
Ok(Captured::Int)
}
fn visit_f64<E: de::Error>(self, _: f64) -> Result<Captured, E> {
Ok(Captured::Float)
}
fn visit_unit<E: de::Error>(self) -> Result<Captured, E> {
Ok(Captured::Null)
}
fn visit_none<E: de::Error>(self) -> Result<Captured, E> {
Ok(Captured::Null)
}
}
deserializer.deserialize_any(Probe)
}
}