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
use crate::discover::DiscoveredSbom;
use crate::model::metadata::SourceMetadata;
use crate::retrieve::RetrievedSbom;
use crate::source::{FileSource, HttpSource, Source};
use async_trait::async_trait;
use walker_common::{
    utils::openpgp::PublicKey,
    validate::source::{Key, KeySource, KeySourceError, MapSourceError},
};

/// A common source type, dispatching to the known implementations.
///
/// This helps creating implementations which don't need to know the exact type. Unfortunately we
/// cannot just "box" this, as the [`Source`] needs to implement [`Clone`], which requires [`Sized`],
/// which prevents us from using `dyn` ("cannot be made into an object").
///
/// There may be a better way around this, feel free to send a PR ;-)
#[derive(Clone)]
pub enum DispatchSource {
    Http(HttpSource),
    File(FileSource),
}

impl From<HttpSource> for DispatchSource {
    fn from(value: HttpSource) -> Self {
        Self::Http(value)
    }
}

impl From<FileSource> for DispatchSource {
    fn from(value: FileSource) -> Self {
        Self::File(value)
    }
}

#[async_trait(?Send)]
impl Source for DispatchSource {
    type Error = anyhow::Error;

    async fn load_metadata(&self) -> Result<SourceMetadata, Self::Error> {
        match self {
            Self::Http(source) => Ok(source.load_metadata().await?),
            Self::File(source) => Ok(source.load_metadata().await?),
        }
    }

    async fn load_index(&self) -> Result<Vec<DiscoveredSbom>, Self::Error> {
        match self {
            Self::Http(source) => Ok(source.load_index().await?),
            Self::File(source) => Ok(source.load_index().await?),
        }
    }

    async fn load_sbom(&self, sbom: DiscoveredSbom) -> Result<RetrievedSbom, Self::Error> {
        match self {
            Self::Http(source) => Ok(source.load_sbom(sbom).await?),
            Self::File(source) => Ok(source.load_sbom(sbom).await?),
        }
    }
}

#[async_trait(?Send)]
impl KeySource for DispatchSource {
    type Error = anyhow::Error;

    async fn load_public_key<'a>(
        &self,
        key: Key<'a>,
    ) -> Result<PublicKey, KeySourceError<Self::Error>> {
        match self {
            Self::Http(source) => source
                .load_public_key(key)
                .await
                .map_source(|err| err.into()),
            Self::File(source) => source.load_public_key(key).await,
        }
    }
}