aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Walters <rwalters@digitalstirling.com>2020-10-07 18:49:56 -0700
committerRichard Walters <rwalters@digitalstirling.com>2020-10-07 18:49:56 -0700
commitdeebb1a2c4d974cb66237cc96b8e3bd5cbfe384f (patch)
tree5d9c2dbc8484a706bf782e7e6ab6c34f36a9beab
parent71209c43c6fd1be1b5a0e1128a100785749837de (diff)
(Rust) combine match with if/else in Uri::validate_ipv4_address
-rw-r--r--src/lib.rs44
1 files changed, 24 insertions, 20 deletions
diff --git a/src/lib.rs b/src/lib.rs
index b908b3f..6a36c9f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -233,32 +233,36 @@ fn validate_ipv4_address(address: &str) -> Result<(), Error> {
// how to remove the redundant code.
for c in address.chars() {
state = match state {
+ State::NotInOctet if DIGIT.contains(&c) => {
+ octet_buffer.push(c);
+ State::ExpectDigitOrDot
+ },
+
State::NotInOctet => {
- if DIGIT.contains(&c) {
- octet_buffer.push(c);
- State::ExpectDigitOrDot
- } else {
+ return Err(Error::IllegalCharacter);
+ },
+
+ State::ExpectDigitOrDot if c == '.' => {
+ num_groups += 1;
+ // TODO: explore combining these two "if" statements or
+ // expressing them in a better way.
+ if num_groups > 4 {
+ return Err(Error::IllegalCharacter);
+ }
+ if octet_buffer.parse::<u8>().is_err() {
return Err(Error::IllegalCharacter);
}
+ octet_buffer.clear();
+ State::NotInOctet
+ },
+
+ State::ExpectDigitOrDot if DIGIT.contains(&c) => {
+ octet_buffer.push(c);
+ State::ExpectDigitOrDot
},
State::ExpectDigitOrDot => {
- if c == '.' {
- num_groups += 1;
- if num_groups > 4 {
- return Err(Error::IllegalCharacter);
- }
- if octet_buffer.parse::<u8>().is_err() {
- return Err(Error::IllegalCharacter);
- }
- octet_buffer.clear();
- State::NotInOctet
- } else if DIGIT.contains(&c) {
- octet_buffer.push(c);
- State::ExpectDigitOrDot
- } else {
- return Err(Error::IllegalCharacter);
- }
+ return Err(Error::IllegalCharacter);
},
};
}