use crate::dependency::RetrievedDependency;
use futures::stream::Stream;
use futures::StreamExt;
use futures::{future::BoxFuture, stream::Iter};
use std::{
fmt::Debug,
pin::Pin,
task::{Context, Poll},
vec::IntoIter,
};
pub struct RetrievedDependencyStream<'a> {
stream: Iter<IntoIter<BoxFuture<'a, RetrievedDependency>>>,
}
impl<'a> RetrievedDependencyStream<'a> {
pub fn new(futures: Vec<BoxFuture<'a, RetrievedDependency>>) -> Self {
Self {
stream: futures::stream::iter(futures),
}
}
pub fn into_inner(self) -> Iter<IntoIter<BoxFuture<'a, RetrievedDependency>>> {
self.stream
}
}
impl<'a> Stream for RetrievedDependencyStream<'a> {
type Item = BoxFuture<'a, RetrievedDependency>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
self.as_mut().stream.poll_next_unpin(cx)
}
}
pub type RetrievedDependencyStreamResult<'a> = Result<RetrievedDependencyStream<'a>, Error>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Error deserialiazing yaml: {0}")]
YamlSerde(#[from] serde_yaml::Error),
#[error("Error deserialiazing json: {0}")]
JsonSerde(#[from] package_lock_json_parser::PackageLockJsonError),
#[error("Error parsing yarn.lock file {0}")]
YarnLock(#[from] yarn_lock_parser::YarnLockError),
#[error("Error parsing Cargo.lock file {0}")]
CargoLock(#[from] cargo_lock::Error),
}
pub trait Collector: Debug + Send + Sync {
fn get_name(&self) -> String;
}
pub trait FileCollector: Collector {
fn get_dependency_filename(&self) -> String;
fn get_dependencies(&self, dependency_file_content: &str) -> RetrievedDependencyStreamResult;
}