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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
use byte_unit::Byte;
use std::{collections::HashSet, future::Future};
pub fn run_future<F, T>(input: F) -> T
where
F: Future<Output = T>,
{
let runtime = tokio::runtime::Runtime::new().unwrap();
runtime.block_on(input)
}
pub fn humanize_bytes(bytes: u64) -> String {
Byte::from_bytes(bytes)
.get_appropriate_unit(true)
.to_string()
}
pub fn byte_string_to_number(byte_string: &str) -> Option<u64> {
Byte::from_str(byte_string).map(|b| b.into()).ok()
}
pub fn replace_invalid_chars_in_filename(input: &str) -> String {
let replacement: char = ' ';
let invalid_chars: Vec<char> =
vec!['/', '\\', '?', '%', '*', ':', '|', '"', '<', '>', ';', '='];
input
.chars()
.map(|c| {
if invalid_chars.contains(&c) {
replacement
} else {
c
}
})
.collect::<String>()
}
pub fn extract_filename_from_url(url: &str) -> Option<String> {
let url = reqwest::Url::parse(url);
if url.is_err() {
return None;
}
let url = url.unwrap();
let path_segments: Vec<&str> = url.path_segments()?.collect();
match path_segments[path_segments.len() - 1] {
"" => None,
segment => Some(segment.to_string()),
}
}
pub fn str_vectors_intersect<T1, T2>(first: &[T1], second: &[T2]) -> bool
where
T1: AsRef<str>,
T2: AsRef<str>,
{
if first.is_empty() || second.is_empty() {
return false;
}
let mut first_set = HashSet::new();
for first_item in first {
first_set.insert(first_item.as_ref().to_lowercase());
}
for second_item in second {
if first_set.contains(&second_item.as_ref().to_lowercase()) {
return true;
}
}
false
}
pub fn parse_usize_range(value: &str, max_value: usize) -> Option<Vec<usize>> {
let dash_idx = value.find('-');
if dash_idx == None {
return value.parse::<usize>().map(|v| vec![v]).ok();
}
let dash_idx = dash_idx.unwrap();
let left = &value[0..dash_idx];
let right = &value[dash_idx + 1..];
let range_left = if !left.is_empty() {
match left.parse::<usize>() {
Ok(v) => v,
Err(_) => return None,
}
} else {
1
};
let range_right = if !right.is_empty() {
match right.parse::<usize>() {
Ok(v) => v,
Err(_) => return None,
}
} else {
max_value
};
Some((range_left..range_right + 1).collect())
}
pub fn union_usize_ranges(values: &[&str], max_value: usize) -> Result<Vec<usize>, anyhow::Error> {
let mut invalid_values = vec![];
let mut parsed = HashSet::new();
for &v in values {
match parse_usize_range(v, max_value) {
Some(usize_values) => parsed.extend(usize_values),
None => invalid_values.push(v),
}
}
if !invalid_values.is_empty() {
let msg = invalid_values
.into_iter()
.map(|v| format!("'{}'", v))
.collect::<Vec<_>>()
.join(", ");
return Err(anyhow::anyhow!("{}", msg));
}
let mut output = Vec::from_iter(parsed);
output.sort();
Ok(output)
}
#[test]
fn test_remove_invalid_chars() {
assert_eq!(
replace_invalid_chars_in_filename("Humble Bundle: Nice book"),
"Humble Bundle Nice book".to_string()
);
}
#[test]
fn test_extract_filename_from_url() {
let test_data = vec![(
"with filename",
"https://dl.humble.com/grokkingalgorithms.mobi?gamekey=xxxxxx&ttl=1655031034&t=yyyyyyyyyy",
Some("grokkingalgorithms.mobi".to_string()),
), (
"no filename",
"https://www.google.com/",
None
)];
for (name, url, expected) in test_data {
assert_eq!(
extract_filename_from_url(url),
expected,
"test case '{}'",
name
);
}
}
#[test]
fn test_vectors_intersect() {
let test_data = vec![
(vec!["FOO", "bar"], vec!["foo"], true),
(vec!["foo", "bar"], vec!["baz"], false),
(vec!["foo"], vec![], false),
(vec![], vec!["baz"], false),
];
for (first, second, result) in test_data {
let msg = format!(
"intersect of {:?} and {:?}, expected: {}",
first, second, result
);
assert_eq!(str_vectors_intersect(&first, &second), result, "{}", msg);
}
}
#[test]
fn test_parse_usize_range() {
const MAX_VAL: usize = 50;
let test_data = vec![
("empty string", "", None),
("invalid string", "abcd", None),
("single value", "42", Some(vec![42])),
(
"range with start and end",
"5-10",
Some(vec![5, 6, 7, 8, 9, 10]),
),
("range with no start", "-5", Some(vec![1, 2, 3, 4, 5])),
(
"range with no end",
"45-",
Some(vec![45, 46, 47, 48, 49, 50]),
), ("invalid start", "abc-", None),
("invalid end", "-abc", None),
("invalid start and end", "abc-def", None),
];
for (name, input, expected) in test_data {
let msg = format!(
"'{}' failed: input = {}, expected = {:?}",
name, input, &expected
);
assert_eq!(parse_usize_range(input, MAX_VAL), expected, "{}", msg);
}
}
#[test]
fn test_union_valid_usize_ranges() {
const MAX_VAL: usize = 10;
let test_data = vec![
("simple values", vec!["5", "10"], vec![5, 10]),
("simple value and range", vec!["8", "7-"], vec![7, 8, 9, 10]),
("two ranges", vec!["-3", "7-"], vec![1, 2, 3, 7, 8, 9, 10]),
];
for (name, input, expected) in test_data {
let output = union_usize_ranges(&input, MAX_VAL);
let msg = format!(
"'{}' failed: input = {:?}, expected = {:?}",
name, &input, &expected
);
assert!(output.is_ok(), "{}", msg);
assert_eq!(output.unwrap(), expected, "{}", msg);
}
}
#[test]
fn test_union_invalid_usize_ranges() {
const MAX_VAL: usize = 10;
let test_data = vec![
("invalid simple values", vec!["a", "b"]),
("invalid ranges", vec!["a-", "-b"]),
];
for (name, input) in test_data {
let expected_err_msg = input
.iter()
.map(|v| format!("'{}'", v))
.collect::<Vec<_>>()
.join(", ");
let output = union_usize_ranges(&input, MAX_VAL);
let assert_msg = format!(
"'{}' failed: input = {:?}, expected = {:?}",
name, &input, &expected_err_msg
);
assert!(output.is_err(), "{}", assert_msg);
let output_err_msg: String = output.unwrap_err().downcast().unwrap();
assert_eq!(output_err_msg, expected_err_msg, "{}", assert_msg);
}
}