Skip to main content

Emit

Trait Emit 

Source
pub trait Emit {
    // Required methods
    fn emit(&self, ctx: &EmitCtx) -> Result<String>;
    fn expected_value(&self) -> Result<Value>;
}
Expand description

A value that can be spliced into a document by the auto-formatting mutators.

Implementors provide two halves of one contract:

  • emit — the YAML spelling to splice, quoted and escaped so that it re-parses to this value at ctx’s site.
  • expected_value — the typed value that spelling must load back as.

The mutators check the second against the document after splicing the first, so an implementation whose two halves disagree causes a refusal with the document left unchanged, never a silent corruption.

Implementations ship for str, String, bool, the integer and float primitives, Value, and any reference to one of those. A Serialize type is emitted by converting it first: doc.push_back_value(path, &noyalib::to_value(&my_struct)?).

§Examples

use noyalib::cst::{Emit, EmitCtx};
use noyalib::{FlowStyle, ScalarStyle, Value};

let ctx = EmitCtx::new(ScalarStyle::Plain, FlowStyle::Block, 2, 0);
assert_eq!(42_i64.emit(&ctx).unwrap(), "42");
assert_eq!(42_i64.expected_value().unwrap(), Value::from(42_i64));

Required Methods§

Source

fn emit(&self, ctx: &EmitCtx) -> Result<String>

The YAML spelling of this value at ctx’s site.

§Errors

Returns Error::Parse when the value has no representation the emitter can produce for that site (for example a tagged scalar, whose tag the scalar emitter would drop), and propagates serializer errors for collections.

Source

fn expected_value(&self) -> Result<Value>

The typed value emit’s output must re-parse to.

§Errors

Returns Error::Parse for the same cases as emit — a value that cannot be emitted has no meaningful oracle.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl Emit for String

Source§

impl Emit for bool

Source§

impl Emit for f32

Source§

impl Emit for f64

Source§

impl Emit for i8

Source§

impl Emit for i16

Source§

impl Emit for i32

Source§

impl Emit for i64

Source§

impl Emit for isize

Source§

impl Emit for str

Source§

impl Emit for u8

Source§

impl Emit for u16

Source§

impl Emit for u32

Source§

impl Emit for u64

Source§

impl Emit for usize

Source§

impl<T: Emit + ?Sized> Emit for &T

Implementors§