aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorbnewbold <bnewbold@robocracy.org>2016-06-12 20:22:16 -0400
committerbnewbold <bnewbold@robocracy.org>2016-06-12 20:22:16 -0400
commit5cc79f64ab0c310a76c5930ff6d51b4547a97ef4 (patch)
tree4e28671465e183c5c4a0a262cff9e311c7430803 /src
parent8ce97a84ea5adb16f4cf5e01bd931438a22caaf7 (diff)
downloadexuberant-hacks-5cc79f64ab0c310a76c5930ff6d51b4547a97ef4.tar.gz
exuberant-hacks-5cc79f64ab0c310a76c5930ff6d51b4547a97ef4.zip
util: refactor arg conv to String; add dechex2u64
Diffstat (limited to 'src')
-rw-r--r--src/util.rs44
1 files 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<String> {
+pub fn convert_xscreensaver_args(raw: Vec<String>) -> Vec<String> {
let known_args = vec!["-root",
"-window",
@@ -57,12 +58,10 @@ pub fn convert_xscreensaver_args(raw: Vec<&str>) -> Vec<String> {
"-record-animation"];
let ret: Vec<String> = 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<String> {
#[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<u64, ParseIntError> {
+
+ if (raw).starts_with("0x") {
+ u64::from_str_radix(raw.trim_left_matches('0').trim_left_matches('x'), 16)
+ } else {
+ raw.parse::<u64>()
+ }
+}
+
+#[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());
}