Trait JsonCrawler

Source
pub trait JsonCrawler
where Self: Sized,
{ type BorrowTo<'a>: JsonCrawler where Self: 'a; type IterMut<'a>: Iterator<Item = Self::BorrowTo<'a>> where Self: 'a; type IntoIter: Iterator<Item = Self>;
Show 18 methods // Required methods fn navigate_pointer(self, new_path: impl AsRef<str>) -> CrawlerResult<Self>; fn navigate_index(self, index: usize) -> CrawlerResult<Self>; fn borrow_pointer( &mut self, path: impl AsRef<str>, ) -> CrawlerResult<Self::BorrowTo<'_>>; fn borrow_index( &mut self, index: usize, ) -> CrawlerResult<Self::BorrowTo<'_>>; fn borrow_mut(&mut self) -> Self::BorrowTo<'_>; fn try_into_iter(self) -> CrawlerResult<Self::IntoIter>; fn try_iter_mut(&mut self) -> CrawlerResult<Self::IterMut<'_>>; fn path_exists(&self, path: &str) -> bool; fn get_path(&self) -> String; fn get_source(&self) -> Arc<String>; fn take_value<T: DeserializeOwned>(&mut self) -> CrawlerResult<T>; fn take_value_pointer<T: DeserializeOwned>( &mut self, path: impl AsRef<str>, ) -> CrawlerResult<T>; fn borrow_value<T: for<'de> Deserialize<'de>>(&self) -> CrawlerResult<T>; fn borrow_value_pointer<T: for<'de> Deserialize<'de>>( &self, path: impl AsRef<str>, ) -> CrawlerResult<T>; fn take_value_pointers<T: DeserializeOwned, S: AsRef<str>>( &mut self, paths: &[S], ) -> CrawlerResult<T>; // Provided methods fn try_expect<F, O>(&mut self, msg: impl ToString, f: F) -> CrawlerResult<O> where F: FnOnce(&mut Self) -> CrawlerResult<Option<O>> { ... } fn take_and_parse_str<F: FromStr>(&mut self) -> CrawlerResult<F> where F::Err: Display { ... } fn try_functions<O>( &mut self, functions: Vec<fn(&mut Self) -> CrawlerResult<O>>, ) -> CrawlerResult<O> { ... }
}
Expand description

Trait to represent a JsonCrawler that may own or borrow from the original serde_json::Value.

Required Associated Types§

Source

type BorrowTo<'a>: JsonCrawler where Self: 'a

Source

type IterMut<'a>: Iterator<Item = Self::BorrowTo<'a>> where Self: 'a

Source

type IntoIter: Iterator<Item = Self>

Required Methods§

Source

fn navigate_pointer(self, new_path: impl AsRef<str>) -> CrawlerResult<Self>

Source

fn navigate_index(self, index: usize) -> CrawlerResult<Self>

Source

fn borrow_pointer( &mut self, path: impl AsRef<str>, ) -> CrawlerResult<Self::BorrowTo<'_>>

Source

fn borrow_index(&mut self, index: usize) -> CrawlerResult<Self::BorrowTo<'_>>

Source

fn borrow_mut(&mut self) -> Self::BorrowTo<'_>

Source

fn try_into_iter(self) -> CrawlerResult<Self::IntoIter>

Source

fn try_iter_mut(&mut self) -> CrawlerResult<Self::IterMut<'_>>

Source

fn path_exists(&self, path: &str) -> bool

Source

fn get_path(&self) -> String

Source

fn get_source(&self) -> Arc<String>

Source

fn take_value<T: DeserializeOwned>(&mut self) -> CrawlerResult<T>

Source

fn take_value_pointer<T: DeserializeOwned>( &mut self, path: impl AsRef<str>, ) -> CrawlerResult<T>

Source

fn borrow_value<T: for<'de> Deserialize<'de>>(&self) -> CrawlerResult<T>

Source

fn borrow_value_pointer<T: for<'de> Deserialize<'de>>( &self, path: impl AsRef<str>, ) -> CrawlerResult<T>

Source

fn take_value_pointers<T: DeserializeOwned, S: AsRef<str>>( &mut self, paths: &[S], ) -> CrawlerResult<T>

For use when you want to try and take value that could be at multiple valid locations. Returns an error message that notes that all valid locations were attempted.

§Usage
// Output will be an error that path should contain "header" and "headerName", if crawler contains neither.
let output: CrawlerResult<String> = crawler.take_value_pointers(&["header", "headerName"]);

Provided Methods§

Source

fn try_expect<F, O>(&mut self, msg: impl ToString, f: F) -> CrawlerResult<O>
where F: FnOnce(&mut Self) -> CrawlerResult<Option<O>>,

For use when you want to apply some operations that return Option, but still return an error with context if they fail. For convenience, closure return type is fallible, allowing you to see the cause of the error at the failure point as well, if you have it.

§Usage
// Returns Ok(42) if crawler parses into 42.
// Returns parsing from string error, plus the message that output should be 42, if output fails to parse from string.
// Returns message that output should be 42, if output parses from string, but is not 42.
let forty_two: CrawlerResult<usize> = crawler.try_expect("Output should be 42", |crawler| {
    let num = crawler.take_and_parse_str::<usize>()?;
    if num == 42 {
        return Ok(Some(num));
    }
    Ok(None)
});
Source

fn take_and_parse_str<F: FromStr>(&mut self) -> CrawlerResult<F>
where F::Err: Display,

Take the value as a String, and apply FromStr to return the desired type.

Source

fn try_functions<O>( &mut self, functions: Vec<fn(&mut Self) -> CrawlerResult<O>>, ) -> CrawlerResult<O>

Try to apply each function in a list of functions, returning the first Ok result, or the last Err result if none returned Ok.

§Warning

If one of the functions mutates before failing, the mutation will still be applied. Also, the mutations are applied sequentially - mutation 1 could impact mutation 2 for example.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§