use crate::model::metadata;
use crate::model::metadata::SourceMetadata;
use std::fmt::Debug;
use std::future::Future;
use std::ops::Deref;
use std::time::SystemTime;
use url::Url;
use walker_common::utils::url::Urlify;
pub struct DiscoverConfig {
pub source: String,
pub since: Option<SystemTime>,
pub keys: Vec<metadata::Key>,
}
impl DiscoverConfig {
pub fn with_since(mut self, since: impl Into<Option<SystemTime>>) -> Self {
self.since = since.into();
self
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DiscoveredSbom {
pub url: Url,
pub modified: SystemTime,
}
impl Urlify for DiscoveredSbom {
fn url(&self) -> &Url {
&self.url
}
}
#[derive(Debug)]
pub struct DiscoveredContext<'c> {
pub metadata: &'c SourceMetadata,
}
impl<'c> Deref for DiscoveredContext<'c> {
type Target = SourceMetadata;
fn deref(&self) -> &Self::Target {
self.metadata
}
}
pub trait DiscoveredVisitor {
type Error: std::fmt::Display + Debug;
type Context;
fn visit_context(
&self,
context: &DiscoveredContext,
) -> impl Future<Output = Result<Self::Context, Self::Error>>;
fn visit_sbom(
&self,
context: &Self::Context,
sbom: DiscoveredSbom,
) -> impl Future<Output = Result<(), Self::Error>>;
}
impl<F, E, Fut> DiscoveredVisitor for F
where
F: Fn(DiscoveredSbom) -> Fut,
Fut: Future<Output = Result<(), E>>,
E: std::fmt::Display + Debug,
{
type Error = E;
type Context = ();
async fn visit_context(
&self,
_context: &DiscoveredContext<'_>,
) -> Result<Self::Context, Self::Error> {
Ok(())
}
async fn visit_sbom(
&self,
_context: &Self::Context,
sbom: DiscoveredSbom,
) -> Result<(), Self::Error> {
self(sbom).await
}
}