forked from lycheeverse/lychee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompact.rs
192 lines (157 loc) · 5.83 KB
/
compact.rs
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
use anyhow::Result;
use console::Style;
use once_cell::sync::Lazy;
use std::{
fmt::{self, Display},
time::Duration,
};
use crate::formatters::color::{color, BOLD_GREEN, BOLD_PINK, BOLD_YELLOW, DIM, NORMAL};
use crate::{formatters::get_response_formatter, options, stats::ResponseStats};
use super::StatsFormatter;
struct CompactResponseStats {
stats: ResponseStats,
mode: options::OutputMode,
}
impl Display for CompactResponseStats {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let stats = &self.stats;
if !stats.error_map.is_empty() {
let input = if stats.error_map.len() == 1 {
"input"
} else {
"inputs"
};
color!(
f,
BOLD_PINK,
"Issues found in {} {input}. Find details below.\n\n",
stats.error_map.len()
)?;
}
let response_formatter = get_response_formatter(&self.mode);
for (source, responses) in super::sort_stat_map(&stats.error_map) {
color!(f, BOLD_YELLOW, "[{}]:\n", source)?;
for response in responses {
writeln!(
f,
"{}",
response_formatter.format_detailed_response(response)
)?;
}
if let Some(suggestions) = stats.suggestion_map.get(source) {
// Sort suggestions
let mut sorted_suggestions: Vec<_> = suggestions.iter().collect();
sorted_suggestions.sort_by(|a, b| {
let (a, b) = (a.to_string().to_lowercase(), b.to_string().to_lowercase());
human_sort::compare(&a, &b)
});
writeln!(f, "\n\u{2139} Suggestions")?;
for suggestion in sorted_suggestions {
writeln!(f, "{suggestion}")?;
}
}
writeln!(f)?;
}
color!(f, NORMAL, "🔍 {} Total", stats.total)?;
// show duration (in a human readable format), e.g. 2m 30s
let duration = Duration::from_secs(stats.duration_secs);
color!(f, DIM, " (in {})", humantime::format_duration(duration))?;
color!(f, BOLD_GREEN, " ✅ {} OK", stats.successful)?;
let total_errors = stats.errors;
let err_str = if total_errors == 1 { "Error" } else { "Errors" };
color!(f, BOLD_PINK, " 🚫 {} {}", total_errors, err_str)?;
write_if_any(stats.unknown, "❓", "Unknown", &BOLD_PINK, f)?;
write_if_any(stats.excludes, "👻", "Excluded", &BOLD_YELLOW, f)?;
write_if_any(stats.timeouts, "⏳", "Timeouts", &BOLD_YELLOW, f)?;
Ok(())
}
}
fn write_if_any(
value: usize,
symbol: &str,
text: &str,
style: &Lazy<Style>,
f: &mut fmt::Formatter<'_>,
) -> Result<(), fmt::Error> {
if value > 0 {
color!(f, style, " {} {} {}", symbol, value, text)?;
}
Ok(())
}
pub(crate) struct Compact {
mode: options::OutputMode,
}
impl Compact {
pub(crate) const fn new(mode: options::OutputMode) -> Self {
Self { mode }
}
}
impl StatsFormatter for Compact {
fn format(&self, stats: ResponseStats) -> Result<Option<String>> {
let compact = CompactResponseStats {
stats,
mode: self.mode.clone(),
};
Ok(Some(compact.to_string()))
}
}
#[cfg(test)]
mod tests {
use crate::formatters::stats::StatsFormatter;
use crate::{options::OutputMode, stats::ResponseStats};
use http::StatusCode;
use lychee_lib::{InputSource, ResponseBody, Status, Uri};
use std::collections::{HashMap, HashSet};
use url::Url;
use super::*;
#[test]
fn test_formatter() {
// A couple of dummy successes
let mut success_map: HashMap<InputSource, HashSet<ResponseBody>> = HashMap::new();
success_map.insert(
InputSource::RemoteUrl(Box::new(Url::parse("https://example.com").unwrap())),
HashSet::from_iter(vec![ResponseBody {
uri: Uri::from(Url::parse("https://example.com").unwrap()),
status: Status::Ok(StatusCode::OK),
}]),
);
let err1 = ResponseBody {
uri: Uri::try_from("https://github.com/mre/idiomatic-rust-doesnt-exist-man").unwrap(),
status: Status::Ok(StatusCode::NOT_FOUND),
};
let err2 = ResponseBody {
uri: Uri::try_from("https://github.com/mre/boom").unwrap(),
status: Status::Ok(StatusCode::INTERNAL_SERVER_ERROR),
};
let mut error_map: HashMap<InputSource, HashSet<ResponseBody>> = HashMap::new();
let source = InputSource::RemoteUrl(Box::new(Url::parse("https://example.com").unwrap()));
error_map.insert(source, HashSet::from_iter(vec![err1, err2]));
let stats = ResponseStats {
total: 1,
successful: 1,
errors: 2,
unknown: 0,
excludes: 0,
timeouts: 0,
duration_secs: 0,
error_map,
suggestion_map: HashMap::default(),
unsupported: 0,
redirects: 0,
cached: 0,
success_map,
excluded_map: HashMap::default(),
detailed_stats: false,
};
let formatter = Compact::new(OutputMode::Plain);
let result = formatter.format(stats).unwrap().unwrap();
println!("{result}");
assert!(result.contains("🔍 1 Total"));
assert!(result.contains("✅ 1 OK"));
assert!(result.contains("🚫 2 Errors"));
assert!(result.contains("[https://example.com/]:"));
assert!(result
.contains("https://github.com/mre/idiomatic-rust-doesnt-exist-man | 404 Not Found"));
assert!(result.contains("https://github.com/mre/boom | 500 Internal Server Error"));
}
}