Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ remove_dir_all = "0.5.1"
same-file = "1"
scopeguard = "1"
semver = "0.9"
strsim = "0.9.1"
sha2 = "0.8"
tar = "0.4"
tempdir = "0.3.4"
Expand Down
8 changes: 8 additions & 0 deletions src/dist/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,4 +448,12 @@ impl Component {
pkg.to_string()
}
}
pub fn description_in_manifest(&self) -> String {
let pkg = self.short_name_in_manifest();
if let Some(ref t) = self.target {
format!("'{}' for target '{}'", pkg, t)
} else {
format!("'{}'", pkg)
}
}
}
4 changes: 2 additions & 2 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,9 +302,9 @@ error_chain! {
description("toolchain does not support components")
display("toolchain '{}' does not support components", t)
}
UnknownComponent(t: String, c: String) {
UnknownComponent(t: String, c: String, s: String) {
description("toolchain does not contain component")
display("toolchain '{}' does not contain component {}", t, c)
display("toolchain '{}' does not contain component {}{}", t, c, s)
}
AddingRequiredComponent(t: String, c: String) {
description("required component cannot be added")
Expand Down
46 changes: 46 additions & 0 deletions src/toolchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@ impl<'a> Toolchain<'a> {
return Err(ErrorKind::UnknownComponent(
self.name.to_string(),
component.description(&manifest),
self.get_component_suggestion(&component, false),
)
.into());
}
Expand All @@ -629,6 +630,50 @@ impl<'a> Toolchain<'a> {
}
}

fn get_component_suggestion(&self, component: &Component, only_instaled: bool) -> String {
use strsim::damerau_levenshtein;;
// Suggest only for very small differences
// High number can result in innacurate suggestions for short queries e.g. `rls`
const MAX_DISTANCE: usize = 3;
Comment thread
hrvolapeter marked this conversation as resolved.

let components = self.list_components();
if let Ok(components) = components {
let min = components
.iter()
.filter(|c| !only_instaled || c.installed)
.map(|c| {
(
damerau_levenshtein(
&c.component.name_in_manifest()[..],
&component.name_in_manifest()[..],
),
c,
)
})
.min_by_key(|t| t.0)
.expect("There should be always at least one component");

// If suggestion is too different don't suggest anything
if min.0 > MAX_DISTANCE {
return String::new();
}
// If component parts are the same include whole description
if min.1.component.short_name_in_manifest() == component.short_name_in_manifest() {
return format!(
" - Did you mean {} (targets differ)?",
min.1.component.description_in_manifest()
);
}

format!(
" - Did you mean '{}'?",
min.1.component.short_name_in_manifest()
)
} else {
String::new()
}
}

pub fn remove_component(&self, mut component: Component) -> Result<()> {
if !self.exists() {
return Err(ErrorKind::ToolchainNotInstalled(self.name.to_owned()).into());
Expand Down Expand Up @@ -674,6 +719,7 @@ impl<'a> Toolchain<'a> {
return Err(ErrorKind::UnknownComponent(
self.name.to_string(),
component.description(&manifest),
self.get_component_suggestion(&component, true),
)
.into());
}
Expand Down
65 changes: 65 additions & 0 deletions tests/cli-v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -920,3 +920,68 @@ fn update_unavailable_force() {
);
});
}

#[test]
fn add_component_suggest_best_match() {
setup(&|config| {
expect_ok(config, &["rustup", "default", "nightly"]);
expect_err(
config,
&["rustup", "component", "add", "rustd"],
"Did you mean 'rustc'?",
);
});
}

#[test]
fn remove_component_suggest_best_match() {
setup(&|config| {
expect_ok(config, &["rustup", "default", "nightly"]);
expect_err(
config,
&["rustup", "component", "remove", "rustd"],
"Did you mean 'rustc'?",
);
});
}

#[test]
fn add_target_suggest_best_match() {
setup(&|config| {
expect_ok(config, &["rustup", "default", "nightly"]);
expect_err(
config,
&[
"rustup",
"target",
"add",
&format!("{}a", clitools::CROSS_ARCH1)[..],
],
&format!(
"Did you mean 'rust-std' for target '{}' (targets differ)?",
clitools::CROSS_ARCH1
)[..],
);
});
}

#[test]
fn remove_target_suggest_best_match() {
setup(&|config| {
expect_ok(config, &["rustup", "default", "nightly"]);
expect_ok(config, &["rustup", "target", "add", clitools::CROSS_ARCH1]);
expect_err(
config,
&[
"rustup",
"target",
"remove",
&format!("{}a", clitools::CROSS_ARCH1)[..],
],
&format!(
"Did you mean 'rust-std' for target '{}' (targets differ)?",
clitools::CROSS_ARCH1
)[..],
);
});
}