Minor documentation improvements in emitter.

This commit is contained in:
Ethiraric 2024-11-11 15:31:34 +01:00
parent a8ee8c31c2
commit 2e24853ef3

View file

@ -1,10 +1,15 @@
//! YAML serialization helpers. //! YAML serialization helpers.
use crate::char_traits; use std::{
use crate::yaml::{Hash, Yaml}; convert::From,
use std::convert::From; error::Error,
use std::error::Error; fmt::{self, Display},
use std::fmt::{self, Display}; };
use crate::{
char_traits,
yaml::{Hash, Yaml},
};
/// An error when emitting YAML. /// An error when emitting YAML.
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
@ -49,16 +54,24 @@ impl From<fmt::Error> for EmitError {
/// ``` /// ```
#[allow(clippy::module_name_repetitions)] #[allow(clippy::module_name_repetitions)]
pub struct YamlEmitter<'a> { pub struct YamlEmitter<'a> {
/// The output stream in which we output YAML.
writer: &'a mut dyn fmt::Write, writer: &'a mut dyn fmt::Write,
best_indent: usize, /// Whether compact in-line notation is on or off.
///
/// See [`Self::compact`].
compact: bool, compact: bool,
/// The current non-flow nesting level.
level: isize, level: isize,
/// Whether we render multiline strings in literal style.
///
/// See [`Self::multiline_strings`].
multiline_strings: bool, multiline_strings: bool,
} }
/// A convenience alias for emitter functions that may fail without returning a value. /// A convenience alias for emitter functions that may fail without returning a value.
pub type EmitResult = Result<(), EmitError>; pub type EmitResult = Result<(), EmitError>;
/// Write the escaped double-quoted string into the given writer.
// from serialize::json // from serialize::json
fn escape_str(wr: &mut dyn fmt::Write, v: &str) -> Result<(), fmt::Error> { fn escape_str(wr: &mut dyn fmt::Write, v: &str) -> Result<(), fmt::Error> {
wr.write_str("\"")?; wr.write_str("\"")?;
@ -127,14 +140,13 @@ impl<'a> YamlEmitter<'a> {
pub fn new(writer: &'a mut dyn fmt::Write) -> Self { pub fn new(writer: &'a mut dyn fmt::Write) -> Self {
YamlEmitter { YamlEmitter {
writer, writer,
best_indent: 2,
compact: true, compact: true,
level: -1, level: -1,
multiline_strings: false, multiline_strings: false,
} }
} }
/// Set 'compact inline notation' on or off, as described for block /// Set 'compact in-line notation' on or off, as described for block
/// [sequences](http://www.yaml.org/spec/1.2/spec.html#id2797382) /// [sequences](http://www.yaml.org/spec/1.2/spec.html#id2797382)
/// and /// and
/// [mappings](http://www.yaml.org/spec/1.2/spec.html#id2798057). /// [mappings](http://www.yaml.org/spec/1.2/spec.html#id2798057).
@ -148,7 +160,9 @@ impl<'a> YamlEmitter<'a> {
self.compact = compact; self.compact = compact;
} }
/// Determine if this emitter is using 'compact inline notation'. /// Determine if this emitter is using 'compact in-line notation'.
///
/// See [`Self::compact`].
#[must_use] #[must_use]
pub fn is_compact(&self) -> bool { pub fn is_compact(&self) -> bool {
self.compact self.compact
@ -159,11 +173,10 @@ impl<'a> YamlEmitter<'a> {
/// # Examples /// # Examples
/// ///
/// ```rust /// ```rust
/// use saphyr::{Yaml, YamlEmitter}; /// # use saphyr::{Yaml, YamlEmitter};
/// /// #
/// let input = r#"{foo: "bar!\nbar!", baz: 42}"#; /// let input = r#"{foo: "bar\nbar", baz: 42}"#;
/// let parsed = Yaml::load_from_str(input).unwrap(); /// let parsed = Yaml::load_from_str(input).unwrap();
/// eprintln!("{:?}", parsed);
/// ///
/// let mut output = String::new(); /// let mut output = String::new();
/// let mut emitter = YamlEmitter::new(&mut output); /// let mut emitter = YamlEmitter::new(&mut output);
@ -172,8 +185,8 @@ impl<'a> YamlEmitter<'a> {
/// assert_eq!(output.as_str(), "\ /// assert_eq!(output.as_str(), "\
/// --- /// ---
/// foo: |- /// foo: |-
/// bar! /// bar
/// bar! /// bar
/// baz: 42"); /// baz: 42");
/// ``` /// ```
/// ///
@ -183,14 +196,17 @@ impl<'a> YamlEmitter<'a> {
} }
/// Determine if this emitter will emit multiline strings when appropriate. /// Determine if this emitter will emit multiline strings when appropriate.
///
/// See [`Self::multiline_strings`].
#[must_use] #[must_use]
pub fn is_multiline_strings(&self) -> bool { pub fn is_multiline_strings(&self) -> bool {
self.multiline_strings self.multiline_strings
} }
/// Dump Yaml to an output stream. /// Dump Yaml to an output stream.
///
/// # Errors /// # Errors
/// Returns `EmitError` when an error occurs. /// Returns [`EmitError`] when an error occurs.
pub fn dump(&mut self, doc: &Yaml) -> EmitResult { pub fn dump(&mut self, doc: &Yaml) -> EmitResult {
// write DocumentStart // write DocumentStart
writeln!(self.writer, "---")?; writeln!(self.writer, "---")?;
@ -203,9 +219,7 @@ impl<'a> YamlEmitter<'a> {
return Ok(()); return Ok(());
} }
for _ in 0..self.level { for _ in 0..self.level {
for _ in 0..self.best_indent { write!(self.writer, " ")?;
write!(self.writer, " ")?;
}
} }
Ok(()) Ok(())
} }