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
#![warn(clippy::pedantic)] use super::{ character_classes::DIGIT, context::Context, error::Error, }; struct Shared { num_groups: usize, octet_buffer: String, } enum State { NotInOctet(Shared), ExpectDigitOrDot(Shared), } impl State { fn finalize(self) -> Result<(), Error> { match self { Self::NotInOctet(_) => Err(Error::TruncatedHost), Self::ExpectDigitOrDot(state) => { Self::finalize_expect_digit_or_dot(state) }, } } fn finalize_expect_digit_or_dot(state: Shared) -> Result<(), Error> { let mut state = state; if !state.octet_buffer.is_empty() { state.num_groups += 1; if state.octet_buffer.parse::<u8>().is_err() { return Err(Error::InvalidDecimalOctet); } } match state.num_groups { 4 => Ok(()), n if n < 4 => Err(Error::TooFewAddressParts), _ => Err(Error::TooManyAddressParts), } } fn new() -> Self { Self::NotInOctet(Shared { num_groups: 0, octet_buffer: String::new(), }) } fn next( self, c: char, ) -> Result<Self, Error> { match self { Self::NotInOctet(state) => Self::next_not_in_octet(state, c), Self::ExpectDigitOrDot(state) => { Self::next_expect_digit_or_dot(state, c) }, } } fn next_not_in_octet( state: Shared, c: char, ) -> Result<Self, Error> { let mut state = state; if DIGIT.contains(&c) { state.octet_buffer.push(c); Ok(Self::ExpectDigitOrDot(state)) } else { Err(Error::IllegalCharacter(Context::Ipv4Address)) } } fn next_expect_digit_or_dot( state: Shared, c: char, ) -> Result<Self, Error> { let mut state = state; if c == '.' { state.num_groups += 1; if state.num_groups > 4 { return Err(Error::TooManyAddressParts); } if state.octet_buffer.parse::<u8>().is_err() { return Err(Error::InvalidDecimalOctet); } state.octet_buffer.clear(); Ok(Self::NotInOctet(state)) } else if DIGIT.contains(&c) { state.octet_buffer.push(c); Ok(Self::ExpectDigitOrDot(state)) } else { Err(Error::IllegalCharacter(Context::Ipv4Address)) } } } pub fn validate_ipv4_address<T>(address: T) -> Result<(), Error> where T: AsRef<str>, { address.as_ref().chars().try_fold(State::new(), State::next)?.finalize() } #[cfg(test)] mod tests { use super::*; #[test] fn good() { let test_vectors = [ "0.0.0.0", "1.2.3.0", "1.2.3.4", "1.2.3.255", "1.2.255.4", "1.255.3.4", "255.2.3.4", "255.255.255.255", ]; for test_vector in &test_vectors { assert!(validate_ipv4_address(*test_vector).is_ok()); } } #[test] fn bad() { named_tuple!( struct TestVector { address_string: &'static str, expected_error: Error, } ); let test_vectors: &[TestVector] = &[ ("1.2.x.4", Error::IllegalCharacter(Context::Ipv4Address)).into(), ("1.2.3.4.8", Error::TooManyAddressParts).into(), ("1.2.3", Error::TooFewAddressParts).into(), ("1.2.3.", Error::TruncatedHost).into(), ("1.2.3.256", Error::InvalidDecimalOctet).into(), ("1.2.3.-4", Error::IllegalCharacter(Context::Ipv4Address)).into(), ("1.2.3. 4", Error::IllegalCharacter(Context::Ipv4Address)).into(), ("1.2.3.4 ", Error::IllegalCharacter(Context::Ipv4Address)).into(), ]; for test_vector in test_vectors { let result = validate_ipv4_address(test_vector.address_string()); assert!(result.is_err(), "{}", test_vector.address_string()); assert_eq!( *test_vector.expected_error(), result.unwrap_err(), "{}", test_vector.address_string() ); } } }