split some common parsers in argument parsing to it's own file
This commit is contained in:
parent
b5906e1b06
commit
332fc79a18
3 changed files with 57 additions and 30 deletions
41
src/bin/common/argument_parsing.rs
Normal file
41
src/bin/common/argument_parsing.rs
Normal file
|
@ -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<Item = String>, opts: &Arguments, current_arg: String) -> Result<u8, String>{
|
||||||
|
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<Item = String>, opts: &Arguments, current_arg: String) -> Result<u8, String>{
|
||||||
|
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);
|
||||||
|
}
|
1
src/bin/common/mod.rs
Normal file
1
src/bin/common/mod.rs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
pub mod argument_parsing;
|
|
@ -3,6 +3,9 @@ use std::process::exit;
|
||||||
|
|
||||||
use binary_braillie::to_binary_braillie;
|
use binary_braillie::to_binary_braillie;
|
||||||
|
|
||||||
|
mod common;
|
||||||
|
use common::argument_parsing;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args = parse_args();
|
let args = parse_args();
|
||||||
if args.value_to_convert.len() <= 0 {
|
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()))
|
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 args = env::args().peekable();
|
||||||
let mut rvalue = Arguments {
|
let mut rvalue = argument_parsing::Arguments {
|
||||||
hexadecimal: false,
|
hexadecimal: false,
|
||||||
mode: 0,
|
mode: 0,
|
||||||
program_name: args.next().unwrap_or(String::from("t2br")),
|
program_name: args.next().unwrap_or(String::from("t2br")),
|
||||||
|
@ -57,31 +54,19 @@ fn parse_args() -> Arguments {
|
||||||
rvalue.hexadecimal = true;
|
rvalue.hexadecimal = true;
|
||||||
}
|
}
|
||||||
else if arg == "--direction" {
|
else if arg == "--direction" {
|
||||||
let direction = args.next().unwrap_or_else(|| {
|
match argument_parsing::parse_direction(&mut args, &rvalue, arg) {
|
||||||
println!("{}: argument for {} was not provided", program_name, arg);
|
Ok(e) => rvalue.mode = e,
|
||||||
|
Err(e) => {
|
||||||
|
print!("{}: {}", program_name, e);
|
||||||
exit(64);
|
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if arg == "--wrap" {
|
else if arg == "--wrap" {
|
||||||
let wrap = args.next().unwrap_or_else(|| {
|
match argument_parsing::parse_wrap(&mut args, &rvalue, arg) {
|
||||||
println!("{}: argument for {} was not provided", program_name, arg);
|
Ok(e) => rvalue.mode = e,
|
||||||
exit(64);
|
Err(e) => {
|
||||||
});
|
print!("{}: {}", program_name, e);
|
||||||
match wrap.as_str() {
|
|
||||||
"normal" => rvalue.mode &= !(1 << 0),
|
|
||||||
"reverse" => rvalue.mode |= 1 << 0,
|
|
||||||
_ => {
|
|
||||||
println!("{}: invalid wrap \"{}\" provided", program_name, wrap);
|
|
||||||
exit(65);
|
exit(65);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue