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

View File

@ -9,7 +9,7 @@ use options::help::HelpString;
/// A list of legal choices for an argument-taking option /// A list of legal choices for an argument-taking option
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug)]
pub struct Choices(Vec<&'static str>); pub struct Choices(&'static [&'static str]);
impl fmt::Display for Choices { impl fmt::Display for Choices {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 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 /// argument. This has to use one of the `getopts` failure
/// variants--its meant to take just an option name, rather than an /// variants--its meant to take just an option name, rather than an
/// option *and* an argument, but it works just as well. /// 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!( 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")); return Err(Misfire::Useless("accessed", true, "time"));
} }
static TIMES: &[& str] = &["modified", "accessed", "created"];
match &*word { match &*word {
"mod" | "modified" => Ok(TimeTypes { accessed: false, modified: true, created: false }), "mod" | "modified" => Ok(TimeTypes { accessed: false, modified: true, created: false }),
"acc" | "accessed" => Ok(TimeTypes { accessed: true, modified: false, created: false }), "acc" | "accessed" => Ok(TimeTypes { accessed: true, modified: false, created: false }),
"cr" | "created" => Ok(TimeTypes { accessed: false, modified: false, created: true }), "cr" | "created" => Ok(TimeTypes { accessed: false, modified: false, created: true }),
otherwise => Err(Misfire::bad_argument("time", otherwise, otherwise => Err(Misfire::bad_argument("time", otherwise, TIMES))
&["modified", "accessed", "created"])),
} }
} }
else if modified || created || accessed { else if modified || created || accessed {
@ -342,13 +342,14 @@ impl TerminalColours {
/// Determine which terminal colour conditions to use. /// Determine which terminal colour conditions to use.
fn deduce(matches: &getopts::Matches) -> Result<TerminalColours, Misfire> { 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")) { if let Some(word) = matches.opt_str("color").or_else(|| matches.opt_str("colour")) {
match &*word { match &*word {
"always" => Ok(TerminalColours::Always), "always" => Ok(TerminalColours::Always),
"auto" | "automatic" => Ok(TerminalColours::Automatic), "auto" | "automatic" => Ok(TerminalColours::Automatic),
"never" => Ok(TerminalColours::Never), "never" => Ok(TerminalColours::Never),
otherwise => Err(Misfire::bad_argument("color", otherwise, otherwise => Err(Misfire::bad_argument("color", otherwise, COLOURS))
&["always", "auto", "never"]))
} }
} }
else { else {