Make builtin status long options const

By using an explicit match instead of unwrap(), we can avoid the use of Lazy.
This commit is contained in:
ridiculousfish 2023-07-01 16:05:10 -07:00
parent 1c5c1993dd
commit 12dfbc14d7

View file

@ -13,7 +13,10 @@ use crate::ffi::{
use crate::future_feature_flags::{feature_metadata, feature_test}; use crate::future_feature_flags::{feature_metadata, feature_test};
use crate::wchar::{wstr, L}; use crate::wchar::{wstr, L};
use crate::wchar_ffi::{AsWstr, WCharFromFFI}; use crate::wchar_ffi::{AsWstr, WCharFromFFI};
use crate::wgetopt::{wgetopter_t, wopt, woption, woption_argument_t}; use crate::wgetopt::{
wgetopter_t, wopt, woption,
woption_argument_t::{no_argument, required_argument},
};
use crate::wutil::{ use crate::wutil::{
fish_wcstoi, sprintf, waccess, wbasename, wdirname, wgettext, wgettext_fmt, wrealpath, Error, fish_wcstoi, sprintf, waccess, wbasename, wdirname, wgettext, wgettext_fmt, wrealpath, Error,
}; };
@ -22,7 +25,6 @@ use nix::errno::Errno;
use nix::NixPath; use nix::NixPath;
use num_derive::FromPrimitive; use num_derive::FromPrimitive;
use num_traits::FromPrimitive; use num_traits::FromPrimitive;
use once_cell::sync::Lazy;
macro_rules! str_enum { macro_rules! str_enum {
($name:ident, $(($val:ident, $str:expr)),* $(,)?) => { ($name:ident, $(($val:ident, $str:expr)),* $(,)?) => {
@ -30,7 +32,7 @@ macro_rules! str_enum {
type Error = (); type Error = ();
fn try_from(s: &str) -> Result<Self, Self::Error> { fn try_from(s: &str) -> Result<Self, Self::Error> {
// matching on str's let's us avoid having to do binary search and friends outselves, // matching on str's lets us avoid having to do binary search and friends ourselves,
// this is ascii only anyways // this is ascii only anyways
match s { match s {
$($str => Ok(Self::$val)),*, $($str => Ok(Self::$val)),*,
@ -107,9 +109,11 @@ str_enum!(
); );
impl StatusCmd { impl StatusCmd {
fn as_char(self) -> char { const fn as_char(self) -> char {
// TODO: once unwrap is const, make LONG_OPTIONS const match char::from_u32(self as u32) {
char::from_u32(self as u32).unwrap() Some(c) => c,
None => panic!("Should be a valid char"),
}
} }
} }
@ -140,40 +144,37 @@ impl Default for StatusCmdOpts {
} }
const SHORT_OPTIONS: &wstr = L!(":L:cbilfnhj:t"); const SHORT_OPTIONS: &wstr = L!(":L:cbilfnhj:t");
static LONG_OPTIONS: Lazy<[woption; 17]> = Lazy::new(|| { const LONG_OPTIONS: &[woption] = &[
use woption_argument_t::*; wopt(L!("help"), no_argument, 'h'),
[ wopt(L!("current-filename"), no_argument, 'f'),
wopt(L!("help"), no_argument, 'h'), wopt(L!("current-line-number"), no_argument, 'n'),
wopt(L!("current-filename"), no_argument, 'f'), wopt(L!("filename"), no_argument, 'f'),
wopt(L!("current-line-number"), no_argument, 'n'), wopt(L!("fish-path"), no_argument, STATUS_FISH_PATH.as_char()),
wopt(L!("filename"), no_argument, 'f'), wopt(L!("is-block"), no_argument, 'b'),
wopt(L!("fish-path"), no_argument, STATUS_FISH_PATH.as_char()), wopt(L!("is-command-substitution"), no_argument, 'c'),
wopt(L!("is-block"), no_argument, 'b'), wopt(
wopt(L!("is-command-substitution"), no_argument, 'c'), L!("is-full-job-control"),
wopt( no_argument,
L!("is-full-job-control"), STATUS_IS_FULL_JOB_CTRL.as_char(),
no_argument, ),
STATUS_IS_FULL_JOB_CTRL.as_char(), wopt(L!("is-interactive"), no_argument, 'i'),
), wopt(
wopt(L!("is-interactive"), no_argument, 'i'), L!("is-interactive-job-control"),
wopt( no_argument,
L!("is-interactive-job-control"), STATUS_IS_INTERACTIVE_JOB_CTRL.as_char(),
no_argument, ),
STATUS_IS_INTERACTIVE_JOB_CTRL.as_char(), wopt(L!("is-login"), no_argument, 'l'),
), wopt(
wopt(L!("is-login"), no_argument, 'l'), L!("is-no-job-control"),
wopt( no_argument,
L!("is-no-job-control"), STATUS_IS_NO_JOB_CTRL.as_char(),
no_argument, ),
STATUS_IS_NO_JOB_CTRL.as_char(), wopt(L!("job-control"), required_argument, 'j'),
), wopt(L!("level"), required_argument, 'L'),
wopt(L!("job-control"), required_argument, 'j'), wopt(L!("line"), no_argument, 'n'),
wopt(L!("level"), required_argument, 'L'), wopt(L!("line-number"), no_argument, 'n'),
wopt(L!("line"), no_argument, 'n'), wopt(L!("print-stack-trace"), no_argument, 't'),
wopt(L!("line-number"), no_argument, 'n'), ];
wopt(L!("print-stack-trace"), no_argument, 't'),
]
});
/// Print the features and their values. /// Print the features and their values.
fn print_features(streams: &mut io_streams_t) { fn print_features(streams: &mut io_streams_t) {
@ -211,7 +212,7 @@ fn parse_cmd_opts(
let mut args_read = Vec::with_capacity(args.len()); let mut args_read = Vec::with_capacity(args.len());
args_read.extend_from_slice(args); args_read.extend_from_slice(args);
let mut w = wgetopter_t::new(SHORT_OPTIONS, &*LONG_OPTIONS, args); let mut w = wgetopter_t::new(SHORT_OPTIONS, LONG_OPTIONS, args);
while let Some(c) = w.wgetopt_long() { while let Some(c) = w.wgetopt_long() {
match c { match c {
'L' => { 'L' => {