Make building the matches more bearable

Now, building the two result vectors doesn’t have to go through at least one field.
This commit is contained in:
Benjamin Sago 2017-08-05 19:15:27 +01:00
parent 9872eba821
commit cdad6bb960

View File

@ -129,10 +129,8 @@ impl Args {
let mut parsing = true; let mut parsing = true;
// The results that get built up. // The results that get built up.
let mut results = Matches { let mut result_flags = Vec::new();
flags: MatchedFlags { flags: Vec::new() }, let mut frees: Vec<&OsStr> = Vec::new();
frees: Vec::new(),
};
// Iterate over the inputs with “while let” because we need to advance // Iterate over the inputs with “while let” because we need to advance
// the iterator manually whenever an argument that takes a value // the iterator manually whenever an argument that takes a value
@ -146,7 +144,7 @@ impl Args {
// the pair “-- --arg”, without it getting matched as a flag that // the pair “-- --arg”, without it getting matched as a flag that
// doesnt exist. // doesnt exist.
if !parsing { if !parsing {
results.frees.push(arg) frees.push(arg)
} }
else if arg == "--" { else if arg == "--" {
parsing = false; parsing = false;
@ -163,7 +161,7 @@ impl Args {
let arg = self.lookup_long(before)?; let arg = self.lookup_long(before)?;
let flag = Flag::Long(arg.long); let flag = Flag::Long(arg.long);
match arg.takes_value { match arg.takes_value {
Necessary => results.flags.flags.push((flag, Some(after))), Necessary => result_flags.push((flag, Some(after))),
Forbidden => return Err(ParseError::ForbiddenValue { flag }) Forbidden => return Err(ParseError::ForbiddenValue { flag })
} }
} }
@ -174,10 +172,10 @@ impl Args {
let arg = self.lookup_long(long_arg_name)?; let arg = self.lookup_long(long_arg_name)?;
let flag = Flag::Long(arg.long); let flag = Flag::Long(arg.long);
match arg.takes_value { match arg.takes_value {
Forbidden => results.flags.flags.push((flag, None)), Forbidden => result_flags.push((flag, None)),
Necessary => { Necessary => {
if let Some(next_arg) = inputs.next() { if let Some(next_arg) = inputs.next() {
results.flags.flags.push((flag, Some(next_arg))); result_flags.push((flag, Some(next_arg)));
} }
else { else {
return Err(ParseError::NeedsValue { flag }) return Err(ParseError::NeedsValue { flag })
@ -212,7 +210,7 @@ impl Args {
let arg = self.lookup_short(*byte)?; let arg = self.lookup_short(*byte)?;
let flag = Flag::Short(*byte); let flag = Flag::Short(*byte);
match arg.takes_value { match arg.takes_value {
Forbidden => results.flags.flags.push((flag, None)), Forbidden => result_flags.push((flag, None)),
Necessary => return Err(ParseError::NeedsValue { flag }) Necessary => return Err(ParseError::NeedsValue { flag })
} }
} }
@ -221,7 +219,7 @@ impl Args {
let arg = self.lookup_short(*arg_with_value)?; let arg = self.lookup_short(*arg_with_value)?;
let flag = Flag::Short(arg.short.unwrap()); let flag = Flag::Short(arg.short.unwrap());
match arg.takes_value { match arg.takes_value {
Necessary => results.flags.flags.push((flag, Some(after))), Necessary => result_flags.push((flag, Some(after))),
Forbidden => return Err(ParseError::ForbiddenValue { flag }) Forbidden => return Err(ParseError::ForbiddenValue { flag })
} }
} }
@ -243,15 +241,15 @@ impl Args {
let arg = self.lookup_short(*byte)?; let arg = self.lookup_short(*byte)?;
let flag = Flag::Short(*byte); let flag = Flag::Short(*byte);
match arg.takes_value { match arg.takes_value {
Forbidden => results.flags.flags.push((flag, None)), Forbidden => result_flags.push((flag, None)),
Necessary => { Necessary => {
if index < bytes.len() - 1 { if index < bytes.len() - 1 {
let remnants = &bytes[index+1 ..]; let remnants = &bytes[index+1 ..];
results.flags.flags.push((flag, Some(OsStr::from_bytes(remnants)))); result_flags.push((flag, Some(OsStr::from_bytes(remnants))));
break; break;
} }
else if let Some(next_arg) = inputs.next() { else if let Some(next_arg) = inputs.next() {
results.flags.flags.push((flag, Some(next_arg))); result_flags.push((flag, Some(next_arg)));
} }
else { else {
return Err(ParseError::NeedsValue { flag }) return Err(ParseError::NeedsValue { flag })
@ -264,11 +262,11 @@ impl Args {
// Otherwise, its a free string, usually a file name. // Otherwise, its a free string, usually a file name.
else { else {
results.frees.push(arg) frees.push(arg)
} }
} }
Ok(results) Ok(Matches { frees, flags: MatchedFlags { flags: result_flags } })
} }
fn lookup_short<'a>(&self, short: ShortArg) -> Result<&Arg, ParseError> { fn lookup_short<'a>(&self, short: ShortArg) -> Result<&Arg, ParseError> {