Avoid allocating vectors for the help text

This commit is contained in:
Benjamin Sago 2017-06-23 22:58:07 +01:00
parent 4018165e26
commit 81c5d8b7c6
3 changed files with 14 additions and 11 deletions

View File

@ -225,6 +225,11 @@ impl SortField {
/// argument. This will return `Err` if the option is there, but does not
/// correspond to a valid field.
fn deduce(matches: &getopts::Matches) -> Result<SortField, Misfire> {
const SORTS: &[&str] = &[ "name", "Name", "size", "extension",
"Extension", "modified", "accessed",
"created", "inode", "none" ];
if let Some(word) = matches.opt_str("sort") {
match &*word {
"name" | "filename" => Ok(SortField::Name(SortCase::Sensitive)),
@ -237,10 +242,7 @@ impl SortField {
"cr" | "created" => Ok(SortField::CreatedDate),
"none" => Ok(SortField::Unsorted),
"inode" => Ok(SortField::FileInode),
field => Err(Misfire::bad_argument("sort", field, &[
"name", "Name", "size", "extension", "Extension",
"modified", "accessed", "created", "inode", "none"]
))
field => Err(Misfire::bad_argument("sort", field, SORTS))
}
}
else {

View File

@ -9,7 +9,7 @@ use options::help::HelpString;
/// A list of legal choices for an argument-taking option
#[derive(PartialEq, Debug)]
pub struct Choices(Vec<&'static str>);
pub struct Choices(&'static [&'static str]);
impl fmt::Display for Choices {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@ -68,10 +68,10 @@ impl Misfire {
/// argument. This has to use one of the `getopts` failure
/// variants--its meant to take just an option name, rather than an
/// option *and* an argument, but it works just as well.
pub fn bad_argument(option: &str, otherwise: &str, legal: &[&'static str]) -> Misfire {
pub fn bad_argument(option: &str, otherwise: &str, legal: &'static [&'static str]) -> Misfire {
Misfire::BadArgument(getopts::Fail::UnrecognizedOption(format!(
"--{} {}",
option, otherwise)), Choices(legal.into()))
option, otherwise)), Choices(legal))
}
}

View File

@ -294,12 +294,12 @@ impl TimeTypes {
return Err(Misfire::Useless("accessed", true, "time"));
}
static TIMES: &[& str] = &["modified", "accessed", "created"];
match &*word {
"mod" | "modified" => Ok(TimeTypes { accessed: false, modified: true, created: false }),
"acc" | "accessed" => Ok(TimeTypes { accessed: true, modified: false, created: false }),
"cr" | "created" => Ok(TimeTypes { accessed: false, modified: false, created: true }),
otherwise => Err(Misfire::bad_argument("time", otherwise,
&["modified", "accessed", "created"])),
otherwise => Err(Misfire::bad_argument("time", otherwise, TIMES))
}
}
else if modified || created || accessed {
@ -342,13 +342,14 @@ impl TerminalColours {
/// Determine which terminal colour conditions to use.
fn deduce(matches: &getopts::Matches) -> Result<TerminalColours, Misfire> {
const COLOURS: &[&str] = &["always", "auto", "never"];
if let Some(word) = matches.opt_str("color").or_else(|| matches.opt_str("colour")) {
match &*word {
"always" => Ok(TerminalColours::Always),
"auto" | "automatic" => Ok(TerminalColours::Automatic),
"never" => Ok(TerminalColours::Never),
otherwise => Err(Misfire::bad_argument("color", otherwise,
&["always", "auto", "never"]))
otherwise => Err(Misfire::bad_argument("color", otherwise, COLOURS))
}
}
else {