Skip to main content

gar_crawl/
handler.rs

1use reqwest::{Client, Url};
2use scraper::{ElementRef, Html};
3use std::sync::Arc;
4
5/// Handlers are void Fns
6pub type Handler<'a> = Box<dyn FnMut(&HandlerArgs) + Send + Sync + 'a>;
7
8/// Propagators return a `Vec<Url>` to queue
9pub type Propagator<'a> = Box<dyn FnMut(&HandlerArgs) -> Vec<Url> + Send + Sync + 'a>;
10
11/// Data to pass to the user as closure arguments
12#[derive(Clone)]
13pub struct HandlerArgs<'a> {
14    /// Current page
15    pub page: &'a Page,
16    /// CSS element if available
17    pub element: Option<ElementRef<'a>>,
18    /// Reqwest client
19    pub client: Arc<Client>,
20}
21
22/// Information about the current page
23#[derive(Clone, Eq, PartialEq)]
24pub struct Page {
25    /// Url of the current location
26    pub url: Url,
27    /// Response body as a string
28    pub text: String,
29    /// Parsed HTML document
30    pub doc: Html,
31    /// Current crawl depth
32    pub depth: usize,
33}
34
35/// These are the events you can hook into
36#[derive(Clone, Eq, PartialEq, Hash)]
37pub enum HandlerEvent {
38    /// Handle all found matches of a CSS selector
39    OnSelector(String),
40    /// Handle every page loaded
41    OnPage,
42}