1use semver::Version;
2
3use super::engine::{EngineFailure, describe};
4use super::{Need, Rooted};
5use crate::diagnostic::SchemaDiagnostic;
6
7#[derive(Debug, Clone)]
10pub enum Failure {
11 Conflict {
13 name: String,
14 requesters: Vec<Requester>,
15 detail: String,
16 },
17 Identity(Identity),
19 EngineIncompatible {
20 name: String,
21 failures: Vec<EngineFailure>,
22 message: String,
23 },
24 EngineUnsatisfiable {
26 name: String,
27 excluded: Vec<(Version, Vec<EngineFailure>)>,
28 },
29 EngineRequirementInvalid {
30 name: String,
31 engine: String,
32 requirement: String,
33 message: String,
34 },
35 EngineVersionInvalid {
36 engine: String,
37 version: String,
38 message: String,
39 },
40 Plugin {
41 name: String,
42 detail: PluginDetail,
43 plugin: Option<String>,
44 text: String,
45 },
46 Subpath {
47 name: String,
48 subpath: String,
49 detail: SubpathDetail,
50 },
51 Unreadable {
53 name: String,
54 rooted: Rooted,
55 diagnostics: Vec<SchemaDiagnostic>,
56 },
57 Path {
59 name: String,
60 declared_in: String,
61 },
62 Unavailable {
64 package: String,
66 need: Need,
67 reason: String,
68 },
69 Bounds {
70 limit: BoundsLimit,
71 at: String,
72 },
73 Oscillation {
75 name: String,
76 steps: Vec<Step>,
77 },
78 Frozen {
80 message: String,
81 },
82}
83
84#[derive(Debug, Clone, PartialEq, Eq)]
85pub struct Requester {
86 pub by: String,
87 pub constraint: String,
88}
89
90#[derive(Debug, Clone, PartialEq, Eq)]
91pub enum Identity {
92 OneSourceTwoNames {
93 existing: String,
94 duplicate: String,
95 source: String,
96 },
97 OneNameTwoSources {
98 name: String,
99 existing: String,
100 incoming: String,
101 },
102}
103
104#[derive(Debug, Clone, Copy, PartialEq, Eq)]
105pub enum PluginDetail {
106 NotAMarketplace,
107 NotListed,
108 Refused,
109 ChoiceNeeded,
110 NotOnePackage,
111 UnusablePath,
112}
113
114#[derive(Debug, Clone, Copy, PartialEq, Eq)]
115pub enum SubpathDetail {
116 Missing,
117 NotADirectory,
118 Traversal,
119}
120
121impl SubpathDetail {
122 pub fn reason(self) -> &'static str {
124 match self {
125 Self::Missing => "does not exist in the source",
126 Self::NotADirectory => "is not a directory in the source",
127 Self::Traversal => "escapes the source",
128 }
129 }
130}
131
132#[derive(Debug, Clone, Copy, PartialEq, Eq)]
133pub enum BoundsLimit {
134 Packages,
135 Bytes,
136}
137
138#[derive(Debug, Clone, PartialEq, Eq)]
139pub struct Step {
140 pub version: Option<String>,
141 pub commit: Option<String>,
142}
143
144impl Failure {
145 pub fn message(&self) -> String {
147 match self {
148 Self::Conflict { name, detail, .. } => {
149 format!("version conflict for `{name}`: {detail}")
150 }
151 Self::Identity(Identity::OneSourceTwoNames {
152 existing,
153 duplicate,
154 source,
155 }) => format!(
156 "duplicate source identity: `{existing}` and `{duplicate}` both resolve to `{source}`"
157 ),
158 Self::Identity(Identity::OneNameTwoSources {
159 name,
160 existing,
161 incoming,
162 }) => format!(
163 "source `{name}` was referenced with conflicting identities: existing `{existing}`, incoming `{incoming}`"
164 ),
165 Self::EngineIncompatible { name, message, .. } => {
166 format!("source `{name}` is incompatible with the running engine: {message}")
167 }
168 Self::EngineUnsatisfiable { name, excluded } => format!(
169 "engine requirements are unsatisfiable for `{name}`: {}",
170 unsatisfiable_detail(excluded)
171 ),
172 Self::EngineRequirementInvalid {
173 name,
174 engine,
175 requirement,
176 message,
177 } => format!(
178 "invalid {engine} version requirement `{requirement}` in package `{name}`: {message}"
179 ),
180 Self::EngineVersionInvalid {
181 engine,
182 version,
183 message,
184 } => format!("invalid running {engine} version `{version}`: {message}"),
185 Self::Plugin { text, .. } => format!("invalid request: {text}"),
186 Self::Subpath {
187 name,
188 subpath,
189 detail,
190 } => format!(
191 "source error: {name}: subpath `{subpath}` {}",
192 detail.reason()
193 ),
194 Self::Unreadable {
195 name, diagnostics, ..
196 } => format!(
197 "failed to read the manifest of `{name}`: {}",
198 diagnostics
199 .iter()
200 .map(|diagnostic| diagnostic.message.as_str())
201 .collect::<Vec<_>>()
202 .join("; ")
203 ),
204 Self::Path { name, declared_in } => format!(
205 "`{declared_in}` declares a path dependency `{name}`, which this resolution cannot read: a path names a folder on one machine"
206 ),
207 Self::Unavailable {
208 package,
209 need,
210 reason,
211 } => format!("{package}: {need}: {reason}"),
212 Self::Bounds { limit, at } => match limit {
213 BoundsLimit::Packages => {
214 format!("the dependency graph is over its package limit at `{at}`")
215 }
216 BoundsLimit::Bytes => {
217 format!("the dependency graph is over its size limit at `{at}`")
218 }
219 },
220 Self::Oscillation { name, steps } => format!(
221 "version conflict for `{name}`: {}",
222 oscillation_detail(name, steps)
223 ),
224 Self::Frozen { message } => format!("frozen violation: {message}"),
225 }
226 }
227
228 pub fn conflict_detail(&self) -> Option<String> {
230 match self {
231 Self::Conflict { detail, .. } => Some(detail.clone()),
232 Self::Oscillation { name, steps } => Some(oscillation_detail(name, steps)),
233 _ => None,
234 }
235 }
236
237 pub fn unsatisfiable_detail(&self) -> Option<String> {
239 match self {
240 Self::EngineUnsatisfiable { excluded, .. } => Some(unsatisfiable_detail(excluded)),
241 _ => None,
242 }
243 }
244}
245
246pub(super) fn unsatisfiable_detail(excluded: &[(Version, Vec<EngineFailure>)]) -> String {
247 let candidates = excluded
248 .iter()
249 .map(|(version, failures)| format!(" {version}: {}", describe(failures)))
250 .collect::<Vec<_>>()
251 .join("\n");
252 format!("no compatible candidate remains:\n{candidates}")
253}
254
255fn oscillation_detail(name: &str, steps: &[Step]) -> String {
256 let cycle: Vec<String> = steps
257 .iter()
258 .map(|step| {
259 format!(
260 "{}@{}",
261 step.version.as_deref().unwrap_or("no-version"),
262 step.commit.as_deref().unwrap_or("no-commit")
263 )
264 })
265 .collect();
266 format!(
267 "resolution oscillation detected for `{name}`: {}",
268 cycle.join(" -> ")
269 )
270}