pub struct ContentKey(pub &'static str);Expand description
The key under which maps hold their own content.
In some formats a value is text or a map that holds the text together
with more entries: the elements of XML are their text (<count>3</count>)
or, if they have attributes, a map (<count unit="m">3</count> is
{"@unit": "m", "$text": "3"}). Which of the two a value is depends on
the input, not on the type it’s deserialized into. Formats like this
set the key of the content in the State (see set),
and values are then passed on in the form the type accepts:
- A type that rejects a map (like
u32) receives the value of the key of the content, the other entries are skipped. If the map has no such key it receives empty text (""as lexical atom). - A type that rejects lexical atoms but accepts maps (like a struct) receives a map with the text under the key of the content (no entry for empty text).
Both only happen after a type rejected the value, so they cost nothing otherwise. Without the key (the default), values are passed on as they are.
use deser::de::{ContentKey, DeserializeDriver};
use deser::{Atom, Deserialize, Event};
#[derive(Deserialize, Debug, PartialEq)]
struct Price {
#[deser(rename = "@currency")]
currency: Option<String>,
#[deser(rename = "$text")]
amount: u32,
}
let mut out = None::<(u32, Price)>;
let mut driver = DeserializeDriver::new(&mut out);
ContentKey("$text").set(driver.state_mut());
driver.emit(Event::seq_start()).unwrap();
// a map for a `u32`
driver.emit(Event::map_start()).unwrap();
for text in ["@unit", "m", "$text", "3"] {
driver.emit(Atom::Lexical(text.into())).unwrap();
}
driver.emit(Event::MapEnd).unwrap();
// text for a struct
driver.emit(Atom::Lexical("12".into())).unwrap();
driver.emit(Event::SeqEnd).unwrap();
drop(driver);
assert_eq!(out, Some((3, Price { currency: None, amount: 12 })));Tuple Fields§
§0: &'static strImplementations§
Source§impl ContentKey
impl ContentKey
Trait Implementations§
Source§impl Clone for ContentKey
impl Clone for ContentKey
impl Copy for ContentKey
Source§impl Debug for ContentKey
impl Debug for ContentKey
impl Eq for ContentKey
Source§impl PartialEq for ContentKey
impl PartialEq for ContentKey
impl StructuralPartialEq for ContentKey
Auto Trait Implementations§
impl Freeze for ContentKey
impl RefUnwindSafe for ContentKey
impl Send for ContentKey
impl Sync for ContentKey
impl Unpin for ContentKey
impl UnsafeUnpin for ContentKey
impl UnwindSafe for ContentKey
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
Mutably borrows from an owned value. Read more
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
Compare self to
key and return true if they are equal.