access list: improve info hash string parsing

This commit is contained in:
Joakim Frostegård 2021-10-15 02:41:12 +02:00
parent 6cfa220097
commit ea52317b49

View file

@ -36,23 +36,24 @@ pub struct AccessList(HashSet<[u8; 20]>);
impl AccessList { impl AccessList {
fn parse_line_to_info_hash(line: String) -> anyhow::Result<[u8; 20]> { fn parse_line_to_info_hash(line: String) -> anyhow::Result<[u8; 20]> {
let mut count = 0usize; if line.len() != 20 {
return Err(anyhow::anyhow!("Info hash is not 20 bytes long: {}", line));
}
let mut bytes = [0u8; 20]; let mut bytes = [0u8; 20];
for (byte, c) in bytes.iter_mut().zip(line.chars()) { for (byte, c) in bytes.iter_mut().zip(line.chars()) {
if c as u32 > 255 {
return Err(anyhow::anyhow!(
"Info hash character is not ASCII: {:#?}",
c
));
}
*byte = c as u8; *byte = c as u8;
count += 1;
} }
if count == 20 { Ok(bytes)
Ok(bytes)
} else {
Err(anyhow::anyhow!(
"Info hash length only {} bytes: {}",
count,
line
))
}
} }
pub fn update_from_path(&mut self, path: &PathBuf) -> anyhow::Result<()> { pub fn update_from_path(&mut self, path: &PathBuf) -> anyhow::Result<()> {