Resolve more linter warnings

This commit is contained in:
2022-08-27 14:47:56 -04:00
parent 0951a6ab3e
commit bac75acd2c
14 changed files with 175 additions and 172 deletions

View File

@@ -45,19 +45,16 @@ impl Bytes {
}
pub fn from_hex(s: &str) -> Bytes {
if s.len() % 2 != 0 {
panic!("Input string has uneven number of characters");
}
assert!(
s.len() % 2 == 0,
"Input string has uneven number of characters"
);
let bytes_result: Result<Vec<u8>, std::num::ParseIntError> = (0..s.len())
.step_by(2)
.map(|i| u8::from_str_radix(&s[i..i + 2], 16))
.collect();
match bytes_result {
Ok(b) => Bytes(b),
Err(_) => panic!("Could not convert all digit pairs to hex."),
}
Bytes(bytes_result.expect("Invalid hex string"))
}
pub fn to_hex(&self) -> String {
@@ -84,12 +81,9 @@ impl Bytes {
let mut r = 0;
for &c in v.iter() {
match c {
32 => r += 2,
33..=64 => r += 1,
65..=90 => r += 3,
91..=96 => r += 1,
97..=122 => r += 3,
123..=127 => r += 1,
b'!'..=b'@' | b'['..=b'`' | b'{'..=b'~' => r += 1,
b' ' => r += 2,
b'A'..=b'Z' | b'a'..=b'z' => r += 3,
_ => (),
}
}
@@ -108,7 +102,9 @@ impl Bytes {
pub fn pad_pkcs7(&mut self, block_size: usize) {
let Bytes(v) = self;
let padding_value = (block_size - v.len() % block_size) as u8;
let padding_value = (block_size - v.len() % block_size)
.try_into()
.expect("Padding value has to be an u8");
for _ in 0..padding_value {
v.push(padding_value);
}
@@ -180,7 +176,6 @@ impl Bytes {
false
}
#[allow(dead_code)]
pub fn hemming(Bytes(a): &Bytes, Bytes(b): &Bytes) -> u32 {
let v: Vec<u32> = Iterator::zip(a.iter(), b.iter())
.map(|z| (*(z.0) ^ *(z.1)).count_ones())