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>
impl DictSerializer<String>
Source§impl<'a> DictSerializer<&'a mut String>
impl<'a> DictSerializer<&'a mut String>
Sourcepub fn with_buffer(buffer: &'a mut String) -> Self
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>
impl<W: BorrowMut<String>> DictSerializer<W>
Sourcepub fn bare_item<'b>(
&mut self,
name: &KeyRef,
value: impl Into<RefBareItem<'b>>,
) -> ParameterSerializer<&mut String>
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.
Sourcepub fn inner_list(&mut self, name: &KeyRef) -> InnerListSerializer<'_>
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.
Sourcepub fn members<'b>(
&mut self,
members: impl IntoIterator<Item = (impl AsRef<KeyRef>, &'b ListEntry)>,
)
pub fn members<'b>( &mut self, members: impl IntoIterator<Item = (impl AsRef<KeyRef>, &'b ListEntry)>, )
Serializes the given members of the dictionary.
Sourcepub fn finish(self) -> Option<W>
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.