From 5cc79f64ab0c310a76c5930ff6d51b4547a97ef4 Mon Sep 17 00:00:00 2001 From: bnewbold Date: Sun, 12 Jun 2016 20:22:16 -0400 Subject: util: refactor arg conv to String; add dechex2u64 --- src/util.rs | 44 +++++++++++++++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 11 deletions(-) diff --git a/src/util.rs b/src/util.rs index 4d87bb5..e3f237a 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,5 +1,6 @@ use std::str::FromStr; +use std::num::ParseIntError; pub fn view_matrix(position: &[f32; 3], direction: &[f32; 3], @@ -42,7 +43,7 @@ pub fn view_matrix(position: &[f32; 3], /// /// These variant long-form args should probably be hidden from the user, eg /// not show up in usage or manpage output. -pub fn convert_xscreensaver_args(raw: Vec<&str>) -> Vec { +pub fn convert_xscreensaver_args(raw: Vec) -> Vec { let known_args = vec!["-root", "-window", @@ -57,12 +58,10 @@ pub fn convert_xscreensaver_args(raw: Vec<&str>) -> Vec { "-record-animation"]; let ret: Vec = raw.into_iter().map(|arg| { - if known_args.contains(&arg) { - let mut longer = String::from_str("-").unwrap(); - longer.push_str(arg); - longer + if known_args.contains(&(arg.as_str())) { + String::from_str("-").unwrap() + &arg } else { - String::from_str(arg).unwrap() + arg } }).collect(); ret @@ -70,9 +69,32 @@ pub fn convert_xscreensaver_args(raw: Vec<&str>) -> Vec { #[test] fn test_xargconv() { - assert_eq!(vec!["--root"], convert_xscreensaver_args(vec!["-root"])); - assert_eq!(vec!["--root"], convert_xscreensaver_args(vec!["--root"])); - assert_eq!(vec!["root"], convert_xscreensaver_args(vec!["root"])); - assert_eq!(vec!["-asdf"], convert_xscreensaver_args(vec!["-asdf"])); - assert_eq!(vec!["-h"], convert_xscreensaver_args(vec!["-h"])); + assert_eq!(vec!["--root"], convert_xscreensaver_args(vec!["-root".to_string()])); + assert_eq!(vec!["--root"], convert_xscreensaver_args(vec!["--root".to_string()])); + assert_eq!(vec!["root"], convert_xscreensaver_args(vec!["root".to_string()])); + assert_eq!(vec!["-asdf"], convert_xscreensaver_args(vec!["-asdf".to_string()])); + assert_eq!(vec!["-h"], convert_xscreensaver_args(vec!["-h".to_string()])); +} + +/// Converts a string which is decimal ("1234") or hex-with-prefix ("0x12BC") to u64. +pub fn dechex2u64(raw: &str) -> Result { + + if (raw).starts_with("0x") { + u64::from_str_radix(raw.trim_left_matches('0').trim_left_matches('x'), 16) + } else { + raw.parse::() + } +} + +#[test] +fn test_dechex2u64() { + assert_eq!(Ok(123), dechex2u64("123")); + assert_eq!(Ok(0), dechex2u64("000")); + assert_eq!(Ok(65535), dechex2u64("0xFFFF")); + assert_eq!(Ok(291), dechex2u64("0x123")); + assert!(dechex2u64("0x").is_err()); + assert!(dechex2u64("").is_err()); + assert!(dechex2u64("FF123").is_err()); + assert!(dechex2u64("0FF123").is_err()); + assert!(dechex2u64("asdf").is_err()); } -- cgit v1.2.3