pub struct ContainerShape { /* private fields */ }Expand description
Facts about a map or sequence.
The shape is carried by Event::MapStart and Event::SeqStart. It
holds information that formats can use to encode or decode a container,
all of which can be ignored:
order: how significant the order of the elements is.len: the number of elements (entries for maps) if known.is_multimap: the keys of the map can be given more than once.
use deser::{ContainerShape, Order};
const SHAPE: ContainerShape =
ContainerShape::new().with_order(Order::Sorted);
assert_eq!(SHAPE.order(), Order::Sorted);
assert_eq!(SHAPE.len(), None);Implementations§
Source§impl ContainerShape
impl ContainerShape
Sourcepub const fn new() -> ContainerShape
pub const fn new() -> ContainerShape
Creates the default shape: unknown length and natural order.
Sourcepub const fn with_len(self, len: usize) -> ContainerShape
pub const fn with_len(self, len: usize) -> ContainerShape
Sets the number of elements.
Sourcepub const fn with_order(self, order: Order) -> ContainerShape
pub const fn with_order(self, order: Order) -> ContainerShape
Sets the order.
Sourcepub const fn len(&self) -> Option<usize>
pub const fn len(&self) -> Option<usize>
Returns the number of elements (entries for maps) if known.
Sourcepub const fn with_multimap(self, yes: bool) -> ContainerShape
pub const fn with_multimap(self, yes: bool) -> ContainerShape
Marks a map as a multimap: its keys can be given more than once.
Formats where keys can repeat (like query strings with a=1&a=2,
the elements of XML or the columns of CSV files) emit their maps
with this flag and pass on every occurrence of a key as an entry of
its own, in the order of the input. How repeated keys are resolved
is up to the type that receives the map:
- The fields of derived structs and the values of maps whose type is
a collection (like
Vec<T>orHashSet<T>) collect the values of all occurrences of their key. A key that is given once is a collection of one value and a key that is missing is an empty collection. - Other fields and values receive a single value,
DuplicateKeysin theStatedecides which one: the last one, the first one or an error (the default).
Types that do not know about multimaps receive the entries like
those of any other map. See State::is_multimap.
use deser::de::DeserializeDriver;
use deser::{Atom, ContainerShape, Deserialize, Event};
#[derive(Deserialize, Debug, PartialEq)]
struct Query {
tag: Vec<String>,
page: u32,
user: Vec<String>,
}
let mut out = None::<Query>;
let mut driver = DeserializeDriver::new(&mut out);
driver
.emit(Event::MapStart(ContainerShape::new().with_multimap(true)))
.unwrap();
for (key, value) in [("tag", "a"), ("page", "1"), ("tag", "b")] {
driver.emit(key).unwrap();
driver.emit(Atom::Lexical(value.into())).unwrap();
}
driver.emit(Event::MapEnd).unwrap();
drop(driver);
assert_eq!(out, Some(Query {
tag: vec!["a".into(), "b".into()],
page: 1,
user: vec![],
}));Sourcepub const fn is_multimap(&self) -> bool
pub const fn is_multimap(&self) -> bool
Returns true if the keys of the map can be given more than once.
See with_multimap.
Trait Implementations§
Source§impl Clone for ContainerShape
impl Clone for ContainerShape
impl Copy for ContainerShape
Source§impl Debug for ContainerShape
impl Debug for ContainerShape
Source§impl Default for ContainerShape
impl Default for ContainerShape
Source§fn default() -> ContainerShape
fn default() -> ContainerShape
impl Eq for ContainerShape
Source§impl Hash for ContainerShape
impl Hash for ContainerShape
Source§impl PartialEq for ContainerShape
impl PartialEq for ContainerShape
impl StructuralPartialEq for ContainerShape
Auto Trait Implementations§
impl Freeze for ContainerShape
impl RefUnwindSafe for ContainerShape
impl Send for ContainerShape
impl Sync for ContainerShape
impl Unpin for ContainerShape
impl UnsafeUnpin for ContainerShape
impl UnwindSafe for ContainerShape
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.