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
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) -> String {
let mut buf = String::new();
for (dep, verdict) in &report.verdicts {
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",
dep.coordinate.0, version_str, pre_suffix
));
}
}
Verdict::Outdated {
compatible,
latest,
latest_pre,
} => {
// Determine the effective update target
let effective_target = compatible.as_ref().unwrap_or(latest);
let old_str = format_old_constraint(&dep.constraint.0, &effective_target.0);
let target_str = if is_prerelease(&effective_target.0) {
effective_target.0.magenta().to_string()
} else {
effective_target.0.green().to_string()
};
// Show breaking latest if it differs from compatible
let breaking_suffix = match compatible {
Some(compat) if compat.0 != latest.0 => format!(" (latest: {})", latest.0)
.bright_black()
.dimmed()
.italic()
.to_string(),
None => {
// No compatible version — latest is breaking-only
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",
dep.coordinate.0,
old_str,
"→".bright_black(),
target_str,
breaking_suffix,
pre_suffix,
));
if should_update
&& crate::core::is_actual_upgrade(&dep.constraint.0, &effective_target.0)
{
buf.push_str(&format!(" {}\n", "Updated!".green().italic()));
}
}
Verdict::Yanked => {
buf.push_str(&format!(
" {} {}\n",
dep.coordinate.0,
"! yanked".red().bold()
));
}
Verdict::Unsatisfiable { constraint } => {
buf.push_str(&format!(
" {} {} {}\n",
dep.coordinate.0,
constraint.0.bright_black(),
"✗ no versions found".red()
));
}
Verdict::Errored(e) => {
buf.push_str(&format!(
" {} {} {}\n",
dep.coordinate.0,
"✗ 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 mut common_len = 0;
for (c1, c2) in version_part.chars().zip(target.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.bright_black(),
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 {
semver::Version::parse(version)
.map(|v| !v.pre.is_empty())
.unwrap_or(false)
}