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>
impl<T, W: Write> ToWriterBuilder<T, W>
Sourcepub fn compact(self) -> Self
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}"#);
Sourcepub fn pretty(self) -> Self
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
}"#);
Sourcepub fn leading_indent(self, s: impl Into<String>) -> Self
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
****}"#);
Sourcepub fn indent_by(self, s: impl Into<String>) -> Self
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
}"#);
Sourcepub fn format_context<F: Fn(&T, &mut W) -> Result + 'static>(self, f: F) -> Self
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"#);
Sourcepub fn add_custom_formatter<F: Fn(&Value<T>, &mut W) -> Option<Result> + 'static>(
self,
f: F,
) -> Self
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.
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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