refactor(lex): Track replacements as str

The lexer will soon return `RawOsStr` and it'll cost to turn that into
an `OsStr`.  However, it caches a `str`, so let's just use that.
This commit is contained in:
Ed Page 2022-04-12 14:10:11 -05:00
parent f66d8abebd
commit c58928b6bd
2 changed files with 5 additions and 5 deletions

View file

@ -3,7 +3,6 @@
// Std // Std
use std::collections::HashMap; use std::collections::HashMap;
use std::env; use std::env;
use std::ffi::OsStr;
use std::ffi::OsString; use std::ffi::OsString;
use std::fmt; use std::fmt;
use std::io; use std::io;
@ -101,7 +100,7 @@ pub struct App<'help> {
g_settings: AppFlags, g_settings: AppFlags,
args: MKeyMap<'help>, args: MKeyMap<'help>,
subcommands: Vec<App<'help>>, subcommands: Vec<App<'help>>,
replacers: HashMap<&'help OsStr, &'help [&'help str]>, replacers: HashMap<&'help str, &'help [&'help str]>,
groups: Vec<ArgGroup<'help>>, groups: Vec<ArgGroup<'help>>,
current_help_heading: Option<&'help str>, current_help_heading: Option<&'help str>,
current_disp_ord: Option<usize>, current_disp_ord: Option<usize>,
@ -1945,7 +1944,7 @@ impl<'help> App<'help> {
#[cfg(feature = "unstable-replace")] #[cfg(feature = "unstable-replace")]
#[must_use] #[must_use]
pub fn replace(mut self, name: &'help str, target: &'help [&'help str]) -> Self { pub fn replace(mut self, name: &'help str, target: &'help [&'help str]) -> Self {
self.replacers.insert(OsStr::new(name), target); self.replacers.insert(name, target);
self self
} }
@ -3933,7 +3932,7 @@ impl<'help> App<'help> {
self.max_w self.max_w
} }
pub(crate) fn get_replacement(&self, key: &OsStr) -> Option<&[&str]> { pub(crate) fn get_replacement(&self, key: &str) -> Option<&[&str]> {
self.replacers.get(key).copied() self.replacers.get(key).copied()
} }

View file

@ -92,7 +92,8 @@ impl<'help, 'cmd> Parser<'help, 'cmd> {
while let Some(arg_os) = raw_args.next(&mut args_cursor) { while let Some(arg_os) = raw_args.next(&mut args_cursor) {
// Recover the replaced items if any. // Recover the replaced items if any.
if let Some(replaced_items) = self.cmd.get_replacement(arg_os) { if let Some(replaced_items) = arg_os.to_str().and_then(|a| self.cmd.get_replacement(a))
{
debug!( debug!(
"Parser::get_matches_with: found replacer: {:?}, target: {:?}", "Parser::get_matches_with: found replacer: {:?}, target: {:?}",
arg_os, replaced_items arg_os, replaced_items