Skip to main content

ContentKey

Struct ContentKey 

Source
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 str

Implementations§

Source§

impl ContentKey

Source

pub fn of(state: &State) -> Option<&'static str>

Returns the key of the content of a deserialization if there is one.

Source

pub fn set(self, state: &mut State)

Sets the key of the content of a deserialization.

The empty key means that there is no key of the content (the default).

Trait Implementations§

Source§

impl Clone for ContentKey

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for ContentKey

Source§

impl Debug for ContentKey

Source§

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

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

impl Eq for ContentKey

Source§

impl PartialEq for ContentKey

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for ContentKey

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.