Improve type safety of write_list_flag function.

This commit is contained in:
Jeremy Wall 2018-03-17 09:15:13 -05:00
parent e6f2f12dc9
commit a7b214a9af

View File

@ -37,23 +37,25 @@ impl FlagConverter {
return Ok(());
}
fn write_list_flag(&self, pfx: &str, name: &str, v: &Val, w: &mut Write) -> Result<()> {
if let &Val::List(ref def) = v {
// first of all we need to make sure that each &Val is only a primitive type.
for v in def.iter() {
let vref = v.as_ref();
if vref.is_list() || vref.is_tuple() || vref.is_macro() {
eprintln!(
"Skipping non primitive val in list for flag {}{}",
pfx, name
);
} else {
try!(self.write_flag_name(pfx, name, w));
try!(self.write(pfx, vref, w));
}
fn write_list_flag(
&self,
pfx: &str,
name: &str,
def: &Vec<Rc<Val>>,
w: &mut Write,
) -> Result<()> {
// first of all we need to make sure that each &Val is only a primitive type.
for v in def.iter() {
let vref = v.as_ref();
if vref.is_list() || vref.is_tuple() || vref.is_macro() {
eprintln!(
"Skipping non primitive val in list for flag {}{}",
pfx, name
);
} else {
try!(self.write_flag_name(pfx, name, w));
try!(self.write(pfx, vref, w));
}
} else {
panic!("Impossible call happened. Somebody messed up.")
}
return Ok(());
}
@ -82,14 +84,18 @@ impl FlagConverter {
try!(self.write_flag_name(pfx, &name.val, w));
continue;
}
if val.is_tuple() {
let new_pfx = format!("{}{}.", pfx, name);
try!(self.write(&new_pfx, val, w));
} else if val.is_list() {
try!(self.write_list_flag(pfx, &name.val, &val, w));
} else {
try!(self.write_flag_name(pfx, &name.val, w));
try!(self.write(pfx, &val, w));
match val.as_ref() {
&Val::Tuple(_) => {
let new_pfx = format!("{}{}.", pfx, name);
try!(self.write(&new_pfx, val, w));
}
&Val::List(ref def) => {
try!(self.write_list_flag(pfx, &name.val, def, w));
}
_ => {
try!(self.write_flag_name(pfx, &name.val, w));
try!(self.write(pfx, &val, w));
}
}
},
&Val::Macro(ref _def) => {