move single component parsing to dedicated function

this will prevent parsing when expecting more than a single component
to be parsed, and prepare for the symetric variant-to-name function to
be added
This commit is contained in:
Rémy Rakic 2023-09-20 20:09:06 +00:00
parent acc3b61c5e
commit 2ce46f8e8c
3 changed files with 12 additions and 13 deletions

View File

@ -240,21 +240,21 @@ pub struct LinkSelfContained {
impl LinkSelfContained { impl LinkSelfContained {
/// Incorporates an enabled or disabled component as specified on the CLI, if possible. /// Incorporates an enabled or disabled component as specified on the CLI, if possible.
/// For example: `+linker`, and `-crto`. /// For example: `+linker`, and `-crto`.
pub(crate) fn handle_cli_component(&mut self, component: &str) -> Result<(), ()> { pub(crate) fn handle_cli_component(&mut self, component: &str) -> Option<()> {
// Note that for example `-Cself-contained=y -Cself-contained=-linker` is not an explicit // Note that for example `-Cself-contained=y -Cself-contained=-linker` is not an explicit
// set of all values like `y` or `n` used to be. Therefore, if this flag had previously been // set of all values like `y` or `n` used to be. Therefore, if this flag had previously been
// set in bulk with its historical values, then manually setting a component clears that // set in bulk with its historical values, then manually setting a component clears that
// `explicitly_set` state. // `explicitly_set` state.
if let Some(component_to_enable) = component.strip_prefix('+') { if let Some(component_to_enable) = component.strip_prefix('+') {
self.explicitly_set = None; self.explicitly_set = None;
self.components.insert(component_to_enable.parse()?); self.components.insert(LinkSelfContainedComponents::from_str(component_to_enable)?);
Ok(()) Some(())
} else if let Some(component_to_disable) = component.strip_prefix('-') { } else if let Some(component_to_disable) = component.strip_prefix('-') {
self.explicitly_set = None; self.explicitly_set = None;
self.components.remove(component_to_disable.parse()?); self.components.remove(LinkSelfContainedComponents::from_str(component_to_disable)?);
Ok(()) Some(())
} else { } else {
Err(()) None
} }
} }

View File

@ -1165,7 +1165,7 @@ mod parse {
// 2. Parse a list of enabled and disabled components. // 2. Parse a list of enabled and disabled components.
for comp in s.split(',') { for comp in s.split(',') {
if slot.handle_cli_component(comp).is_err() { if slot.handle_cli_component(comp).is_none() {
return false; return false;
} }
} }

View File

@ -539,18 +539,17 @@ bitflags::bitflags! {
} }
} }
impl FromStr for LinkSelfContainedComponents { impl LinkSelfContainedComponents {
type Err = (); /// Parses a single `-Clink-self-contained` well-known component, not a set of flags.
pub fn from_str(s: &str) -> Option<LinkSelfContainedComponents> {
fn from_str(s: &str) -> Result<Self, Self::Err> { Some(match s {
Ok(match s {
"crto" => LinkSelfContainedComponents::CRT_OBJECTS, "crto" => LinkSelfContainedComponents::CRT_OBJECTS,
"libc" => LinkSelfContainedComponents::LIBC, "libc" => LinkSelfContainedComponents::LIBC,
"unwind" => LinkSelfContainedComponents::UNWIND, "unwind" => LinkSelfContainedComponents::UNWIND,
"linker" => LinkSelfContainedComponents::LINKER, "linker" => LinkSelfContainedComponents::LINKER,
"sanitizers" => LinkSelfContainedComponents::SANITIZERS, "sanitizers" => LinkSelfContainedComponents::SANITIZERS,
"mingw" => LinkSelfContainedComponents::MINGW, "mingw" => LinkSelfContainedComponents::MINGW,
_ => return Err(()), _ => return None,
}) })
} }
} }