From 6cf330c9d2ae632b931a673106e0360759bee3d0 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Thu, 14 Nov 2019 09:54:41 +0100 Subject: [PATCH] add tools::parse submodule, move hex_nibble to it Signed-off-by: Wolfgang Bumiller --- proxmox-sys/src/linux/procfs.rs | 10 +--------- proxmox-tools/src/lib.rs | 1 + proxmox-tools/src/parse.rs | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 9 deletions(-) create mode 100644 proxmox-tools/src/parse.rs diff --git a/proxmox-sys/src/linux/procfs.rs b/proxmox-sys/src/linux/procfs.rs index a74489eb..46f3e889 100644 --- a/proxmox-sys/src/linux/procfs.rs +++ b/proxmox-sys/src/linux/procfs.rs @@ -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, Error> { Ok(result) } -fn hex_nibble(c: u8) -> Result { - 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>(hex: T) -> Result { let hex = hex.as_ref(); if hex.len() != 8 { diff --git a/proxmox-tools/src/lib.rs b/proxmox-tools/src/lib.rs index 2c475d19..b09daed3 100644 --- a/proxmox-tools/src/lib.rs +++ b/proxmox-tools/src/lib.rs @@ -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; diff --git a/proxmox-tools/src/parse.rs b/proxmox-tools/src/parse.rs new file mode 100644 index 00000000..f20c7c9b --- /dev/null +++ b/proxmox-tools/src/parse.rs @@ -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 { + 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), + }) +}