Struct kserd::fmt::FormattingConfig[][src]

pub struct FormattingConfig {
    pub id_on_primitives: bool,
    pub id_on_tuples: bool,
    pub id_on_containers: bool,
    pub id_on_seqs: bool,
    pub id_on_maps: bool,
    pub width_limit: Option<u16>,
}

Configuration of how a Kserd is to be formatted.

Generally the default values should be used when displaying a Kserd. If the Kserd is being used for serialisation of data then the default is not recommended. There are constructor methods that can be used that better capture round-trip data conversions.

The likely field that would be changed is width_limit. Altering this value can allow for more or less verbosity as to the formatter’s taste.

Example

use kserd::fmt::FormattingConfig;

let kserd = Kserd::new_cntr(vec![
    ("a", Kserd::new_num(101)),
    ("b", Kserd::new_num(3.14)),
]).unwrap();

let fmt = kserd.as_str_with_config(FormattingConfig {
    width_limit: None, // None means no limit, so all inline
    ..Default::default()
});
assert_eq!(&fmt, "(a = 101, b = 3.14)");

let fmt = kserd.as_str_with_config(FormattingConfig {
    width_limit: Some(16), // This force a concise layout
    ..Default::default()
});
assert_eq!(&fmt, "(
    a = 101
    b = 3.14
)"
);

let fmt = kserd.as_str_with_config(FormattingConfig {
    width_limit: Some(0), // Always will be verbose where applicable
    ..Default::default()
});
assert_eq!(&fmt, "a = 101
b = 3.14
");

Fields

id_on_primitives: bool

Display the identity on primitive values. Default is false.

Example

use kserd::fmt::FormattingConfig;

let kserd = Kserd::new_cntr(vec![
    ("unit", Kserd::new_unit()),
    ("bool", Kserd::new_bool(true)),
    ("number", Kserd::new_num(1)),
    ("float", Kserd::new_num(3.14)),
    ("str", Kserd::new_str("Hello")),
    ("barr", Kserd::new_barr(&[0, 1, 2])),
    ]).unwrap();

let show_prims = FormattingConfig {
    id_on_primitives: true,
    width_limit: Some(0),
    ..Default::default()
};

assert_eq!(&kserd.as_str_with_config(show_prims),
r#"barr = <barr> b91':CQA'
bool = <bool> true
float = <f64> 3.14
number = <i32> 1
str = <str> "Hello"
unit = ()
"#);
id_on_tuples: bool

Display the identity on tuple values. Default is true.

Example

use kserd::fmt::FormattingConfig;

let kserd = Kserd::with_id("a-tuple", Value::Tuple(vec![])).unwrap();

let show_tuples = FormattingConfig {
    id_on_tuples: true,
    ..Default::default()
};

assert_eq!(
    &kserd.as_str_with_config(show_tuples),
    "a-tuple()"
);
id_on_containers: bool

Display the identity on container values. Default is true.

Example

use kserd::fmt::FormattingConfig;

let kserd = Kserd::with_id("a-cntr",
    Value::new_cntr(Vec::<(&str, _)>::new()).unwrap())
    .unwrap();

let show_containers = FormattingConfig {
    id_on_containers: true,
    ..Default::default()
};

assert_eq!(
    &kserd.as_str_with_config(show_containers),
    "a-cntr ()"
);
id_on_seqs: bool

Display the identity on sequences. Default is false.

Example

use kserd::fmt::FormattingConfig;

let kserd = Kserd::with_id("a-seq", Value::Seq(vec![])).unwrap();

let show_seqs = FormattingConfig {
    id_on_seqs: true,
    ..Default::default()
};

assert_eq!(
    &kserd.as_str_with_config(show_seqs),
    "a-seq []"
);
id_on_maps: bool

Display the identity on maps. Default is false.

Example

use kserd::fmt::FormattingConfig;

let kserd = Kserd::with_id("a-map", Value::new_map(vec![])).unwrap();

let show_maps = FormattingConfig {
    id_on_maps: true,
    ..Default::default()
};

assert_eq!(
    &kserd.as_str_with_config(show_maps),
    "a-map {}"
);
width_limit: Option<u16>

The column limit in characters. The default is Some(60).

When a FormattingConfig is applied to a Kserd, the contents tries to use inline formatting as much as possible. If the inline format is too wide, then a concise format is used. If the concise format is still too wide, the verbose formatting is used.

Setting to None means a limit is never reached and all formatting is done inline. Setting to Some(0) means the formatting always reaches a limit and each Kserd will be formatted at it’s maximum verbosity.

Trait Implementations

impl Clone for FormattingConfig[src]

impl Copy for FormattingConfig[src]

impl Debug for FormattingConfig[src]

impl Default for FormattingConfig[src]

impl PartialEq<FormattingConfig> for FormattingConfig[src]

impl StructuralPartialEq for FormattingConfig[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Conv for T

impl<T> Conv for T

impl<T> FmtForward for T

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Pipe for T where
    T: ?Sized

impl<T> Pipe for T

impl<T> PipeAsRef for T

impl<T> PipeBorrow for T

impl<T> PipeDeref for T

impl<T> PipeRef for T

impl<T> Tap for T

impl<T> Tap for T

impl<T, U> TapAsRef<U> for T where
    U: ?Sized

impl<T, U> TapBorrow<U> for T where
    U: ?Sized

impl<T> TapDeref for T

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> TryConv for T

impl<T> TryConv for T

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.