formatting fixup

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2019-06-17 12:32:22 +02:00
parent c48e17fe26
commit a5af6eedf7
3 changed files with 40 additions and 17 deletions

View File

@ -46,7 +46,9 @@ impl CliMode {
pub fn quote(&self, name: &proc_macro2::Ident) -> TokenStream { pub fn quote(&self, name: &proc_macro2::Ident) -> TokenStream {
match self { match self {
CliMode::Disabled => quote! { None }, CliMode::Disabled => quote! { None },
CliMode::ParseCli => quote! { Some(<#name as ::proxmox::api::cli::ParseCli>::parse_cli) }, CliMode::ParseCli => {
quote! { Some(<#name as ::proxmox::api::cli::ParseCli>::parse_cli) }
}
CliMode::FromStr => quote! { CliMode::FromStr => quote! {
Some(<#name as ::proxmox::api::cli::ParseCliFromStr>::parse_cli) Some(<#name as ::proxmox::api::cli::ParseCliFromStr>::parse_cli)
}, },

View File

@ -304,7 +304,8 @@ impl<T> ParseCliFromStr for T
where where
T: FromStr + Serialize, T: FromStr + Serialize,
<T as FromStr>::Err: Into<Error>, <T as FromStr>::Err: Into<Error>,
{} {
}
#[macro_export] #[macro_export]
macro_rules! no_cli_type { macro_rules! no_cli_type {

View File

@ -10,7 +10,7 @@ pub mod vec;
macro_rules! offsetof { macro_rules! offsetof {
($ty:ty, $field:ident) => { ($ty:ty, $field:ident) => {
unsafe { &(*(0 as *const $ty)).$field as *const _ as usize } unsafe { &(*(0 as *const $ty)).$field as *const _ as usize }
} };
} }
const HEX_CHARS: &'static [u8; 16] = b"0123456789abcdef"; const HEX_CHARS: &'static [u8; 16] = b"0123456789abcdef";
@ -20,7 +20,7 @@ pub fn digest_to_hex(digest: &[u8]) -> String {
} }
pub fn bin_to_hex(digest: &[u8]) -> String { pub fn bin_to_hex(digest: &[u8]) -> String {
let mut buf = Vec::<u8>::with_capacity(digest.len()*2); let mut buf = Vec::<u8>::with_capacity(digest.len() * 2);
for i in 0..digest.len() { for i in 0..digest.len() {
buf.push(HEX_CHARS[(digest[i] >> 4) as usize]); buf.push(HEX_CHARS[(digest[i] >> 4) as usize]);
@ -35,19 +35,27 @@ pub fn hex_to_bin(hex: &str) -> Result<Vec<u8>, Error> {
let bytes = hex.as_bytes(); let bytes = hex.as_bytes();
if (bytes.len() % 2) != 0 { bail!("hex_to_bin: got wrong input length."); } if (bytes.len() % 2) != 0 {
bail!("hex_to_bin: got wrong input length.");
}
let val = |c| { let val = |c| {
if c >= b'0' && c <= b'9' { return Ok(c - b'0'); } if c >= b'0' && c <= b'9' {
if c >= b'a' && c <= b'f' { return Ok(c - b'a' + 10); } return Ok(c - b'0');
if c >= b'A' && c <= b'F' { return Ok(c - b'A' + 10); } }
if c >= b'a' && c <= b'f' {
return Ok(c - b'a' + 10);
}
if c >= b'A' && c <= b'F' {
return Ok(c - b'A' + 10);
}
bail!("found illegal hex character."); bail!("found illegal hex character.");
}; };
for pair in bytes.chunks(2) { for pair in bytes.chunks(2) {
let h = val(pair[0])?; let h = val(pair[0])?;
let l = val(pair[1])?; let l = val(pair[1])?;
result.push((h<<4)|l); result.push((h << 4) | l);
} }
Ok(result) Ok(result)
@ -58,25 +66,37 @@ pub fn hex_to_digest(hex: &str) -> Result<[u8; 32], Error> {
let bytes = hex.as_bytes(); let bytes = hex.as_bytes();
if bytes.len() != 64 { bail!("got wrong digest length."); } if bytes.len() != 64 {
bail!("got wrong digest length.");
}
let val = |c| { let val = |c| {
if c >= b'0' && c <= b'9' { return Ok(c - b'0'); } if c >= b'0' && c <= b'9' {
if c >= b'a' && c <= b'f' { return Ok(c - b'a' + 10); } return Ok(c - b'0');
if c >= b'A' && c <= b'F' { return Ok(c - b'A' + 10); } }
if c >= b'a' && c <= b'f' {
return Ok(c - b'a' + 10);
}
if c >= b'A' && c <= b'F' {
return Ok(c - b'A' + 10);
}
bail!("found illegal hex character."); bail!("found illegal hex character.");
}; };
let mut pos = 0; let mut pos = 0;
for pair in bytes.chunks(2) { for pair in bytes.chunks(2) {
if pos >= digest.len() { bail!("hex digest too long."); } if pos >= digest.len() {
bail!("hex digest too long.");
}
let h = val(pair[0])?; let h = val(pair[0])?;
let l = val(pair[1])?; let l = val(pair[1])?;
digest[pos] = (h<<4)|l; digest[pos] = (h << 4) | l;
pos +=1; pos += 1;
} }
if pos != digest.len() { bail!("hex digest too short."); } if pos != digest.len() {
bail!("hex digest too short.");
}
Ok(digest) Ok(digest)
} }