(s: &str)
| 125 | } |
| 126 | |
| 127 | fn hex_decode(s: &str) -> Result<Vec<u8>, TokenError> { |
| 128 | let mut out = Vec::with_capacity(s.len() / 2); |
| 129 | for chunk in s.as_bytes().chunks(2) { |
| 130 | if chunk.len() != 2 { |
| 131 | return Err(TokenError::InvalidHex); |
| 132 | } |
| 133 | let hi = hex_digit(chunk[0]).ok_or(TokenError::InvalidHex)?; |
| 134 | let lo = hex_digit(chunk[1]).ok_or(TokenError::InvalidHex)?; |
| 135 | out.push((hi << 4) | lo); |
| 136 | } |
| 137 | Ok(out) |
| 138 | } |
| 139 | |
| 140 | fn hex_digit(b: u8) -> Option<u8> { |
| 141 | match b { |