Skip to main content

TrackLocations

Struct TrackLocations 

Source
pub struct TrackLocations(pub bool);
Expand description

Asks the formats to provide the Source (a value of the Context).

Formats publish the byte range of every event, resolving them into lines and columns (for instance with the Spanned type of deser-location) also requires the source, which is a copy of the input. Formats only provide it if this is set to true in the context (or the state). The errors a deserialization fails with have their line and column either way, but for instance the keys collected with UnknownFields::Collect only have them with the source:

use deser::de::{Deserializer, IgnoredFields, UnknownFields};
use deser::{Context, Deserialize, TrackLocations};

#[derive(Deserialize)]
struct Config {
    name: String,
}

let config = deser_json::DeserializerConfig::builder()
    .context(Context::with(TrackLocations(true)))
    .build();
let input = "{\n  \"name\": \"demo\",\n  \"nmae\": \"x\"\n}";
let ignored = IgnoredFields::new();
deser_json::Deserializer::from_str_with_config(input, config)
    .deserialize_with::<Config, _>(|driver| {
        UnknownFields::Collect(ignored.clone()).set(driver.state_mut())
    })
    .unwrap();
let ignored = ignored.take();
assert_eq!((ignored[0].line(), ignored[0].column()), (Some(3), Some(3)));

Formats check this with of and set the Source before they emit the first event:

use deser::de::{DeserializeDriver, Deserializer};
use deser::{Error, Source, TrackLocations};

/// A format which provides the source if asked to.
struct Text<'a>(&'a str);

impl<'de> Deserializer<'de> for Text<'de> {
    fn drive(&mut self, driver: &mut DeserializeDriver<'_, 'de>) -> Result<(), Error> {
        if TrackLocations::of(driver.state()) {
            Source(self.0.into()).set(driver.state_mut());
        }
        driver.state_mut().set_input_range(0, self.0.len());
        driver.emit(self.0)
    }
}

Tuple Fields§

§0: bool

Implementations§

Source§

impl TrackLocations

Source

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

Returns true if the formats provide the source.

Source

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

Sets if the formats provide the source.

Trait Implementations§

Source§

impl Clone for TrackLocations

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 TrackLocations

Source§

impl Debug for TrackLocations

Source§

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

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

impl Default for TrackLocations

Source§

fn default() -> Self

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

impl Eq for TrackLocations

Source§

impl PartialEq for TrackLocations

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 TrackLocations

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.