use super::Source;
use crate::discover::DiscoveredAdvisory;
use crate::model::metadata::{Distribution, Key, ProviderMetadata};
use crate::retrieve::RetrievedAdvisory;
use crate::source::{FileSource, HttpSource, KeySource, KeySourceError, MapSourceError};
use crate::utils::openpgp::PublicKey;
use async_trait::async_trait;
#[derive(Clone)]
pub enum DispatchSource {
File(FileSource),
Http(HttpSource),
}
impl From<FileSource> for DispatchSource {
fn from(value: FileSource) -> Self {
Self::File(value)
}
}
impl From<HttpSource> for DispatchSource {
fn from(value: HttpSource) -> Self {
Self::Http(value)
}
}
#[async_trait(?Send)]
impl Source for DispatchSource {
type Error = anyhow::Error;
async fn load_metadata(&self) -> Result<ProviderMetadata, Self::Error> {
match self {
Self::File(source) => source.load_metadata().await,
Self::Http(source) => source.load_metadata().await.map_err(|err| err.into()),
}
}
async fn load_index(
&self,
distribution: &Distribution,
) -> Result<Vec<DiscoveredAdvisory>, Self::Error> {
match self {
Self::File(source) => source.load_index(distribution).await,
Self::Http(source) => source
.load_index(distribution)
.await
.map_err(|err| err.into()),
}
}
async fn load_advisory(
&self,
advisory: DiscoveredAdvisory,
) -> Result<RetrievedAdvisory, Self::Error> {
match self {
Self::File(source) => source.load_advisory(advisory).await,
Self::Http(source) => source
.load_advisory(advisory)
.await
.map_err(|err| err.into()),
}
}
}
#[async_trait(?Send)]
impl KeySource for DispatchSource {
type Error = anyhow::Error;
async fn load_public_key(&self, key: &Key) -> Result<PublicKey, KeySourceError<Self::Error>> {
match self {
Self::File(source) => source.load_public_key(key).await,
Self::Http(source) => source
.load_public_key(key)
.await
.map_source(|err| err.into()),
}
}
}