scrapman/
stage.rs

1use crate::action::ScrapeAction;
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Serialize, Deserialize, Clone)]
5pub enum FlowControl {
6    Continue,
7    Quit,
8    Goto(String),
9    Repeat { delay: Option<f64> },
10}
11
12impl FlowControl {
13    pub fn goto<T: Into<String>>(stage: T) -> Self {
14        FlowControl::Goto(stage.into())
15    }
16
17    pub fn repeat() -> Self {
18        FlowControl::Repeat { delay: None }
19    }
20
21    pub fn repeat_with_delay(delay: f64) -> Self {
22        FlowControl::Repeat { delay: Some(delay) }
23    }
24}
25
26#[derive(Debug, Serialize, Deserialize)]
27pub struct ScrapeStage {
28    pub name: Option<String>,
29    pub action: Box<dyn ScrapeAction>,
30    pub on_complete: FlowControl,
31    pub on_error: FlowControl,
32}
33
34impl ScrapeStage {
35    pub fn with_name<T: Into<String>>(mut self, name: T) -> Self {
36        self.name = Some(name.into());
37        self
38    }
39
40    pub fn on_complete(mut self, on_complete: FlowControl) -> Self {
41        self.on_complete = on_complete;
42        self
43    }
44
45    pub fn on_any_error(mut self, on_error: FlowControl) -> Self {
46        self.on_error = on_error;
47        self
48    }
49}
50
51impl<Action> From<Action> for ScrapeStage
52where
53    Action: 'static + ScrapeAction,
54{
55    fn from(action: Action) -> Self {
56        ScrapeStage {
57            name: None,
58            action: Box::new(action),
59            on_complete: FlowControl::Continue,
60            on_error: FlowControl::Continue,
61        }
62    }
63}
64
65impl<Name, Action> Into<ScrapeStage> for (Name, Action)
66where
67    Name: Into<String>,
68    Action: 'static + ScrapeAction,
69{
70    fn into(self) -> ScrapeStage {
71        ScrapeStage {
72            name: Some(self.0.into()),
73            action: Box::new(self.1),
74            on_complete: FlowControl::Continue,
75            on_error: FlowControl::Continue,
76        }
77    }
78}