aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRichard Walters <rwalters@digitalstirling.com>2020-10-12 14:16:13 -0700
committerRichard Walters <rwalters@digitalstirling.com>2020-10-12 14:16:13 -0700
commitf9a5be09cded001ae6f4b073d9f4ee4602ffee50 (patch)
tree6e9a05cfb70f5f13bfe38c1873e048eb3c263397 /src
parentcf3d28248a036e7544cf899fd6f658d2863d4ec8 (diff)
Make Uri::check_scheme more functional
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs32
1 files changed, 16 insertions, 16 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 54538e5..1865b1a 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -18,6 +18,7 @@ use std::convert::TryFrom;
// from the ASCII character set.
//
// TODO: consider improvement
+//
// [14:49] silmeth: @rhymu8354 you might want to look at once_cell as a nicer
// macro-less replacement for lazystatic!()
lazy_static! {
@@ -553,22 +554,21 @@ impl Uri {
if scheme.is_empty() {
return Err(Error::EmptyScheme);
}
- // TODO: Improve on this by enumerating
- //
- // [16:20] everx80: you could enumerate() and then check the index,
- // instead of having a bool flag?
- let mut is_first_character = true;
- for c in scheme.chars() {
- let valid_characters: &HashSet<char> = if is_first_character {
- &ALPHA
- } else {
- &SCHEME_NOT_FIRST
- };
- if !valid_characters.contains(&c) {
- return Err(Error::IllegalCharacter(Context::Scheme));
- }
- is_first_character = false;
- }
+ scheme
+ .chars()
+ .enumerate()
+ .try_fold((), |_, (i, c)| {
+ let valid_characters: &HashSet<char> = if i == 0 {
+ &ALPHA
+ } else {
+ &SCHEME_NOT_FIRST
+ };
+ if valid_characters.contains(&c) {
+ Ok(())
+ } else {
+ Err(Error::IllegalCharacter(Context::Scheme))
+ }
+ })?;
Ok(scheme)
}