diff options
| author | Richard Walters <rwalters@digitalstirling.com> | 2020-10-07 18:49:56 -0700 | 
|---|---|---|
| committer | Richard Walters <rwalters@digitalstirling.com> | 2020-10-07 18:49:56 -0700 | 
| commit | deebb1a2c4d974cb66237cc96b8e3bd5cbfe384f (patch) | |
| tree | 5d9c2dbc8484a706bf782e7e6ab6c34f36a9beab /src | |
| parent | 71209c43c6fd1be1b5a0e1128a100785749837de (diff) | |
(Rust) combine match with if/else in Uri::validate_ipv4_address
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib.rs | 44 | 
1 files changed, 24 insertions, 20 deletions
| @@ -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);              },          };      } | 
