Skip to main content

Context

Struct Context 

Source
pub struct Context<C> { /* private fields */ }
Expand description

A request being crawled, with everything needed to handle it.

It holds the Request, a client to fetch it with, a Queue to add the links it leads to, and the crawl’s datasets. Handlers borrow it, so several of them can handle one request: the request is fetched once, by the first response or body, and the response’s head is kept. The body is always read from the client, so a browser page reads as currently rendered.

Implementations§

Source§

impl<C> Context<C>

Source

pub fn new(request: Request, client: C) -> Self

Creates a standalone context, e.g. to test a handler.

It gets an empty queue of its own and no datasets; see with_queue and with_dataset.

Source

pub fn with_queue(self, queue: &Queue) -> Self

Replaces the queue, binding it to the request.

Source

pub fn with_dataset<T, D>(self, dataset: D) -> Self
where T: Send + 'static, D: Dataset<T>,

Registers dataset for items of type T.

Source

pub const fn request(&self) -> &Request

Returns the request.

Source

pub const fn request_mut(&mut self) -> &mut Request

Returns the request mutably, e.g. to add headers before fetching.

Source

pub const fn client(&self) -> &C

Returns the client.

Source

pub const fn queue(&self) -> &Queue

Returns the queue for the links this request leads to.

Source

pub fn dataset<T: Send + 'static>(&self) -> Result<Data<T>>

Returns the dataset for items of type T.

§Errors

Returns a Dataset error if no dataset is registered for T.

Source

pub async fn response(&self) -> Result<&Response<()>>
where C: Client,

Returns the response’s head, fetching the request on the first call.

Later calls return the same head without fetching again. A failed fetch is not kept: the next call tries again. Once fetched, links followed through the queue, including clones taken earlier, resolve against the URI the response was served from.

§Errors

Returns an error if the client fails to fetch the request.

Source

pub async fn body(&self) -> Result<Bytes>
where C: Client,

Returns the response’s body, fetching the request first if needed: the bytes received, or a browser page as currently rendered.

§Errors

Returns an error if the request cannot be fetched or its body read.

Trait Implementations§

Source§

impl<C> Debug for Context<C>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<C> !Freeze for Context<C>

§

impl<C> !RefUnwindSafe for Context<C>

§

impl<C> !UnwindSafe for Context<C>

§

impl<C> Send for Context<C>
where C: Send,

§

impl<C> Sync for Context<C>
where C: Sync,

§

impl<C> Unpin for Context<C>
where C: Unpin,

§

impl<C> UnsafeUnpin for Context<C>
where C: UnsafeUnpin,

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, 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.
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