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
use std::io::prelude::*;
use std::io;
use std::fs::File;
use bit_vec::BitVec;
pub fn usize_from_file(path: &str) -> Result<usize, &'static str> {
if let Ok(s) = string_from_file(path) {
if let Ok(i) = s.trim().parse() {
Ok(i)
} else {
Err("Unable to parse")
}
} else {
Err("Unable to open file")
}
}
fn string_from_file(path: &str) -> Result<String, io::Error> {
match File::open(&path) {
Ok(mut f) => {
let mut s = String::new();
match f.read_to_string(&mut s) {
Ok(_) => Ok(s),
Err(e) => Err(e),
}
}
Err(e) => Err(e),
}
}
pub fn bitmask_from_hex_file(path: &str) -> Result<BitVec, &'static str> {
if let Ok(s) = string_from_file(path) {
bitmask_from_hex(&s)
} else {
Err("Error reading file")
}
}
pub fn bytes_from_hex(hex: &str) -> Result<Vec<u8>, &'static str> {
let mut bytes = Vec::<u8>::new();
for c in hex.trim().chars() {
match c {
',' | ' ' | '_' => continue,
_ => {}
}
if let Ok(byte) = u8::from_str_radix(&c.to_string(), 16) {
bytes.push(byte);
} else {
return Err("Failure parsing hex string");
}
}
Ok(bytes)
}
pub fn bitmask_from_hex(hex: &str) -> Result<BitVec, &'static str> {
match bytes_from_hex(hex) {
Ok(bytes) => Ok(BitVec::from_bytes(&bytes)),
Err(e) => Err(e),
}
}