Skip to main content

ExtractError

Struct ExtractError 

Source
pub struct ExtractError(/* private fields */);
Available on crate feature query only.
Expand description

Why extraction failed, where, and what it was reading.

Its Display gives the field path, the selector and the source, then the reason:

Product.title ("h1"): no element matches
Product.price (".price" @data-value): cannot parse "$5" as f64: invalid float literal
Product.price (".price" @data-value): <span> has no "data-value" attribute
Product.sku (@data-sku): a document has no attributes; give the field a selector or extract from an element
Cents.cents (".price"): cannot convert "$x" with parse_cents: no digits
Typo.title ("h1:hover"): invalid selector: unsupported pseudo-class ':hover' at byte 2
Card: extract_all needs a selector on the type: #[html(select = "…")]

The path names the type and field. Nested structs prefix the outer field (Product.seller.name), Vec items add their index (Product.reviews[1].rating) and so do Dom::extract_all items (Product[1].title). The parentheses hold the selector and the source (@attr, html, outer_html, markdown); a text or nested field shows only its selector, and one with neither has no parentheses. Values are quoted and escaped like {:?}, cut short after 60 characters.

It is one pointer wide (the details are boxed), so Result<T, ExtractError> costs little on the success path.

Implementations§

Source§

impl ExtractError

Source

pub fn new(kind: ExtractErrorKind) -> Self

A new error of kind, without a path, selector or source yet.

Source

pub fn kind(&self) -> &ExtractErrorKind

What went wrong.

Source

pub fn path(&self) -> String

Where: the type and field path, like Product.reviews[2].rating (empty for a value read directly, like dom.extract::<f64>()).

use domq::{ExtractError, ExtractErrorKind};

let err = ExtractError::new(ExtractErrorKind::NoMatch)
    .in_field("Review", "rating")
    .at_index(2)
    .in_field("Product", "reviews");
assert_eq!(err.path(), "Product.reviews[2].rating");
Source

pub fn selector(&self) -> Option<&str>

The selector being matched when it failed (the innermost one).

Source

pub fn value_source(&self) -> Option<&ValueSource>

The value being read when it failed, if it got that far.

Source

pub fn with_selector(self, selector: &str) -> Self

Record the selector being matched, unless the error already names one (the innermost selector is the most specific).

Source

pub fn with_source(self, source: ValueSource) -> Self

Record the value being read, unless the error already names one.

Source

pub fn in_field(self, type_name: &'static str, field: &'static str) -> Self

Put the error inside field field of type type_name: the path Seller.name becomes Product.seller.name after .in_field("Product", "seller").

Source

pub fn at_index(self, index: usize) -> Self

Put the error inside item index of a list: Review.rating becomes Review[2].rating, and after in_field Product.reviews[2].rating.

Trait Implementations§

Source§

impl Clone for ExtractError

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 Debug for ExtractError

Source§

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

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

impl Display for ExtractError

Source§

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

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

impl Eq for ExtractError

Source§

impl Error for ExtractError

Available on crate feature std only.
1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0:

use the Display impl or to_string()

1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl PartialEq for ExtractError

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 ExtractError

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<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> 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 = !

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.