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};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum TextStyle {
#[default]
Auto,
Folded,
Literal,
}
#[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),
_ => 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,
) -> 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, preferred),
None => Ok(Rendered::inline(yaml.trim())),
},
}
}
pub(crate) fn flow_items<T: Serialize + ?Sized>(value: &T) -> 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::Auto, None)?
.inline_head()
.map(flow_safe),
None => Some(written.to_owned()),
}
}
_ => None,
};
match item {
Some(item) => items.push(item),
None => return Ok(None),
}
}
Ok(Some(items))
}
_ => 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 flow_safe(head: &str) -> String {
if head.starts_with(['\'', '"', '[', '{']) || !head.contains([',', '[', ']', '{', '}']) {
head.to_owned()
} else if head.contains('\'') {
format!("\"{}\"", escape(head))
} else {
format!("'{head}'")
}
}
pub(crate) fn text(
value: &str,
style: TextStyle,
preferred: Option<Style>,
) -> Result<Rendered, Error> {
let requested = match style {
TextStyle::Auto => None,
TextStyle::Folded => Some(Style::Folded),
TextStyle::Literal => Some(Style::Literal),
};
let multiline = value.contains(LF).then_some(Style::Literal);
requested
.into_iter()
.chain(preferred.filter(|style| style.is_scalar()))
.chain(multiline)
.chain([Style::Plain, Style::SingleQuoted, Style::DoubleQuoted])
.filter_map(|candidate| scalar(value, candidate))
.find(|rendered| reads_back(rendered, value))
.ok_or_else(|| Error::Serialize {
message: "no scalar style reads back as this string".to_owned(),
})
}
fn scalar(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 if one_line => {
let quoted = format!("'{}'", value.replace('\'', "''"));
Some(Rendered::inline(quoted))
}
Style::DoubleQuoted => Some(Rendered::inline(format!("\"{}\"", escape(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)),
_ => 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 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 c.is_control() || matches!(c, '\u{85}' | '\u{2028}' | '\u{2029}' | '\u{feff}') => {
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) -> bool {
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))
},
)
}
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)
}
}