ToWriterBuilder

Struct ToWriterBuilder 

Source
pub struct ToWriterBuilder<T, W> { /* private fields */ }
Expand description

A struct which will try to format a Value and write the output to a writer. This can be configured with custom parsers to extend how to write out the value.

Implementations§

Source§

impl<T, W: Write> ToWriterBuilder<T, W>

Source

pub fn compact(self) -> Self

Write the crate::Value to a compact string, omitting spaces.

§Example
use scale_value::{value,Value};
use scale_value::stringify::to_writer_custom;

let val = value!({
    foo: (1,2,3,4,5),
    bar: true
});

let mut s = String::new();

to_writer_custom()
    .compact()
    .write(&val, &mut s)
    .unwrap();

assert_eq!(s, r#"{foo:(1,2,3,4,5),bar:true}"#);
Source

pub fn pretty(self) -> Self

Write the crate::Value to a pretty spaced/indented string.

§Example
use scale_value::{value, Value};
use scale_value::stringify::to_writer_custom;

let val = value!({
    foo: (1,2,3,4,5),
    bar: true
});

let mut s = String::new();

to_writer_custom()
    .pretty()
    .write(&val, &mut s)
    .unwrap();

assert_eq!(s, r#"{
  foo: (
    1,
    2,
    3,
    4,
    5
  ),
  bar: true
}"#);
Source

pub fn leading_indent(self, s: impl Into<String>) -> Self

Write the crate::Value to a pretty spaced string, indenting each new line by applying Self::indent_by() the number of times given here. Defaults to nothing.

§Example
use scale_value::{value, Value};
use scale_value::stringify::to_writer_custom;

let val = value!({
    foo: (1,2,3,4,5),
    bar: true
});

let mut s = String::new();

to_writer_custom()
    .pretty()
    .leading_indent("****")
    .write(&val, &mut s)
    .unwrap();

assert_eq!(s, r#"{
****  foo: (
****    1,
****    2,
****    3,
****    4,
****    5
****  ),
****  bar: true
****}"#);
Source

pub fn indent_by(self, s: impl Into<String>) -> Self

When using Self::pretty(), this defines the characters used to add each step of indentation to newlines. Defaults to two spaces.

§Example
use scale_value::{value, Value};
use scale_value::stringify::to_writer_custom;

let val = value!({
    foo: (1,2,3,4,5),
    bar: true
});

let mut s = String::new();

to_writer_custom()
    .pretty()
    .indent_by("**")
    .write(&val, &mut s)
    .unwrap();

assert_eq!(s, r#"{
**foo: (
****1,
****2,
****3,
****4,
****5
**),
**bar: true
}"#);
Source

pub fn format_context<F: Fn(&T, &mut W) -> Result + 'static>(self, f: F) -> Self

Write the context contained in each value out, too. This is prepended to each value within angle brackets (< and >).

§Warning

When this is used, the resulting outpuot cannot then be parsed back into Value (in part since the user can output arbitrary content for the context). Nevertheless, writing the context can be useful for debugging errors and providing more verbose output.

§Example
use scale_value::{value, Value, ValueDef, Primitive};
use scale_value::stringify::to_writer_custom;
use std::fmt::Write;

let val = Value {
    value: ValueDef::Primitive(Primitive::Bool(true)),
    context: "hi"
};

let mut s = String::new();

to_writer_custom()
    .format_context(|ctx, w: &mut &mut String| write!(w, "context: {ctx}"))
    .write(&val, &mut s)
    .unwrap();

assert_eq!(s, r#"<context: hi> true"#);
Source

pub fn add_custom_formatter<F: Fn(&Value<T>, &mut W) -> Option<Result> + 'static>( self, f: F, ) -> Self

Add a custom formatter (for example, crate::stringify::custom_formatters::format_hex). Custom formatters have the opportunity to read the value at each stage, and:

  • Should output None if they do not wish to override the standard formatting (in this case, they should also avoid writing anything to the provided writer).
  • Should output Some(core::fmt::Result) if they decide to override the default formatting at this point.

Custom formatters are tried in the order that they are added here, and when one decides to write output (signalled by returning Some(..)), no others will be tried. Thus, the order in which they are added is important.

Source

pub fn write(&self, value: &Value<T>, writer: W) -> Result

Write some value to the provided writer, using the currently configured options.

Auto Trait Implementations§

§

impl<T, W> Freeze for ToWriterBuilder<T, W>

§

impl<T, W> !RefUnwindSafe for ToWriterBuilder<T, W>

§

impl<T, W> !Send for ToWriterBuilder<T, W>

§

impl<T, W> !Sync for ToWriterBuilder<T, W>

§

impl<T, W> Unpin for ToWriterBuilder<T, W>

§

impl<T, W> !UnwindSafe for ToWriterBuilder<T, W>

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> JsonSchemaMaybe for T