1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//! The actual walker

use crate::discover::{DiscoveredAdvisory, DiscoveredVisitor};
use crate::model::metadata::{Distribution, ProviderMetadata};
use reqwest::Url;
use std::fmt::Debug;
use url::ParseError;

#[derive(Debug, thiserror::Error)]
pub enum Error<VE>
where
    VE: std::error::Error + Debug,
{
    #[error("Request error: {0}")]
    Request(#[from] reqwest::Error),
    #[error("URL error: {0}")]
    Url(#[from] ParseError),
    #[error("Visitor error: {0}")]
    Visitor(VE),
}

pub struct Walker {
    url: Url,
    client: reqwest::Client,
}

impl Walker {
    pub fn new(url: Url, client: reqwest::Client) -> Self {
        Self { url, client }
    }

    pub async fn walk<V>(self, visitor: V) -> Result<(), Error<V::Error>>
    where
        V: DiscoveredVisitor,
    {
        let metadata: ProviderMetadata = self
            .client
            .get(self.url.clone())
            .send()
            .await?
            .error_for_status()?
            .json()
            .await?;

        let context = visitor
            .visit_context(&metadata)
            .await
            .map_err(Error::Visitor)?;

        for source in metadata.distributions {
            log::debug!("Walking: {}", source.directory_url);
            for url in self.load_index::<V>(&source).await? {
                log::debug!("  Discovered advisory: {url}");
                visitor
                    .visit_advisory(&context, DiscoveredAdvisory { url })
                    .await
                    .map_err(Error::Visitor)?;
            }
        }

        Ok(())
    }

    async fn load_index<V>(&self, dist: &Distribution) -> Result<Vec<Url>, Error<V::Error>>
    where
        V: DiscoveredVisitor,
    {
        Ok(self
            .client
            .get(dist.directory_url.join("index.txt")?)
            .send()
            .await?
            .text()
            .await?
            .lines()
            .into_iter()
            .map(|s| Url::parse(&format!("{}{s}", dist.directory_url)))
            .collect::<Result<_, _>>()?)
    }
}