Struct Element

Source
pub struct Element<'buf> { /* private fields */ }
Expand description

A JSON Element composed of a Path and it’s [Value].

The Span is included so that the Element’s source &str can be acquired from the source JSON if needed.

Implementations§

Source§

impl<'buf> Element<'buf>

Source

pub fn id(&self) -> ElemId

Return the unique Id for this Element.

Source

pub fn path(&self) -> PathRef<'buf>

Return the Path to this Element.

Examples found in repository?
examples/price_cdr_with_unknown_version.rs (line 89)
76fn print_timezone_warnings(
77    cdr: &cdr::Versioned<'_>,
78    warnings: &warning::Report<timezone::WarningKind>,
79) {
80    if warnings.is_empty() {
81        return;
82    }
83
84    eprintln!("WARN: {} warnings from the timezone search", warnings.len());
85
86    for warning::ElementReport { element, warnings } in warnings.iter(cdr.as_element()) {
87        eprintln!(
88            "Warnings reported for `json::Element` at path: `{}`",
89            element.path()
90        );
91
92        for warning in warnings {
93            eprintln!("  * {warning}");
94        }
95
96        eprintln!();
97    }
98}
More examples
Hide additional examples
examples/lint_tariff.rs (line 42)
29fn print_lint_warnings(
30    tariff: &tariff::Versioned<'_>,
31    warnings: &warning::Report<lint::tariff::WarningKind>,
32) {
33    if warnings.is_empty() {
34        return;
35    }
36
37    eprintln!("WARN: {} warnings from the linting", warnings.len());
38
39    for warning::ElementReport { element, warnings } in warnings.iter(tariff.as_element()) {
40        eprintln!(
41            "Warnings reported for `json::Element` at path: `{}`",
42            element.path()
43        );
44
45        for warning in warnings {
46            eprintln!("  * {warning}");
47        }
48
49        eprintln!();
50    }
51}
Source

pub fn source_json(&self, source_json: &'buf str) -> SourceStr<'buf>

Return the source JSON &str of the entire Object field if the Element is an Object. Otherwise return the span of the [Value].

In the case of an array like ["one", "two"], calling this method on the second Element will return "two".

In the case of an object like {"one": 1, "two": 2}, calling this method on the second Element will return "\"two\": 2".

§Panics

If a source JSON is used that this Element didn’t not originate from there is a chance that this function will panic.

Source

pub fn source_json_value(&self, source_json: &'buf str) -> &'buf str

Return the source JSON &str for the [Value].

In the case of an array like ["one", "two"], calling this method on the second Element will return "two".

In the case of an object like {"one": 1, "two": 2}, calling this method on the second Element will return "2".

§Panics

If a source JSON is used that this Element didn’t not originate from there is a chance that this function will panic.

Trait Implementations§

Source§

impl<'buf> Debug for Element<'buf>

Source§

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

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

impl Display for Element<'_>

Source§

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

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

impl Ord for Element<'_>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<'buf> PartialEq for Element<'buf>

Source§

fn eq(&self, other: &Element<'buf>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Element<'_>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'buf> Eq for Element<'buf>

Source§

impl<'buf> StructuralPartialEq for Element<'buf>

Auto Trait Implementations§

§

impl<'buf> Freeze for Element<'buf>

§

impl<'buf> RefUnwindSafe for Element<'buf>

§

impl<'buf> !Send for Element<'buf>

§

impl<'buf> !Sync for Element<'buf>

§

impl<'buf> Unpin for Element<'buf>

§

impl<'buf> UnwindSafe for Element<'buf>

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more