scrapman/action/
open_url.rs

1use crate::{
2    action::{ScrapeAction, ScrapeActionResult},
3    pipeline::ScrapeContext,
4    value::Value,
5    ScrapeError,
6};
7use async_trait::async_trait;
8use serde::{Deserialize, Serialize};
9use std::fmt::{Display, Formatter, Result as FormatResult};
10
11#[derive(Debug, Serialize, Deserialize)]
12pub struct OpenUrl {
13    pub url: Value,
14}
15
16impl OpenUrl {
17    pub fn new(url: Value) -> OpenUrl {
18        OpenUrl { url }
19    }
20}
21
22impl Display for OpenUrl {
23    fn fmt(&self, fmt: &mut Formatter<'_>) -> FormatResult {
24        write!(fmt, "open URL with the value from {}", self.url)
25    }
26}
27
28#[async_trait]
29#[typetag::serde]
30impl ScrapeAction for OpenUrl {
31    async fn execute(&self, mut context: &mut ScrapeContext) -> ScrapeActionResult {
32        match self.url.resolve(&mut context).await? {
33            Some(url) => context.client.goto(&url).await,
34            None => Err(ScrapeError::MissingUrl),
35        }
36    }
37}