Skip to content

Commit 04f5a5e

Browse files
committed
Replace Component::new_with_target by Component::try_new
1 parent 79e6407 commit 04f5a5e

File tree

2 files changed

+33
-18
lines changed

2 files changed

+33
-18
lines changed

src/cli/rustup_mode.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1364,8 +1364,7 @@ fn component_add(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
13641364
let target = get_target(m, &distributable);
13651365

13661366
for component in m.get_many::<String>("component").unwrap() {
1367-
let new_component = Component::new_with_target(component, false)
1368-
.unwrap_or_else(|| Component::new(component.to_string(), target.clone(), true));
1367+
let new_component = Component::try_new(component, &distributable, target.as_ref())?;
13691368
distributable.add_component(new_component)?;
13701369
}
13711370

@@ -1385,8 +1384,7 @@ fn component_remove(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
13851384
let target = get_target(m, &distributable);
13861385

13871386
for component in m.get_many::<String>("component").unwrap() {
1388-
let new_component = Component::new_with_target(component, false)
1389-
.unwrap_or_else(|| Component::new(component.to_string(), target.clone(), true));
1387+
let new_component = Component::try_new(component, &distributable, target.as_ref())?;
13901388
distributable.remove_component(new_component)?;
13911389
}
13921390

src/dist/manifest.rs

+31-14
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ use std::str::FromStr;
1818

1919
use anyhow::{anyhow, bail, Context, Result};
2020

21-
use crate::dist::dist::{PartialTargetTriple, Profile, TargetTriple};
21+
use crate::dist::dist::{Profile, TargetTriple};
2222
use crate::errors::*;
23+
use crate::toolchain::distributable::DistributableToolchain;
2324
use crate::utils::toml_utils::*;
2425

2526
use super::{config::Config, dist::ToolchainDesc};
@@ -588,21 +589,37 @@ impl Component {
588589
}
589590
}
590591

591-
pub(crate) fn new_with_target(pkg_with_target: &str, is_extension: bool) -> Option<Self> {
592-
for (pos, _) in pkg_with_target.match_indices('-') {
593-
let pkg = &pkg_with_target[0..pos];
594-
let target = &pkg_with_target[pos + 1..];
595-
if let Some(partial) = PartialTargetTriple::new(target) {
596-
if let Ok(triple) = TargetTriple::try_from(partial) {
597-
return Some(Self {
598-
pkg: pkg.to_string(),
599-
target: Some(triple),
600-
is_extension,
601-
});
602-
}
592+
pub(crate) fn try_new(
593+
name: &str,
594+
distributable: &DistributableToolchain<'_>,
595+
fallback_target: Option<&TargetTriple>,
596+
) -> Result<Self> {
597+
let manifestation = distributable.get_manifestation()?;
598+
let config = manifestation.read_config()?.unwrap_or_default();
599+
let manifest = distributable.get_manifest()?;
600+
let manifest_components = manifest.query_components(distributable.desc(), &config)?;
601+
602+
for component_status in manifest_components {
603+
let short_name = component_status.component.short_name_in_manifest();
604+
let target = component_status.component.target.as_ref();
605+
606+
if name.starts_with(short_name)
607+
&& target.is_some()
608+
&& name == format!("{}-{}", short_name, target.unwrap())
609+
{
610+
return Ok(Component::new(
611+
short_name.to_string(),
612+
target.cloned(),
613+
false,
614+
));
603615
}
604616
}
605-
None
617+
618+
Ok(Component::new(
619+
name.to_string(),
620+
fallback_target.cloned(),
621+
true,
622+
))
606623
}
607624

608625
pub(crate) fn wildcard(&self) -> Self {

0 commit comments

Comments
 (0)