Skip to content
Merged
15 changes: 7 additions & 8 deletions src/cli/docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use super::topical_doc;
use crate::{
config::{ActiveSource, Cfg},
dist::{PartialChannelToolchainName, manifest::ComponentStatus},
toolchain::DistributableToolchain,
toolchain::ChannelToolchain,
utils::{self, ExitCode},
};

Expand Down Expand Up @@ -191,7 +191,7 @@ pub(crate) async fn doc(
let toolchain = toolchain.map(|desc| (desc, ActiveSource::CommandLine));
let toolchain = cfg.toolchain_from_partial(toolchain).await?.0;

if let Ok(distributable) = DistributableToolchain::try_from(&toolchain)
if let Ok(distributable) = ChannelToolchain::try_from(toolchain.clone())
&& let [_] = distributable
.components()?
.into_iter()
Expand All @@ -201,9 +201,8 @@ pub(crate) async fn doc(
.as_slice()
{
info!(
"`rust-docs` not installed in toolchain `{}`\nhelp: run `rustup component add --toolchain {} rust-docs` to install it",
"`rust-docs` not installed in toolchain `{0}`\nhelp: run `rustup component add --toolchain {0} rust-docs` to install it",
distributable.desc(),
distributable.desc()
);
return Err(anyhow!(
"unable to view documentation which is not installed"
Expand All @@ -213,12 +212,12 @@ pub(crate) async fn doc(
let (doc_path, fragment) = match (topic, doc_page.name()) {
(Some(topic), Some(name)) => {
let (doc_path, fragment) = doc_page
.resolve(&toolchain.doc_path("")?, topic)
.resolve(&toolchain.doc_path(""), topic)
.context(format!("no document for {name} on {topic}"))?;
(Cow::Owned(doc_path), fragment)
}
(Some(topic), None) => {
let doc_path = topical_doc::local_path(&toolchain.doc_path("").unwrap(), topic)?;
let doc_path = topical_doc::local_path(&toolchain.doc_path(""), topic)?;
(Cow::Owned(doc_path), None)
}
(None, name) => {
Expand All @@ -229,13 +228,13 @@ pub(crate) async fn doc(
};

if path_only {
let doc_path = toolchain.doc_path(&doc_path)?;
let doc_path = toolchain.doc_path(&doc_path);
writeln!(cfg.process.stdout().lock(), "{}", doc_path.display())?;
return Ok(ExitCode::SUCCESS);
}

if serve {
let root = toolchain.doc_path("")?;
let root = toolchain.doc_path("");
serve_and_open(root, &doc_path, fragment).await?;
return Ok(ExitCode::SUCCESS);
}
Expand Down
32 changes: 14 additions & 18 deletions src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use crate::{
install::{InstallMethod, UpdateStatus},
process::{ColorableTerminal, Process},
toolchain::{
CustomToolchainName, DistributableToolchain, MaybePartialToolchainName, Override,
ChannelToolchain, CustomToolchainName, MaybePartialToolchainName, Override,
PartialToolchainName, PartialToolchainNameOrPath, Toolchain, ToolchainName,
ToolchainNameOrPath,
},
Expand Down Expand Up @@ -911,7 +911,7 @@ fn completion_command(cfg: &Cfg<'_>) -> clap::Command {
let ToolchainName::Channel(desc) = name else {
return None;
};
let dist = DistributableToolchain::new(cfg, desc.clone()).ok()?;
let dist = ChannelToolchain::new(cfg, desc.clone()).ok()?;
let components = dist.components().ok()?;
Some(
components
Expand Down Expand Up @@ -1183,7 +1183,7 @@ async fn update(
cfg,
)?;

let status = match DistributableToolchain::new(cfg, desc.clone()) {
let status = match ChannelToolchain::new(cfg, desc.clone()) {
Ok(d) => {
if !opts.no_update {
InstallMethod::Dist(dist_opts.for_update(&d, opts.allow_downgrade))
Expand All @@ -1194,7 +1194,7 @@ async fn update(
}
}
Err(RustupError::ToolchainNotInstalled { .. }) => {
DistributableToolchain::install(dist_opts).await?.status
ChannelToolchain::install(dist_opts).await?.status
}
Err(e) => Err(e)?,
};
Expand Down Expand Up @@ -1391,7 +1391,7 @@ async fn show(cfg: &Cfg<'_>, verbose: bool) -> anyhow::Result<ExitCode> {
writeln!(t.lock(), "installed targets:")?;

let active_toolchain_targets = match active_toolchain_name {
ToolchainName::Channel(desc) => DistributableToolchain::new(cfg, desc)?
ToolchainName::Channel(desc) => ChannelToolchain::new(cfg, desc)?
.components()?
.into_iter()
.filter_map(|c| {
Expand Down Expand Up @@ -1467,7 +1467,7 @@ async fn target_list(
// If a toolchain is Distributable, we can assume it has a manifest and thus print all possible targets and the installed ones.
// However, if it is a custom toolchain, we can only print the installed targets.
// NB: this decision is made based on the absence of a manifest in custom toolchains.
if let Ok(distributable) = DistributableToolchain::from_partial(toolchain.clone(), cfg).await {
if let Ok(distributable) = ChannelToolchain::from_partial(toolchain.clone(), cfg).await {
common::list_items(
distributable.components()?.into_iter().filter_map(|c| {
if c.component.short_name() == "rust-std" && c.available {
Expand Down Expand Up @@ -1501,7 +1501,7 @@ async fn target_add(
// isn't a feature yet.
// list_components *and* add_components would both be inappropriate for
// custom toolchains.
let distributable = DistributableToolchain::from_partial(
let distributable = ChannelToolchain::from_partial(
toolchain.map(|desc| (desc, ActiveSource::CommandLine)),
cfg,
)
Expand Down Expand Up @@ -1542,7 +1542,7 @@ async fn target_remove(
targets: Vec<TargetTuple>,
toolchain: Option<PartialChannelToolchainName>,
) -> anyhow::Result<ExitCode> {
let distributable = DistributableToolchain::from_partial(
let distributable = ChannelToolchain::from_partial(
toolchain.map(|desc| (desc, ActiveSource::CommandLine)),
cfg,
)
Expand Down Expand Up @@ -1575,7 +1575,7 @@ async fn component_list(
let toolchain = toolchain.map(|desc| (desc, ActiveSource::CommandLine));

// downcasting required because the toolchain files can name any toolchain
if let Ok(distributable) = DistributableToolchain::from_partial(toolchain.clone(), cfg).await {
if let Ok(distributable) = ChannelToolchain::from_partial(toolchain.clone(), cfg).await {
common::list_items(
distributable
.components()?
Expand Down Expand Up @@ -1605,7 +1605,7 @@ async fn component_add(
toolchain: Option<PartialChannelToolchainName>,
target: Option<String>,
) -> anyhow::Result<ExitCode> {
let distributable = DistributableToolchain::from_partial(
let distributable = ChannelToolchain::from_partial(
toolchain.map(|desc| (desc, ActiveSource::CommandLine)),
cfg,
)
Expand All @@ -1624,10 +1624,7 @@ async fn component_add(
Ok(ExitCode::SUCCESS)
}

fn get_target(
target: Option<String>,
distributable: &DistributableToolchain<'_>,
) -> Option<TargetTuple> {
fn get_target(target: Option<String>, distributable: &ChannelToolchain<'_>) -> Option<TargetTuple> {
target
.map(TargetTuple::new)
.or_else(|| Some(distributable.desc().target.clone()))
Expand All @@ -1640,7 +1637,7 @@ async fn component_remove(
target: Option<String>,
) -> anyhow::Result<ExitCode> {
let toolchain = toolchain.map(|desc| (desc, ActiveSource::CommandLine));
let distributable = DistributableToolchain::from_partial(toolchain, cfg).await?;
let distributable = ChannelToolchain::from_partial(toolchain, cfg).await?;
let target = get_target(target, &distributable);

distributable
Expand Down Expand Up @@ -1725,8 +1722,7 @@ fn pin_active_toolchain(qualified: bool, cfg: &Cfg<'_>) -> anyhow::Result<ExitCo
.context("no default toolchain to pin")?;
let components = match &default {
PartialToolchainName::Channel(desc) => {
let tc =
DistributableToolchain::new(cfg, desc.clone().complete(&default_host)?)?;
let tc = ChannelToolchain::new(cfg, desc.clone().complete(&default_host)?)?;
let manifest = tc.get_manifest()?;

Some(
Expand Down Expand Up @@ -1793,7 +1789,7 @@ async fn override_add(
ToolchainName::Custom(_) => Err(e)?,
ToolchainName::Channel(desc) => {
let options = DistOptions::new(&[], &[], desc, cfg.get_profile()?, false, cfg)?;
let status = DistributableToolchain::install(options).await?.status;
let status = ChannelToolchain::install(options).await?.status;
writeln!(cfg.process.stdout().lock())?;
common::show_channel_update(
cfg,
Expand Down
9 changes: 5 additions & 4 deletions src/cli/self_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ use crate::{
process::Process,
settings::SettingsFile,
toolchain::{
DistributableToolchain, MaybeChannelToolchainName, PartialToolchainName, Toolchain,
ChannelToolchain, MaybeChannelToolchainName, PartialToolchainName, Toolchain,
ToolchainNameExt as _,
},
utils::{self, ExitCode},
};
Expand Down Expand Up @@ -274,19 +275,19 @@ impl InstallOpts<'_> {
let desc = partial_desc.clone().complete(&cfg.default_host_tuple()?)?;
let options =
DistOptions::new(components, targets, &desc, cfg.get_profile()?, true, &cfg)?;
let status = if Toolchain::exists(&cfg, &desc.clone().into())? {
let status = if desc.exists(&cfg)? {
warn!("Updating existing toolchain, profile choice will be ignored");
// If we have a partial install we might not be able to read content here. We could:
// - fail and folk have to delete the partially present toolchain to recover
// - silently ignore it (and provide inconsistent metadata for reporting the install/update change)
// - delete the partial install and start over
// For now, we error.
let toolchain = DistributableToolchain::new(&cfg, desc.clone())?;
let toolchain = ChannelToolchain::new(&cfg, desc.clone())?;
InstallMethod::Dist(options.for_update(&toolchain, false))
.install(None)
.await?
} else {
DistributableToolchain::install(options).await?.status
ChannelToolchain::install(options).await?.status
};

check_proxy_sanity(&cargo_bin, components, &desc)?;
Expand Down
25 changes: 9 additions & 16 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ use crate::{
process::Process,
settings::{MetadataVersion, Settings, SettingsFile},
toolchain::{
CustomToolchainName, DistributableToolchain, Override, PartialToolchainName,
PartialToolchainNameOrPath, Toolchain, ToolchainName, ToolchainNameOrPath, ToolchainPath,
ChannelToolchain, CustomToolchainName, Override, PartialToolchainName,
PartialToolchainNameOrPath, Toolchain, ToolchainName, ToolchainNameExt as _,
ToolchainNameOrPath, ToolchainPath,
},
utils,
};
Expand Down Expand Up @@ -767,7 +768,7 @@ impl<'a> Cfg<'a> {

// XXX: this awkwardness deals with settings file being locked already
let toolchain_name = toolchain_name.complete(&default_host)?;
if !Toolchain::exists(self, &toolchain_name.clone().into())?
if !toolchain_name.exists(self)?
&& matches!(toolchain_name, ToolchainName::Custom(_))
{
bail!(
Expand Down Expand Up @@ -925,9 +926,9 @@ impl<'a> Cfg<'a> {
self,
)?;

Ok(match DistributableToolchain::new(self, toolchain.clone()) {
Ok(match ChannelToolchain::new(self, toolchain.clone()) {
Err(RustupError::ToolchainNotInstalled { .. }) => {
let tc = DistributableToolchain::install(options).await?;
let tc = ChannelToolchain::install(options).await?;
EnsureInstalled::new(tc.inner.into(), tc.status)
}
Ok(distributable) => {
Expand Down Expand Up @@ -1034,13 +1035,13 @@ impl<'a> Cfg<'a> {

pub(crate) fn list_channels(
&self,
) -> anyhow::Result<Vec<(ChannelToolchainName, DistributableToolchain<'_>)>> {
) -> anyhow::Result<Vec<(ChannelToolchainName, ChannelToolchain<'_>)>> {
let mut channels = self
.list_toolchains(true)?
.into_iter()
.filter_map(|t| match t {
ToolchainName::Channel(n) if n.is_tracking() => {
Some(DistributableToolchain::new(self, n.clone()).map(|t| (n, t)))
Some(ChannelToolchain::new(self, n.clone()).map(|t| (n, t)))
}
_ => None,
})
Expand Down Expand Up @@ -1082,14 +1083,6 @@ impl<'a> Cfg<'a> {
.with(|s| Ok(default_host_tuple(s, self.process)))
}

/// The path on disk of any concrete toolchain
pub(crate) fn toolchain_path(&self, toolchain: &ToolchainNameOrPath) -> PathBuf {
match toolchain {
ToolchainNameOrPath::Named(name) => self.toolchains_dir.join(name.to_string()),
ToolchainNameOrPath::Path(p) => p.to_path_buf(),
}
}

/// Notifies a user with a hint whenever a new Rust release is available.
/// This is only shown at max once per day and only if not in proxy mode.
pub(crate) fn notify_release(&self) -> anyhow::Result<()> {
Expand All @@ -1114,7 +1107,7 @@ impl<'a> Cfg<'a> {
let default_host = self.default_host_tuple()?;
let stable_desc =
PartialChannelToolchainName::from_str("stable")?.complete(&default_host)?;
let stable = match DistributableToolchain::new(self, stable_desc) {
let stable = match ChannelToolchain::new(self, stable_desc) {
Ok(stable) => stable,
// If the `stable` toolchain is not installed, we don't notify the user.
Err(RustupError::ToolchainNotInstalled { .. }) => return Ok(()),
Expand Down
4 changes: 2 additions & 2 deletions src/dist/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use serde::{Deserialize, Serialize};
use crate::{
dist::{ChannelToolchainName, DistError, Profile, TargetTuple, config::Config},
errors::RustupError,
toolchain::DistributableToolchain,
toolchain::ChannelToolchain,
};

/// A download manifest paired with its respective hash, as returned by the dist server.
Expand Down Expand Up @@ -558,7 +558,7 @@ impl Component {

pub(crate) fn try_new(
name: &str,
distributable: &DistributableToolchain<'_>,
distributable: &ChannelToolchain<'_>,
fallback_target: Option<&TargetTuple>,
) -> anyhow::Result<Self> {
let manifest = distributable.get_manifest()?;
Expand Down
6 changes: 4 additions & 2 deletions src/dist/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::{
config::Cfg,
errors::{NIGHTLY_COMPONENT_NOTE, RustupError},
process::Process,
toolchain::DistributableToolchain,
toolchain::{ChannelToolchain, ToolchainNameExt},
utils,
};

Expand Down Expand Up @@ -301,6 +301,8 @@ impl ChannelToolchainName {
}
}

impl ToolchainNameExt for ChannelToolchainName {}

impl FromStr for ChannelToolchainName {
type Err = anyhow::Error;
fn from_str(name: &str) -> anyhow::Result<Self> {
Expand Down Expand Up @@ -884,7 +886,7 @@ impl<'cfg, 'a> DistOptions<'cfg, 'a> {

pub(super) fn for_update(
mut self,
toolchain: &'a DistributableToolchain<'cfg>,
toolchain: &'a ChannelToolchain<'cfg>,
allow_downgrade: bool,
) -> Self {
self.allow_downgrade = allow_downgrade;
Expand Down
6 changes: 3 additions & 3 deletions src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
config::Cfg,
dist::{DistOptions, manifest::ManifestWithHash, prefix::InstallPrefix},
errors::RustupError,
toolchain::{CustomToolchainName, Toolchain, ToolchainNameOrPath},
toolchain::{CustomToolchainName, ToolchainNameExt as _, ToolchainNameOrPath},
utils,
};

Expand Down Expand Up @@ -65,7 +65,7 @@ impl InstallMethod<'_, '_> {
}
};

let toolchain_path = &cfg.toolchain_path(&toolchain);
let toolchain_path = &toolchain.path(cfg);
debug!("toolchain directory: {}", toolchain_path.display());
if toolchain_path.exists() && !matches!(self, Self::Dist { .. }) {
uninstall(toolchain_path)?;
Expand Down Expand Up @@ -95,7 +95,7 @@ impl InstallMethod<'_, '_> {
};

// Final check, to ensure we're installed
if !Toolchain::exists(cfg, &toolchain)? {
if !toolchain.exists(cfg)? {
return Err(RustupError::ToolchainNotInstallable(toolchain.to_string()).into());
}

Expand Down
Loading
Loading