Struct DictSerializer

Source
pub struct DictSerializer<W> { /* private fields */ }
Expand description

Serializes Dictionary field value components incrementally.

Note: The serialization conforms to RFC 9651, meaning that Dates and Display Strings, which cause parsing errors under RFC 8941, will be serialized unconditionally. The consumer of this API is responsible for determining whether it is valid to serialize these bare items for any specific header.

use sfv::{KeyRef, StringRef, TokenRef, DictSerializer, Decimal};

let mut ser = DictSerializer::new();

ser.bare_item(KeyRef::from_str("member1")?, 11)
    .parameter(KeyRef::from_str("foo")?, true);

{
  let mut ser = ser.inner_list(KeyRef::from_str("member2")?);

  ser.bare_item(TokenRef::from_str("abc")?)
      .parameter(KeyRef::from_str("abc_param")?, false);

  ser.bare_item(TokenRef::from_str("def")?);

  ser.finish()
     .parameter(KeyRef::from_str("bar")?, StringRef::from_str("val")?);
}

ser.bare_item(KeyRef::from_str("member3")?, Decimal::try_from(12.34566)?);

assert_eq!(
    ser.finish().as_deref(),
    Some(r#"member1=11;foo, member2=(abc;abc_param=?0 def);bar="val", member3=12.346"#),
);

Implementations§

Source§

impl DictSerializer<String>

Source

pub fn new() -> Self

Creates a serializer that writes into a new string.

Source§

impl<'a> DictSerializer<&'a mut String>

Source

pub fn with_buffer(buffer: &'a mut String) -> Self

Creates a serializer that writes into the given string.

Source§

impl<W: BorrowMut<String>> DictSerializer<W>

Source

pub fn bare_item<'b>( &mut self, name: &KeyRef, value: impl Into<RefBareItem<'b>>, ) -> ParameterSerializer<&mut String>

Serializes the given bare item as a member of the dictionary with the given key.

Returns a serializer for the item’s parameters.

Source

pub fn inner_list(&mut self, name: &KeyRef) -> InnerListSerializer<'_>

Opens an inner list with the given key, returning a serializer to be used for its items and parameters.

Source

pub fn members<'b>( &mut self, members: impl IntoIterator<Item = (impl AsRef<KeyRef>, &'b ListEntry)>, )

Serializes the given members of the dictionary.

Source

pub fn finish(self) -> Option<W>

Finishes serialization of the dictionary and returns the underlying output.

Returns None if and only if no members were serialized, as empty dictionaries are not meant to be serialized at all.

Trait Implementations§

Source§

impl<W: Debug> Debug for DictSerializer<W>

Source§

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

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

impl Default for DictSerializer<String>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<W> Freeze for DictSerializer<W>
where W: Freeze,

§

impl<W> RefUnwindSafe for DictSerializer<W>
where W: RefUnwindSafe,

§

impl<W> Send for DictSerializer<W>
where W: Send,

§

impl<W> Sync for DictSerializer<W>
where W: Sync,

§

impl<W> Unpin for DictSerializer<W>
where W: Unpin,

§

impl<W> UnwindSafe for DictSerializer<W>
where W: UnwindSafe,

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 = 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.