use crate::dependencies::{DependencyType, Registry, ResolvedDependency};
use crate::project::buildable::GetBuildable;
use std::fmt::{Debug, Display, Formatter};
use std::io::Write;
use std::path::Path;
pub trait Dependency: GetBuildable + Debug {
fn id(&self) -> String;
fn dep_type(&self) -> DependencyType;
fn try_resolve(
&self,
registry: &dyn Registry,
cache_path: &Path,
) -> Result<ResolvedDependency, AcquisitionError>;
}
assert_obj_safe!(Dependency);
impl Display for Box<dyn Dependency + Send + Sync> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.id())
}
}
pub trait IntoDependency {
type IntoDep: Dependency;
fn into_dependency(self) -> Self::IntoDep;
}
impl<D: Dependency> IntoDependency for D {
type IntoDep = D;
fn into_dependency(self) -> Self::IntoDep {
self
}
}
#[derive(Debug, thiserror::Error)]
pub enum AcquisitionError {
#[error("{}", error)]
Custom { error: String },
#[error("Can't acquire dependency because url is in wrong scheme (expected = {expected}, found = {found})")]
IncorrectUrlScheme { found: String, expected: String },
#[error("File is missing")]
MissingFile,
#[error("Errors: {}", inner.iter().map(|e| e.to_string()).collect::<Vec<_>>().join(","))]
InnerErrors { inner: Vec<AcquisitionError> },
}
impl AcquisitionError {
pub fn custom(message: impl ToString) -> Self {
Self::Custom {
error: message.to_string(),
}
}
}
impl FromIterator<AcquisitionError> for AcquisitionError {
fn from_iter<T: IntoIterator<Item = AcquisitionError>>(iter: T) -> Self {
Self::InnerErrors {
inner: iter.into_iter().collect(),
}
}
}