mod engine;
mod failure;
mod lock;
mod package;
mod plugin;
mod version;
pub mod wire;
use std::collections::{BTreeMap, BTreeSet};
use std::fmt;
use std::rc::Rc;
use std::sync::Arc;
use indexmap::IndexMap;
use semver::Version;
use crate::dependency::{Entry, Source};
use crate::diagnostic::SchemaDiagnostic;
use crate::dialect::Dialect;
use crate::names::SourceSubpath;
use crate::prepare::Prepared;
use crate::tree::TreeSource;
use crate::version::Constraint;
pub use engine::{BadVersion, Engine, EngineFailure};
pub use failure::{BoundsLimit, Failure, Identity, PluginDetail, Requester, Step, SubpathDetail};
pub use lock::{LockedContent, LockedDependency, PluginRoot};
pub use package::sort_tags;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Tag {
pub tag: String,
pub commit: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TagVersion {
pub version: Version,
pub tag: String,
pub commit: String,
}
#[derive(Debug, Clone)]
pub enum Answer<T> {
Found(T),
Missing,
Failed(FailedFetch),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FailedFetch {
Fetch(String),
Subpath(SubpathDetail),
}
impl fmt::Display for FailedFetch {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Fetch(reason) => f.write_str(reason),
Self::Subpath(detail) => write!(f, "the folder {}", detail.reason()),
}
}
}
#[derive(Debug, Clone)]
pub struct PathRequest<'a> {
pub name: &'a str,
pub path: &'a str,
pub subpath: Option<&'a str>,
pub declared_in: Option<&'a Rooted>,
}
#[derive(Clone)]
pub struct PathSource {
pub canonical: String,
pub tree: Rc<dyn TreeSource>,
}
pub trait Answers {
fn tags(&self, url: &str) -> Option<Answer<Vec<Tag>>>;
fn reference(&self, url: &str, name: &str) -> Option<Answer<String>>;
fn tree(
&self,
url: &str,
commit: &str,
path: Option<&str>,
) -> Option<Answer<Rc<dyn TreeSource>>>;
fn path(&self, request: &PathRequest<'_>) -> Answer<PathSource>;
fn take_read_error(&self) -> Option<String> {
None
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum Need {
Tags {
url: String,
},
Ref {
url: String,
name: String,
},
Tree {
url: String,
commit: String,
path: Option<String>,
},
}
impl fmt::Display for Need {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Tags { url } => write!(f, "tags of {url}"),
Self::Ref { url, name } => write!(f, "ref `{name}` of {url}"),
Self::Tree { url, commit, path } => match path {
Some(path) => write!(f, "files of {url} at {commit} under {path}"),
None => write!(f, "files of {url} at {commit}"),
},
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum Origin {
Git { url: String, commit: String },
Path { dir: String },
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Rooted {
pub checkout: Origin,
pub subpath: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum SourceId {
Git {
url: String,
subpath: Option<SourceSubpath>,
plugin: Option<String>,
},
Path {
canonical: String,
subpath: Option<SourceSubpath>,
plugin: Option<String>,
},
}
impl SourceId {
pub fn plugin(&self) -> Option<&str> {
match self {
Self::Git { plugin, .. } | Self::Path { plugin, .. } => plugin.as_deref(),
}
}
}
impl fmt::Display for SourceId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let (subpath, plugin) = match self {
Self::Git {
url,
subpath,
plugin,
} => {
write!(f, "git:{url}")?;
(subpath, plugin)
}
Self::Path {
canonical,
subpath,
plugin,
} => {
write!(f, "path:{canonical}")?;
(subpath, plugin)
}
};
if let Some(subpath) = subpath {
write!(f, "@{subpath}")?;
}
if let Some(plugin) = plugin {
write!(f, " (plugin {plugin})")?;
}
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Spec {
Git {
url: String,
version: Option<String>,
},
Path(String),
}
impl Spec {
pub fn of(entry: &Entry) -> Self {
match &entry.source {
Source::Git { url } => Self::Git {
url: url.clone(),
version: entry.version.clone(),
},
Source::Path { path } => Self::Path(path.to_string_lossy().into_owned()),
}
}
pub fn constraint(&self) -> Constraint {
match self {
Self::Git { version, .. } => crate::version::parse_constraint(version.as_deref()),
Self::Path(_) => Constraint::Latest,
}
}
}
#[derive(Debug, Clone)]
pub struct Dependency {
pub name: String,
pub declared: Spec,
pub spec: Spec,
pub subpath: Option<SourceSubpath>,
pub plugin: Option<String>,
pub constraint: Constraint,
pub required_by: String,
pub declared_in: Option<Rooted>,
pub dialect: Option<Dialect>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Mode {
Sync,
Frozen,
Upgrade { targets: BTreeSet<String> },
}
#[derive(Debug, Clone, Default)]
pub struct Bounds {
pub packages: Option<usize>,
pub bytes: Option<u64>,
}
pub struct Request {
pub root: Vec<Dependency>,
pub supplied: BTreeMap<String, Arc<Prepared>>,
pub overrides: BTreeMap<String, String>,
pub locked: BTreeMap<String, LockedDependency>,
pub mode: Mode,
pub engine: Engine,
pub bounds: Bounds,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Ref {
pub version: Option<Version>,
pub tag: Option<String>,
pub commit: Option<String>,
pub origin: Origin,
}
#[derive(Clone)]
pub struct Node {
pub name: String,
pub declared: SourceId,
pub id: SourceId,
pub fetch_url: Option<String>,
pub resolved: Ref,
pub rooted: Rooted,
pub plugin: Option<PluginRoot>,
pub prepared: Arc<Prepared>,
pub normalized: BTreeMap<String, Vec<SchemaDiagnostic>>,
}
pub fn locked_dependency(
id: &SourceId,
tag: Option<&str>,
commit: Option<&str>,
plugin: Option<&PluginRoot>,
) -> LockedDependency {
let (url, path, subpath) = match id {
SourceId::Git { url, subpath, .. } => (Some(url.clone()), None, subpath.clone()),
SourceId::Path {
canonical, subpath, ..
} => (None, Some(canonical.clone()), subpath.clone()),
};
LockedDependency {
url,
path,
subpath,
version: tag.map(str::to_owned),
commit: commit.map(str::to_owned),
plugin: plugin.and_then(|plugin| plugin.locked_name().map(str::to_owned)),
content: plugin.and_then(|plugin| plugin.locked_content().cloned()),
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EngineFallbackSkipped {
pub version: String,
pub failures: Vec<EngineFailure>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EngineFallback {
pub source: String,
pub skipped: Vec<EngineFallbackSkipped>,
pub selected_version: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Level {
Warning,
Advisory,
Info,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Diagnostic {
Note {
level: Level,
code: String,
message: String,
once: bool,
},
Registered { package: String },
}
pub struct Resolved {
pub nodes: IndexMap<String, Node>,
pub constraints: BTreeMap<String, Vec<(String, Constraint)>>,
pub engine_fallbacks: Vec<EngineFallback>,
pub diagnostics: Vec<Diagnostic>,
}
pub struct Failed {
pub failure: Failure,
pub diagnostics: Vec<Diagnostic>,
}
pub enum Outcome {
Needs(Vec<Need>),
Resolved(Resolved),
Failed(Failed),
}
pub fn resolve(request: &Request, answers: &dyn Answers) -> Outcome {
package::Run::new(request, answers).run()
}
#[cfg(test)]
mod tests;