add tools::parse submodule, move hex_nibble to it

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2019-11-14 09:54:41 +01:00
parent 85a9752ea2
commit 6cf330c9d2
3 changed files with 16 additions and 9 deletions

View File

@ -9,6 +9,7 @@ use lazy_static::lazy_static;
use libc;
use proxmox_tools::fs::file_read_firstline;
use proxmox_tools::parse::hex_nibble;
/// POSIX sysconf call
pub fn sysconf(name: i32) -> i64 {
@ -325,15 +326,6 @@ pub fn read_proc_net_dev() -> Result<Vec<ProcFsNetDev>, Error> {
Ok(result)
}
fn hex_nibble(c: u8) -> Result<u8, Error> {
Ok(match c {
b'0'..=b'9' => c - b'0',
b'a'..=b'f' => c - b'a' + 0xa,
b'A'..=b'F' => c - b'A' + 0xa,
_ => bail!("not a hex digit: {}", c as char),
})
}
fn hexstr_to_ipv4addr<T: AsRef<[u8]>>(hex: T) -> Result<Ipv4Addr, Error> {
let hex = hex.as_ref();
if hex.len() != 8 {

View File

@ -8,6 +8,7 @@ pub mod common_regex;
pub mod fd;
pub mod fs;
pub mod io;
pub mod parse;
pub mod serde;
pub mod uuid;
pub mod vec;

View File

@ -0,0 +1,14 @@
//! Some parsing utilities.
use failure::{bail, Error};
/// Parse a hexadecimal digit into a byte.
#[inline]
pub fn hex_nibble(c: u8) -> Result<u8, Error> {
Ok(match c {
b'0'..=b'9' => c - b'0',
b'a'..=b'f' => c - b'a' + 0xa,
b'A'..=b'F' => c - b'A' + 0xa,
_ => bail!("not a hex digit: {}", c as char),
})
}