+15
-17
lines changedFilter options
+15
-17
lines changed Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ use serde::{Deserialize, Serialize};
12
12
use super::errors::Error;
13
13
use super::errors::Result;
14
14
use super::parser;
15
-
use super::utils::PercentCodec;
15
+
use super::utils::{to_lowercase, PercentCodec};
16
16
use super::validation;
17
17
18
18
const ENCODE_SET: &AsciiSet = &percent_encoding::CONTROLS
@@ -84,21 +84,14 @@ impl<'a> PackageUrl<'a> {
84
84
let mut t = ty.into();
85
85
let mut n = name.into();
86
86
if validation::is_type_valid(&t) {
87
-
// lowercase type if needed
88
-
if !t.chars().all(|c| c.is_uppercase()) {
89
-
t = Cow::Owned(t.to_lowercase());
90
-
}
87
+
t = to_lowercase(t);
91
88
// lowercase name if required by type and needed
92
89
match t.as_ref() {
93
90
"bitbucket" | "deb" | "github" | "hex" | "npm" => {
94
-
if !n.chars().all(|c| c.is_uppercase()) {
95
-
n = Cow::Owned(n.to_lowercase());
96
-
}
91
+
n = to_lowercase(n);
97
92
}
98
93
"pypi" => {
99
-
if !n.chars().all(|c| c.is_uppercase()) {
100
-
n = Cow::Owned(n.to_lowercase());
101
-
}
94
+
n = to_lowercase(n);
102
95
if n.chars().any(|c| c == '_') {
103
96
n = Cow::Owned(n.replace('_', "-"));
104
97
}
@@ -166,9 +159,7 @@ impl<'a> PackageUrl<'a> {
166
159
let mut n = namespace.into();
167
160
match self.ty.as_ref() {
168
161
"bitbucket" | "deb" | "github" | "golang" | "hex" | "rpm" => {
169
-
if n.chars().any(|c| c.is_uppercase()) {
170
-
n = Cow::Owned(n.to_lowercase());
171
-
}
162
+
n = to_lowercase(n);
172
163
}
173
164
_ => {}
174
165
}
@@ -238,9 +229,7 @@ impl<'a> PackageUrl<'a> {
238
229
if !validation::is_qualifier_key_valid(&k) {
239
230
Err(Error::InvalidKey(k.into()))
240
231
} else {
241
-
if k.chars().any(|c| c.is_uppercase()) {
242
-
k = Cow::Owned(k.to_lowercase());
243
-
}
232
+
k = to_lowercase(k);
244
233
self.qualifiers.insert(k, value.into());
245
234
Ok(self)
246
235
}
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ mod quickfind;
3
3
4
4
pub use self::encodable::PercentCodec;
5
5
pub use self::quickfind::QuickFind;
6
+
use std::borrow::Cow;
6
7
7
8
pub fn rcut(input: &str, sep: u8) -> (&str, &str) {
8
9
if let Some(i) = input.quickrfind(sep) {
@@ -20,6 +21,14 @@ pub fn cut(input: &str, sep: u8) -> (&str, &str) {
20
21
}
21
22
}
22
23
24
+
pub(crate) fn to_lowercase(s: Cow<str>) -> Cow<str> {
25
+
if !s.chars().any(|c| c.is_uppercase()) {
26
+
Cow::Owned(s.to_lowercase())
27
+
} else {
28
+
s
29
+
}
30
+
}
31
+
23
32
#[cfg(test)]
24
33
mod tests {
25
34
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4