diff options
| author | diogo <[email protected]> | 2021-02-28 16:00:24 +0000 |
|---|---|---|
| committer | diogo <[email protected]> | 2021-02-28 16:00:24 +0000 |
| commit | dc3d0a467b9330380d75c851dcd775514fce53c2 (patch) | |
| tree | 67bc2ee4bd71b78c59164acaba60fdaa78cf8584 | |
| parent | 3acdd305518c543391f6fb28ec346343a2fa11bf (diff) | |
Fixed clippy warnings and formatting
| -rw-r--r-- | src/lib.rs | 29 |
1 files changed, 14 insertions, 15 deletions
| @@ -17,14 +17,14 @@ struct EncodeHexIterSlice<'data_lifetime> { | |||
| 17 | //bytes to encode as hex | 17 | //bytes to encode as hex |
| 18 | data: &'data_lifetime [u8], | 18 | data: &'data_lifetime [u8], |
| 19 | //each byte generates 2 hex characters but we only return one at a time | 19 | //each byte generates 2 hex characters but we only return one at a time |
| 20 | next_char : Option<char> | 20 | next_char: Option<char>, |
| 21 | } | 21 | } |
| 22 | 22 | ||
| 23 | impl<'data_lifetime> EncodeHexIterSlice<'data_lifetime> { | 23 | impl<'data_lifetime> EncodeHexIterSlice<'data_lifetime> { |
| 24 | fn new(data : &'data_lifetime [u8]) -> Self { | 24 | fn new(data: &'data_lifetime [u8]) -> Self { |
| 25 | EncodeHexIterSlice{ | 25 | EncodeHexIterSlice { |
| 26 | data, | 26 | data, |
| 27 | next_char : None | 27 | next_char: None, |
| 28 | } | 28 | } |
| 29 | } | 29 | } |
| 30 | } | 30 | } |
| @@ -36,7 +36,7 @@ impl<'data_lifetime> Iterator for EncodeHexIterSlice<'data_lifetime> { | |||
| 36 | if let Some(c) = self.next_char.take() { | 36 | if let Some(c) = self.next_char.take() { |
| 37 | return Some(c); | 37 | return Some(c); |
| 38 | } | 38 | } |
| 39 | if self.data.len() == 0 { | 39 | if self.data.is_empty() { |
| 40 | return None; | 40 | return None; |
| 41 | } | 41 | } |
| 42 | 42 | ||
| @@ -91,8 +91,8 @@ fn hex_char_byte_to_value(c: u8) -> Result<u8, HexDecodeError> { | |||
| 91 | fn byte_to_hex_char(b: u8) -> char { | 91 | fn byte_to_hex_char(b: u8) -> char { |
| 92 | assert!(b <= 15); | 92 | assert!(b <= 15); |
| 93 | match b { | 93 | match b { |
| 94 | digit if digit < 10 => ('0' as u8 + digit) as char, | 94 | digit if digit < 10 => (b'0' + digit) as char, |
| 95 | letter => ('a' as u8 + letter - 10) as char, | 95 | letter => (b'a' + letter - 10) as char, |
| 96 | } | 96 | } |
| 97 | } | 97 | } |
| 98 | 98 | ||
| @@ -112,9 +112,9 @@ fn append_byte_hex_to_string(byte: u8, string: &mut String) { | |||
| 112 | /// //do something | 112 | /// //do something |
| 113 | ///} | 113 | ///} |
| 114 | ///``` | 114 | ///``` |
| 115 | pub fn decode_hex_iter<'a, T>(string: &'a T) -> impl Iterator<Item = u8> + 'a | 115 | pub fn decode_hex_iter<T>(string: &T) -> impl Iterator<Item = u8> + '_ |
| 116 | where | 116 | where |
| 117 | T: AsRef<[u8]> + ?Sized | 117 | T: AsRef<[u8]> + ?Sized, |
| 118 | { | 118 | { |
| 119 | DecodeHexIterSlice::new(string.as_ref()) | 119 | DecodeHexIterSlice::new(string.as_ref()) |
| 120 | } | 120 | } |
| @@ -128,8 +128,9 @@ where | |||
| 128 | /// //do something with c | 128 | /// //do something with c |
| 129 | ///} | 129 | ///} |
| 130 | ///``` | 130 | ///``` |
| 131 | pub fn encode_hex_iter<'a, T>(data : &'a T) -> impl Iterator<Item = char> + 'a | 131 | pub fn encode_hex_iter<T>(data: &T) -> impl Iterator<Item = char> + '_ |
| 132 | where T: AsRef<[u8]> + ?Sized | 132 | where |
| 133 | T: AsRef<[u8]> + ?Sized, | ||
| 133 | { | 134 | { |
| 134 | EncodeHexIterSlice::new(data.as_ref()) | 135 | EncodeHexIterSlice::new(data.as_ref()) |
| 135 | } | 136 | } |
| @@ -146,9 +147,8 @@ pub fn decode_hex<T: AsRef<[u8]>>(string: T) -> Result<Vec<u8>, HexDecodeError> | |||
| 146 | return Err(HexDecodeError::InvalidByteCount); | 147 | return Err(HexDecodeError::InvalidByteCount); |
| 147 | } | 148 | } |
| 148 | let mut data = Vec::new(); | 149 | let mut data = Vec::new(); |
| 149 | let mut index = 0; | ||
| 150 | let mut first_digit: u8 = 0; | 150 | let mut first_digit: u8 = 0; |
| 151 | for c in chars.iter() { | 151 | for (index, c) in chars.iter().enumerate() { |
| 152 | if index % 2 != 0 { | 152 | if index % 2 != 0 { |
| 153 | let second_digit = hex_char_byte_to_value(*c)?; | 153 | let second_digit = hex_char_byte_to_value(*c)?; |
| 154 | let value = first_digit * 16 + second_digit; | 154 | let value = first_digit * 16 + second_digit; |
| @@ -156,7 +156,6 @@ pub fn decode_hex<T: AsRef<[u8]>>(string: T) -> Result<Vec<u8>, HexDecodeError> | |||
| 156 | } else { | 156 | } else { |
| 157 | first_digit = hex_char_byte_to_value(*c)?; | 157 | first_digit = hex_char_byte_to_value(*c)?; |
| 158 | } | 158 | } |
| 159 | index += 1; | ||
| 160 | } | 159 | } |
| 161 | Ok(data) | 160 | Ok(data) |
| 162 | } | 161 | } |
| @@ -261,7 +260,7 @@ mod tests { | |||
| 261 | #[test] | 260 | #[test] |
| 262 | fn iter_encode_test() { | 261 | fn iter_encode_test() { |
| 263 | let data = &[0xff, 0xaa, 0x12, 0x45]; | 262 | let data = &[0xff, 0xaa, 0x12, 0x45]; |
| 264 | let string : String = encode_hex_iter(&data).collect(); | 263 | let string: String = encode_hex_iter(&data).collect(); |
| 265 | assert_eq!(string, "ffaa1245"); | 264 | assert_eq!(string, "ffaa1245"); |
| 266 | } | 265 | } |
| 267 | } | 266 | } |
