From 87e46b68a2a2112039767b6dccd565cbf3d8bc08 Mon Sep 17 00:00:00 2001 From: Ethiraric Date: Mon, 11 Nov 2024 15:34:17 +0100 Subject: [PATCH] Reorder declarations. --- saphyr/src/emitter.rs | 188 +++++++++++++++++++++--------------------- 1 file changed, 94 insertions(+), 94 deletions(-) diff --git a/saphyr/src/emitter.rs b/saphyr/src/emitter.rs index 606f4a2..d94637f 100644 --- a/saphyr/src/emitter.rs +++ b/saphyr/src/emitter.rs @@ -11,33 +11,6 @@ use crate::{ yaml::{Hash, Yaml}, }; -/// An error when emitting YAML. -#[derive(Copy, Clone, Debug)] -pub enum EmitError { - /// A formatting error. - FmtError(fmt::Error), -} - -impl Error for EmitError { - fn cause(&self) -> Option<&dyn Error> { - None - } -} - -impl Display for EmitError { - fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - match *self { - EmitError::FmtError(ref err) => Display::fmt(err, formatter), - } - } -} - -impl From for EmitError { - fn from(f: fmt::Error) -> Self { - EmitError::FmtError(f) - } -} - /// The YAML serializer. /// /// ``` @@ -68,73 +41,6 @@ pub struct YamlEmitter<'a> { multiline_strings: bool, } -/// A convenience alias for emitter functions that may fail without returning a value. -pub type EmitResult = Result<(), EmitError>; - -/// Write the escaped double-quoted string into the given writer. -// from serialize::json -fn escape_str(wr: &mut dyn fmt::Write, v: &str) -> Result<(), fmt::Error> { - wr.write_str("\"")?; - - let mut start = 0; - - for (i, byte) in v.bytes().enumerate() { - let escaped = match byte { - b'"' => "\\\"", - b'\\' => "\\\\", - b'\x00' => "\\u0000", - b'\x01' => "\\u0001", - b'\x02' => "\\u0002", - b'\x03' => "\\u0003", - b'\x04' => "\\u0004", - b'\x05' => "\\u0005", - b'\x06' => "\\u0006", - b'\x07' => "\\u0007", - b'\x08' => "\\b", - b'\t' => "\\t", - b'\n' => "\\n", - b'\x0b' => "\\u000b", - b'\x0c' => "\\f", - b'\r' => "\\r", - b'\x0e' => "\\u000e", - b'\x0f' => "\\u000f", - b'\x10' => "\\u0010", - b'\x11' => "\\u0011", - b'\x12' => "\\u0012", - b'\x13' => "\\u0013", - b'\x14' => "\\u0014", - b'\x15' => "\\u0015", - b'\x16' => "\\u0016", - b'\x17' => "\\u0017", - b'\x18' => "\\u0018", - b'\x19' => "\\u0019", - b'\x1a' => "\\u001a", - b'\x1b' => "\\u001b", - b'\x1c' => "\\u001c", - b'\x1d' => "\\u001d", - b'\x1e' => "\\u001e", - b'\x1f' => "\\u001f", - b'\x7f' => "\\u007f", - _ => continue, - }; - - if start < i { - wr.write_str(&v[start..i])?; - } - - wr.write_str(escaped)?; - - start = i + 1; - } - - if start != v.len() { - wr.write_str(&v[start..])?; - } - - wr.write_str("\"")?; - Ok(()) -} - impl<'a> YamlEmitter<'a> { /// Create a new emitter serializing into `writer`. pub fn new(writer: &'a mut dyn fmt::Write) -> Self { @@ -369,6 +275,36 @@ impl<'a> YamlEmitter<'a> { } } +/// A convenience alias for emitter functions that may fail without returning a value. +pub type EmitResult = Result<(), EmitError>; + +/// An error when emitting YAML. +#[derive(Copy, Clone, Debug)] +pub enum EmitError { + /// A formatting error. + FmtError(fmt::Error), +} + +impl Error for EmitError { + fn cause(&self) -> Option<&dyn Error> { + None + } +} + +impl Display for EmitError { + fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + match *self { + EmitError::FmtError(ref err) => Display::fmt(err, formatter), + } + } +} + +impl From for EmitError { + fn from(f: fmt::Error) -> Self { + EmitError::FmtError(f) + } +} + /// Check if the string requires quoting. /// Strings starting with any of the following characters must be quoted. /// :, &, *, ?, |, -, <, >, =, !, %, @ @@ -433,6 +369,70 @@ fn need_quotes(string: &str) -> bool { || string.parse::().is_ok() } +/// Write the escaped double-quoted string into the given writer. +// from serialize::json +fn escape_str(wr: &mut dyn fmt::Write, v: &str) -> Result<(), fmt::Error> { + wr.write_str("\"")?; + + let mut start = 0; + + for (i, byte) in v.bytes().enumerate() { + let escaped = match byte { + b'"' => "\\\"", + b'\\' => "\\\\", + b'\x00' => "\\u0000", + b'\x01' => "\\u0001", + b'\x02' => "\\u0002", + b'\x03' => "\\u0003", + b'\x04' => "\\u0004", + b'\x05' => "\\u0005", + b'\x06' => "\\u0006", + b'\x07' => "\\u0007", + b'\x08' => "\\b", + b'\t' => "\\t", + b'\n' => "\\n", + b'\x0b' => "\\u000b", + b'\x0c' => "\\f", + b'\r' => "\\r", + b'\x0e' => "\\u000e", + b'\x0f' => "\\u000f", + b'\x10' => "\\u0010", + b'\x11' => "\\u0011", + b'\x12' => "\\u0012", + b'\x13' => "\\u0013", + b'\x14' => "\\u0014", + b'\x15' => "\\u0015", + b'\x16' => "\\u0016", + b'\x17' => "\\u0017", + b'\x18' => "\\u0018", + b'\x19' => "\\u0019", + b'\x1a' => "\\u001a", + b'\x1b' => "\\u001b", + b'\x1c' => "\\u001c", + b'\x1d' => "\\u001d", + b'\x1e' => "\\u001e", + b'\x1f' => "\\u001f", + b'\x7f' => "\\u007f", + _ => continue, + }; + + if start < i { + wr.write_str(&v[start..i])?; + } + + wr.write_str(escaped)?; + + start = i + 1; + } + + if start != v.len() { + wr.write_str(&v[start..])?; + } + + wr.write_str("\"")?; + Ok(()) +} + #[cfg(test)] mod test { use crate::Yaml;