Skip to main content

Writer

Struct Writer 

Source
pub struct Writer { /* private fields */ }
Expand description

Writes the text form piece by piece, without a Value in between. A list or map is given its length, then that many values (a map’s each after its key); it closes by itself after the last, so there are no end calls. The pretty form puts one entry per line.

let mut w = substrate_data::text::Writer::new();
w.map(2).key("id").str("docs/intro").key("pages").list(2);
w.int(1).int(2);
assert_eq!(w.finish(), r#"{"id":"docs/intro","pages":[1,2]}"#);

Implementations§

Source§

impl Writer

Source

pub fn new() -> Writer

A writer of compact text.

Source

pub fn pretty() -> Writer

A writer of text indented by two spaces, one entry per line, ending with a newline.

Source

pub fn null(&mut self) -> &mut Writer

Null.

Source

pub fn bool(&mut self, b: bool) -> &mut Writer

A bool.

Source

pub fn int(&mut self, n: i64) -> &mut Writer

An integer.

Source

pub fn float(&mut self, f: f64) -> &mut Writer

A float, in the shortest spelling that reads back as it and as a float; null for a NaN or an infinity.

Source

pub fn str(&mut self, s: &str) -> &mut Writer

A string, as spelled (the text form has no other kind of text).

Source

pub fn reference(&mut self, kind: &str, id: &str) -> &mut Writer

A reference, as its string form @kind:id.

Source

pub fn bytes(&mut self, bytes: &[u8]) -> &mut Writer

Bytes, as their string form @bytes:<base64>.

Source

pub fn list(&mut self, len: usize) -> &mut Writer

A list of len values, which follow.

Source

pub fn map(&mut self, len: usize) -> &mut Writer

A map of len entries (key, then its value), which follow.

Source

pub fn key(&mut self, key: &str) -> &mut Writer

The key of the map’s next entry; its value follows.

Source

pub fn value(&mut self, value: &Value) -> &mut Writer

A whole Value (typed values in their string forms).

Source

pub fn finish(self) -> String

The text written.

Trait Implementations§

Source§

impl Debug for Writer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for Writer

Source§

fn default() -> Writer

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.