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}
265
266#[derive(Debug, Clone, PartialEq, Eq)]
268pub enum Mode {
269 Sync,
271 Frozen,
273 Upgrade { targets: BTreeSet<String> },
275}
276
277#[derive(Debug, Clone, Default)]
278pub struct Bounds {
279 pub packages: Option<usize>,
280 pub bytes: Option<u64>,
281}
282
283pub struct Request {
284 pub root: Vec<Dependency>,
285 pub supplied: BTreeMap<String, Arc<Prepared>>,
287 pub overrides: BTreeMap<String, String>,
289 pub locked: BTreeMap<String, LockedDependency>,
290 pub mode: Mode,
291 pub engine: Engine,
292 pub bounds: Bounds,
293}
294
295#[derive(Debug, Clone, PartialEq, Eq)]
298pub struct Ref {
299 pub version: Option<Version>,
300 pub tag: Option<String>,
301 pub commit: Option<String>,
302 pub origin: Origin,
303}
304
305#[derive(Clone)]
306pub struct Node {
307 pub name: String,
308 pub declared: SourceId,
309 pub id: SourceId,
310 pub fetch_url: Option<String>,
312 pub resolved: Ref,
313 pub rooted: Rooted,
316 pub plugin: Option<PluginRoot>,
317 pub prepared: Arc<Prepared>,
318 pub normalized: BTreeMap<String, Vec<SchemaDiagnostic>>,
320}
321
322pub fn locked_dependency(
324 id: &SourceId,
325 tag: Option<&str>,
326 commit: Option<&str>,
327 plugin: Option<&PluginRoot>,
328) -> LockedDependency {
329 let (url, path, subpath) = match id {
330 SourceId::Git { url, subpath, .. } => (Some(url.clone()), None, subpath.clone()),
331 SourceId::Path {
332 canonical, subpath, ..
333 } => (None, Some(canonical.clone()), subpath.clone()),
334 };
335 LockedDependency {
336 url,
337 path,
338 subpath,
339 version: tag.map(str::to_owned),
340 commit: commit.map(str::to_owned),
341 plugin: plugin.and_then(|plugin| plugin.locked_name().map(str::to_owned)),
342 content: plugin.and_then(|plugin| plugin.locked_content().cloned()),
343 }
344}
345
346#[derive(Debug, Clone, PartialEq, Eq)]
347pub struct EngineFallbackSkipped {
348 pub version: String,
349 pub failures: Vec<EngineFailure>,
350}
351
352#[derive(Debug, Clone, PartialEq, Eq)]
354pub struct EngineFallback {
355 pub source: String,
356 pub skipped: Vec<EngineFallbackSkipped>,
357 pub selected_version: String,
358}
359
360#[derive(Debug, Clone, Copy, PartialEq, Eq)]
361pub enum Level {
362 Warning,
364 Advisory,
366 Info,
367}
368
369#[derive(Debug, Clone, PartialEq, Eq)]
370pub enum Diagnostic {
371 Note {
372 level: Level,
373 code: String,
374 message: String,
375 once: bool,
377 },
378 Registered { package: String },
381}
382
383pub struct Resolved {
384 pub nodes: IndexMap<String, Node>,
386 pub constraints: BTreeMap<String, Vec<(String, Constraint)>>,
388 pub engine_fallbacks: Vec<EngineFallback>,
389 pub diagnostics: Vec<Diagnostic>,
390}
391
392pub struct Failed {
393 pub failure: Failure,
394 pub diagnostics: Vec<Diagnostic>,
395}
396
397pub enum Outcome {
398 Needs(Vec<Need>),
400 Resolved(Resolved),
401 Failed(Failed),
403}
404
405pub fn resolve(request: &Request, answers: &dyn Answers) -> Outcome {
407 package::Run::new(request, answers).run()
408}
409
410#[cfg(test)]
411mod tests;