Skip to main content

LexicalRules

Struct LexicalRules 

Source
pub struct LexicalRules { /* private fields */ }
Expand description

How lexical atoms are interpreted.

Lexical atoms are text whose type the format cannot express. The types they are delivered to interpret them: numbers and booleans parse them, strings take them as they are. What text means beyond that depends on where it comes from. Keys of JSON objects are strict (a bool key is true or false), everything in a query string or an environment variable is text so on and yes are booleans too and empty values are missing values.

The rules are an extension value, the default are the strict rules. Formats where everything is text have other defaults (query strings, environment variables and CSV use the lenient rules), which rules in the Context (or in the State, see set) override. As they are part of the state (and not of the atoms), lexical atoms that are buffered and replayed are interpreted with the rules of the deserialization they are replayed in.

use deser::de::{DeserializeDriver, LexicalRules};
use deser::{Atom, Text};

let mut out = None::<bool>;
let mut driver = DeserializeDriver::new(&mut out);
LexicalRules::LENIENT.set(driver.state_mut());
driver.emit(Atom::Lexical(Text::borrowed("on"))).unwrap();
drop(driver);
assert_eq!(out, Some(true));

Implementations§

Source§

impl LexicalRules

Source

pub const STRICT: LexicalRules

The rules for text that happens to be text, like the keys of JSON objects. This is the default.

  • booleans are true and false
  • integers and floats are parsed with str::parse
  • empty text is not a missing value
Source

pub const LENIENT: LexicalRules

The rules for formats where everything is text, like query strings and environment variables.

  • booleans are true, yes, on and 1 and false, no, off and 0 (ignoring ASCII case)
  • integers and floats are parsed with str::parse
  • empty text is a missing value for types that do not accept it: None for an Option<u32>, Some("") for an Option<String> and ()

These formats typically also allow keys to repeat, a key given once can then stand for a sequence of one value. That is not a rule of the text but of the maps they emit (see ContainerShape::set_multimap).

Source

pub fn of(state: &State) -> LexicalRules

Returns the rules of a deserialization.

Source

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

Sets the rules of a deserialization.

Source

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

Sets the rules unless the state or the context has rules.

Formats use this for their default (see State::set_default).

Source

pub const fn set_lenient_bools(&mut self, yes: bool)

Sets if booleans are also yes, on and 1 and no, off and 0 (ignoring ASCII case).

Source

pub const fn lenient_bools(&self) -> bool

Returns true if booleans are also yes, on and 1 and no, off and 0 (see set_lenient_bools).

Source

pub const fn set_empty_is_null(&mut self, yes: bool)

Sets if empty text is a missing value for types that do not accept it.

Source

pub const fn empty_is_null(&self) -> bool

Returns true if empty text is a missing value for types that do not accept it (see set_empty_is_null).

Trait Implementations§

Source§

impl Clone for LexicalRules

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 LexicalRules

Source§

impl Debug for LexicalRules

Source§

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

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

impl Default for LexicalRules

Source§

fn default() -> LexicalRules

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

impl Eq for LexicalRules

Source§

impl PartialEq for LexicalRules

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 LexicalRules

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.