1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-01 13:37:48 +00:00

od: clean up parse_radix code and use site

+ Make parse_radix terser and clearer.
+ Make purpose of radix clearer at use site
  (note that the code currently completely misuses the --address-radix
  flag; this is inherited from the previous code)
+ Don't panic! inside parse_radix; instead return Result<> and let the
  caller handle errors (currently we panic, but probably we'll want to use
  some less alarming error routine); this will be more testable later as
  well.
This commit is contained in:
Keunwoo Lee 2015-01-25 22:02:48 -08:00
parent 2f0d8c89c9
commit b756a57345

View file

@ -46,13 +46,10 @@ pub fn uumain(args: Vec<String>) -> isize {
Err(f) => panic!("Invalid options\n{}", f)
};
let mut rad = Radix::Octal;
if matches.opt_present("A") {
rad = parse_radix(matches.opt_str("A"));
} else {
println!("{}", getopts::usage("od", &opts));
}
let input_offset_base = match parse_radix(matches.opt_str("A")) {
Ok(r) => r,
Err(f) => { panic!("Invalid -A/--address-radix\n{}", f) }
};
let mut fname;
match args.last() {
@ -60,12 +57,12 @@ pub fn uumain(args: Vec<String>) -> isize {
None => { panic!("Need fname for now") ; }
};
main(rad, fname.clone());
main(input_offset_base, fname.clone());
0
}
fn main(radix: Radix, fname: String) {
fn main(input_offset_base: Radix, fname: String) {
let mut f = match File::open(&Path::new(fname)) {
Ok(f) => f,
Err(e) => panic!("file error: {}", e)
@ -77,7 +74,7 @@ fn main(radix: Radix, fname: String) {
match f.read(bytes) {
Ok(n) => {
print!("{:07o}", addr);
match radix {
match input_offset_base {
Radix::Decimal => {},
Radix::Octal => {
for b in range(0, n / std::u16::BYTES) {
@ -99,30 +96,24 @@ fn main(radix: Radix, fname: String) {
};
}
fn parse_radix(radix_str: Option<String>) -> Radix {
let rad = match radix_str {
fn parse_radix(radix_str: Option<String>) -> Result<Radix, &'static str> {
match radix_str {
None => Ok(Radix::Octal),
Some(s) => {
let st = s.into_bytes();
if st.len() != 1 {
panic!("Radix must be one of [d, o, b, x]\n");
}
let radix: char = *(st.get(0)
.expect("byte string of length 1 lacks a 0th elem")) as char;
if radix == 'd' {
Radix::Decimal
} else if radix == 'x' {
Radix::Hexadecimal
} else if radix == 'o' {
Radix::Octal
} else if radix == 'b' {
Radix::Binary
Err("Radix must be one of [d, o, b, x]\n")
} else {
panic!("Radix must be one of [d, o, b, x]\n");
let radix: char = *(st.get(0)
.expect("byte string of length 1 lacks a 0th elem")) as char;
match radix {
'd' => Ok(Radix::Decimal),
'x' => Ok(Radix::Hexadecimal),
'o' => Ok(Radix::Octal),
'b' => Ok(Radix::Binary),
_ => Err("Radix must be one of [d, o, b, x]\n")
}
}
},
None => Radix::Octal
};
rad
}
}
}