1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use crate::core::contract::Ecosystem;
use crate::core::model::{Report, Verdict};
use owo_colors::OwoColorize;
pub struct HumanReporter;
impl HumanReporter {
pub fn render(
report: &Report,
is_tree: bool,
should_update: bool,
ecosystems: &[Ecosystem],
) -> String {
let mut buf = String::new();
for manifest in &report.manifests {
// Find the ecosystem for this manifest
let eco = ecosystems.iter().find(|e| e.name == manifest.ecosystem);
let scheme = eco.map(|e| &*e.scheme);
// Print path (ecosystem) header
buf.push_str(&format!(
"{} ({})\n",
manifest.path.display().bold(),
manifest.ecosystem.bright_black()
));
for (dep, verdict) in &manifest.verdicts {
let coord_name = if dep.coordinate.0.is_empty() {
"(manifest)"
} else {
&dep.coordinate.0
};
match verdict {
Verdict::Current { latest, latest_pre } => {
if is_tree {
let version_str = latest.0.bright_black().to_string();
let pre_suffix = if let Some(pre) = latest_pre {
format!(" ({})", pre.0)
.magenta()
.dimmed()
.italic()
.to_string()
} else {
String::new()
};
buf.push_str(&format!(
" {} {}{}\n",
coord_name, version_str, pre_suffix
));
}
}
Verdict::Outdated {
compatible,
latest,
latest_pre,
} => {
let effective_target = compatible.as_ref().unwrap_or(latest);
let old_str = format_old_constraint(&dep.constraint.0, &effective_target.0);
let version_start = dep
.constraint
.0
.find(|c: char| c.is_ascii_digit())
.unwrap_or(0);
let operator = &dep.constraint.0[..version_start];
let target_is_pre = match scheme {
Some(s) => s.is_prerelease(effective_target),
None => is_prerelease(&effective_target.0),
};
let target_ver_str = if target_is_pre {
effective_target.0.magenta().to_string()
} else {
effective_target.0.green().to_string()
};
let target_str = format!("{}{}", operator.cyan(), target_ver_str);
let breaking_suffix = match compatible {
Some(compat) if compat.0 != latest.0 => {
format!(" (latest: {})", latest.0)
.bright_black()
.dimmed()
.italic()
.to_string()
}
None => format!(" (breaking: {})", latest.0)
.yellow()
.dimmed()
.italic()
.to_string(),
_ => String::new(),
};
let pre_suffix = if let Some(pre) = latest_pre {
format!(" (pre: {})", pre.0)
.magenta()
.dimmed()
.italic()
.to_string()
} else {
String::new()
};
buf.push_str(&format!(
" {} {} {} {}{}{}\n",
coord_name,
old_str,
"→".bright_black(),
target_str,
breaking_suffix,
pre_suffix,
));
if should_update {
let can_rewrite = scheme.map_or(true, |s| {
s.rewrite(&dep.constraint, effective_target).is_some()
});
if can_rewrite {
buf.push_str(&format!(" {}\n", "Updated!".green().italic()));
} else {
buf.push_str(&format!(
" {}\n",
"skipped: complex range".yellow().italic()
));
}
}
}
Verdict::Yanked => {
buf.push_str(&format!(" {} {}\n", coord_name, "! yanked".red().bold()));
}
Verdict::Unsatisfiable {
constraint,
prerelease_available,
} => {
let hint = if *prerelease_available {
format!(" (only prereleases available — retry with --pre)")
.dimmed()
.to_string()
} else {
String::new()
};
buf.push_str(&format!(
" {} {} {}{}\n",
coord_name,
constraint.0.bright_black(),
"✗ no versions found".red(),
hint
));
}
Verdict::Errored(e) => {
buf.push_str(&format!(" {} {} {}\n", coord_name, "✗ error:".red(), e));
}
}
}
}
buf
}
}
fn format_old_constraint(constraint: &str, target: &str) -> String {
let version_start = constraint.find(|c: char| c.is_ascii_digit()).unwrap_or(0);
let prefix_symbols = &constraint[..version_start];
let version_part = &constraint[version_start..];
let target_version_start = target.find(|c: char| c.is_ascii_digit()).unwrap_or(0);
let target_version_part = &target[target_version_start..];
let mut common_len = 0;
for (c1, c2) in version_part.chars().zip(target_version_part.chars()) {
if c1 == c2 {
common_len += c1.len_utf8();
} else {
break;
}
}
let (same_part, diff_part) = version_part.split_at(common_len);
format!(
"{}{}{}",
prefix_symbols.cyan(),
same_part.bright_black(),
diff_part.yellow(),
)
}
pub struct JsonReporter;
impl JsonReporter {
pub fn render(report: &Report) -> Result<String, serde_json::Error> {
serde_json::to_string_pretty(report)
}
}
fn is_prerelease(version: &str) -> bool {
version.contains('-')
}