1mod engine;
14mod failure;
15mod lock;
16mod package;
17mod plugin;
18mod version;
19pub mod wire;
20
21use std::collections::{BTreeMap, BTreeSet};
22use std::fmt;
23use std::rc::Rc;
24use std::sync::Arc;
25
26use indexmap::IndexMap;
27use semver::Version;
28
29use crate::dependency::{Entry, Source};
30use crate::diagnostic::SchemaDiagnostic;
31use crate::dialect::Dialect;
32use crate::names::SourceSubpath;
33use crate::prepare::Prepared;
34use crate::tree::TreeSource;
35use crate::version::Constraint;
36
37pub use engine::{BadVersion, Engine, EngineFailure};
38pub use failure::{BoundsLimit, Failure, Identity, PluginDetail, Requester, Step, SubpathDetail};
39pub use lock::{LockedContent, LockedDependency, PluginRoot};
40pub use package::sort_tags;
41
42#[derive(Debug, Clone, PartialEq, Eq)]
44pub struct Tag {
45 pub tag: String,
46 pub commit: String,
47}
48
49#[derive(Debug, Clone, PartialEq, Eq)]
51pub struct TagVersion {
52 pub version: Version,
53 pub tag: String,
54 pub commit: String,
55}
56
57#[derive(Debug, Clone)]
60pub enum Answer<T> {
61 Found(T),
62 Missing,
63 Failed(FailedFetch),
64}
65
66#[derive(Debug, Clone, PartialEq, Eq)]
68pub enum FailedFetch {
69 Fetch(String),
71 Subpath(SubpathDetail),
74}
75
76impl fmt::Display for FailedFetch {
77 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78 match self {
79 Self::Fetch(reason) => f.write_str(reason),
80 Self::Subpath(detail) => write!(f, "the folder {}", detail.reason()),
81 }
82 }
83}
84
85#[derive(Debug, Clone)]
88pub struct PathRequest<'a> {
89 pub name: &'a str,
90 pub path: &'a str,
91 pub subpath: Option<&'a str>,
92 pub declared_in: Option<&'a Rooted>,
93}
94
95#[derive(Clone)]
97pub struct PathSource {
98 pub canonical: String,
99 pub tree: Rc<dyn TreeSource>,
100}
101
102pub trait Answers {
105 fn tags(&self, url: &str) -> Option<Answer<Vec<Tag>>>;
106 fn reference(&self, url: &str, name: &str) -> Option<Answer<String>>;
107 fn tree(
108 &self,
109 url: &str,
110 commit: &str,
111 path: Option<&str>,
112 ) -> Option<Answer<Rc<dyn TreeSource>>>;
113 fn path(&self, request: &PathRequest<'_>) -> Answer<PathSource>;
114 fn take_read_error(&self) -> Option<String> {
117 None
118 }
119}
120
121#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
123pub enum Need {
124 Tags {
125 url: String,
126 },
127 Ref {
128 url: String,
129 name: String,
130 },
131 Tree {
132 url: String,
133 commit: String,
134 path: Option<String>,
135 },
136}
137
138impl fmt::Display for Need {
139 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
140 match self {
141 Self::Tags { url } => write!(f, "tags of {url}"),
142 Self::Ref { url, name } => write!(f, "ref `{name}` of {url}"),
143 Self::Tree { url, commit, path } => match path {
144 Some(path) => write!(f, "files of {url} at {commit} under {path}"),
145 None => write!(f, "files of {url} at {commit}"),
146 },
147 }
148 }
149}
150
151#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
153pub enum Origin {
154 Git { url: String, commit: String },
155 Path { dir: String },
156}
157
158#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
160pub struct Rooted {
161 pub checkout: Origin,
162 pub subpath: Option<String>,
163}
164
165#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
167pub enum SourceId {
168 Git {
169 url: String,
170 subpath: Option<SourceSubpath>,
171 plugin: Option<String>,
172 },
173 Path {
174 canonical: String,
175 subpath: Option<SourceSubpath>,
176 plugin: Option<String>,
177 },
178}
179
180impl SourceId {
181 pub fn plugin(&self) -> Option<&str> {
182 match self {
183 Self::Git { plugin, .. } | Self::Path { plugin, .. } => plugin.as_deref(),
184 }
185 }
186}
187
188impl fmt::Display for SourceId {
189 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
190 let (subpath, plugin) = match self {
191 Self::Git {
192 url,
193 subpath,
194 plugin,
195 } => {
196 write!(f, "git:{url}")?;
197 (subpath, plugin)
198 }
199 Self::Path {
200 canonical,
201 subpath,
202 plugin,
203 } => {
204 write!(f, "path:{canonical}")?;
205 (subpath, plugin)
206 }
207 };
208 if let Some(subpath) = subpath {
209 write!(f, "@{subpath}")?;
210 }
211 if let Some(plugin) = plugin {
212 write!(f, " (plugin {plugin})")?;
213 }
214 Ok(())
215 }
216}
217
218#[derive(Debug, Clone, PartialEq, Eq)]
220pub enum Spec {
221 Git {
222 url: String,
223 version: Option<String>,
224 },
225 Path(String),
226}
227
228impl Spec {
229 pub fn of(entry: &Entry) -> Self {
231 match &entry.source {
232 Source::Git { url } => Self::Git {
233 url: url.clone(),
234 version: entry.version.clone(),
235 },
236 Source::Path { path } => Self::Path(path.to_string_lossy().into_owned()),
237 }
238 }
239
240 pub fn constraint(&self) -> Constraint {
241 match self {
242 Self::Git { version, .. } => crate::version::parse_constraint(version.as_deref()),
243 Self::Path(_) => Constraint::Latest,
244 }
245 }
246}
247
248#[derive(Debug, Clone)]
250pub struct Dependency {
251 pub name: String,
252 pub declared: Spec,
254 pub spec: Spec,
256 pub subpath: Option<SourceSubpath>,
257 pub plugin: Option<String>,
258 pub constraint: Constraint,
259 pub required_by: String,
260 pub declared_in: Option<Rooted>,
262 pub dialect: Option<Dialect>,
264 pub namespace: Option<String>,
270}
271
272#[derive(Debug, Clone, PartialEq, Eq)]
274pub enum Mode {
275 Sync,
277 Frozen,
279 Upgrade { targets: BTreeSet<String> },
281}
282
283#[derive(Debug, Clone, Default)]
284pub struct Bounds {
285 pub packages: Option<usize>,
286 pub bytes: Option<u64>,
287}
288
289pub struct Request {
290 pub root: Vec<Dependency>,
291 pub supplied: BTreeMap<String, Arc<Prepared>>,
293 pub overrides: BTreeMap<String, String>,
295 pub locked: BTreeMap<String, LockedDependency>,
296 pub mode: Mode,
297 pub engine: Engine,
298 pub bounds: Bounds,
299}
300
301#[derive(Debug, Clone, PartialEq, Eq)]
304pub struct Ref {
305 pub version: Option<Version>,
306 pub tag: Option<String>,
307 pub commit: Option<String>,
308 pub origin: Origin,
309}
310
311#[derive(Clone)]
312pub struct Node {
313 pub name: String,
314 pub declared: SourceId,
315 pub id: SourceId,
316 pub fetch_url: Option<String>,
318 pub resolved: Ref,
319 pub rooted: Rooted,
322 pub plugin: Option<PluginRoot>,
323 pub prepared: Arc<Prepared>,
324 pub normalized: BTreeMap<String, Vec<SchemaDiagnostic>>,
326}
327
328pub fn locked_dependency(
330 id: &SourceId,
331 tag: Option<&str>,
332 commit: Option<&str>,
333 plugin: Option<&PluginRoot>,
334) -> LockedDependency {
335 let (url, path, subpath) = match id {
336 SourceId::Git { url, subpath, .. } => (Some(url.clone()), None, subpath.clone()),
337 SourceId::Path {
338 canonical, subpath, ..
339 } => (None, Some(canonical.clone()), subpath.clone()),
340 };
341 LockedDependency {
342 url,
343 path,
344 subpath,
345 version: tag.map(str::to_owned),
346 commit: commit.map(str::to_owned),
347 plugin: plugin.and_then(|plugin| plugin.locked_name().map(str::to_owned)),
348 content: plugin.and_then(|plugin| plugin.locked_content().cloned()),
349 }
350}
351
352#[derive(Debug, Clone, PartialEq, Eq)]
353pub struct EngineFallbackSkipped {
354 pub version: String,
355 pub failures: Vec<EngineFailure>,
356}
357
358#[derive(Debug, Clone, PartialEq, Eq)]
360pub struct EngineFallback {
361 pub source: String,
362 pub skipped: Vec<EngineFallbackSkipped>,
363 pub selected_version: String,
364}
365
366#[derive(Debug, Clone, Copy, PartialEq, Eq)]
367pub enum Level {
368 Warning,
370 Advisory,
372 Info,
373}
374
375#[derive(Debug, Clone, PartialEq, Eq)]
376pub enum Diagnostic {
377 Note {
378 level: Level,
379 code: String,
380 message: String,
381 once: bool,
383 },
384 Registered { package: String },
387}
388
389pub struct Resolved {
390 pub nodes: IndexMap<String, Node>,
392 pub constraints: BTreeMap<String, Vec<(String, Constraint)>>,
394 pub engine_fallbacks: Vec<EngineFallback>,
395 pub diagnostics: Vec<Diagnostic>,
396}
397
398pub struct Failed {
399 pub failure: Failure,
400 pub diagnostics: Vec<Diagnostic>,
401}
402
403pub enum Outcome {
404 Needs(Vec<Need>),
406 Resolved(Resolved),
407 Failed(Failed),
409}
410
411pub fn resolve(request: &Request, answers: &dyn Answers) -> Outcome {
413 package::Run::new(request, answers).run()
414}
415
416#[cfg(test)]
417mod tests;