diff --git a/src/bin/common/argument_parsing.rs b/src/bin/common/argument_parsing.rs new file mode 100644 index 0000000..1e13095 --- /dev/null +++ b/src/bin/common/argument_parsing.rs @@ -0,0 +1,41 @@ +pub struct Arguments { + pub hexadecimal: bool, + pub mode: u8, + pub program_name: String, + pub reverse_characters: bool, + pub value_to_convert: String, +} + +pub fn parse_direction(args: &mut impl Iterator, opts: &Arguments, current_arg: String) -> Result{ + let mut rvalue = opts.mode; + let direction = match args.next() { + None => return Err(format!("argument for {} was not provided", current_arg)), + Some(e) => e, + }; + match direction.as_str() { + "row" => { rvalue &= !(1 << 1) ; rvalue &= !(1 << 2); } + "row-reverse" => { rvalue |= 1 << 1 ; rvalue &= !(1 << 2); } + "column" => { rvalue &= !(1 << 1) ; rvalue |= 1 << 2 ; } + "column-reverse" => { rvalue |= 1 << 1 ; rvalue |= 1 << 2 ; } + _ => return Err( + format!("invalid direction \"{}\" provided for {}", direction, current_arg) + ) + }; + return Ok(rvalue); +} + +pub fn parse_wrap(args: &mut impl Iterator, opts: &Arguments, current_arg: String) -> Result{ + let mut rvalue = opts.mode; + let wrap = match args.next() { + None => return Err(format!("argument for {} was not provided", current_arg)), + Some(e) => e, + }; + match wrap.as_str() { + "normal" => rvalue &= !(1 << 0), + "reverse" => rvalue |= 1 << 0, + _ => return Err( + format!("invalid wrap \"{}\" provided for {}", wrap, current_arg) + ) + }; + return Ok(rvalue); +} diff --git a/src/bin/common/mod.rs b/src/bin/common/mod.rs new file mode 100644 index 0000000..55989fb --- /dev/null +++ b/src/bin/common/mod.rs @@ -0,0 +1 @@ +pub mod argument_parsing; diff --git a/src/bin/convert_to_braillie.rs b/src/bin/convert_to_braillie.rs index 4922e17..170677b 100644 --- a/src/bin/convert_to_braillie.rs +++ b/src/bin/convert_to_braillie.rs @@ -3,6 +3,9 @@ use std::process::exit; use binary_braillie::to_binary_braillie; +mod common; +use common::argument_parsing; + fn main() { let args = parse_args(); if args.value_to_convert.len() <= 0 { @@ -24,17 +27,11 @@ fn main() { print!("{}", to_binary_braillie(&value, value.len(), args.mode.try_into().unwrap())) } -struct Arguments { - pub hexadecimal: bool, - pub mode: u8, - pub program_name: String, - pub reverse_characters: bool, - pub value_to_convert: String, -} -fn parse_args() -> Arguments { + +fn parse_args() -> argument_parsing::Arguments { let mut args = env::args().peekable(); - let mut rvalue = Arguments { + let mut rvalue = argument_parsing::Arguments { hexadecimal: false, mode: 0, program_name: args.next().unwrap_or(String::from("t2br")), @@ -57,31 +54,19 @@ fn parse_args() -> Arguments { rvalue.hexadecimal = true; } else if arg == "--direction" { - let direction = args.next().unwrap_or_else(|| { - println!("{}: argument for {} was not provided", program_name, arg); - exit(64); - }); - match direction.as_str() { - "row" => { rvalue.mode &= !(1 << 1) ; rvalue.mode &= !(1 << 2); } - "row-reverse" => { rvalue.mode |= 1 << 1 ; rvalue.mode &= !(1 << 2); } - "column" => { rvalue.mode &= !(1 << 1) ; rvalue.mode |= 1 << 2 ; } - "column-reverse" => { rvalue.mode |= 1 << 1 ; rvalue.mode |= 1 << 2 ; } - _ => { - println!("{}: invalid direction \"{}\" provided", program_name, direction); - exit(65); + match argument_parsing::parse_direction(&mut args, &rvalue, arg) { + Ok(e) => rvalue.mode = e, + Err(e) => { + print!("{}: {}", program_name, e); + exit(64); } } } else if arg == "--wrap" { - let wrap = args.next().unwrap_or_else(|| { - println!("{}: argument for {} was not provided", program_name, arg); - exit(64); - }); - match wrap.as_str() { - "normal" => rvalue.mode &= !(1 << 0), - "reverse" => rvalue.mode |= 1 << 0, - _ => { - println!("{}: invalid wrap \"{}\" provided", program_name, wrap); + match argument_parsing::parse_wrap(&mut args, &rvalue, arg) { + Ok(e) => rvalue.mode = e, + Err(e) => { + print!("{}: {}", program_name, e); exit(65); } }