mirror of
https://github.com/rust-lang/cargo
synced 2024-08-27 03:09:25 +00:00
Ues strip_prefix for cleaner code
This commit is contained in:
parent
24840d9b6d
commit
44666f7377
|
@ -35,8 +35,8 @@ fn get_target() -> String {
|
||||||
.expect("rustc failed to run");
|
.expect("rustc failed to run");
|
||||||
let stdout = String::from_utf8(output.stdout).unwrap();
|
let stdout = String::from_utf8(output.stdout).unwrap();
|
||||||
for line in stdout.lines() {
|
for line in stdout.lines() {
|
||||||
if line.starts_with("host: ") {
|
if let Some(line) = line.strip_prefix("host: ") {
|
||||||
return String::from(&line[6..]);
|
return String::from(line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
panic!("Failed to find host: {}", stdout);
|
panic!("Failed to find host: {}", stdout);
|
||||||
|
|
|
@ -126,8 +126,7 @@ impl FromStr for Platform {
|
||||||
type Err = ParseError;
|
type Err = ParseError;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Platform, ParseError> {
|
fn from_str(s: &str) -> Result<Platform, ParseError> {
|
||||||
if s.starts_with("cfg(") && s.ends_with(')') {
|
if let Some(s) = s.strip_prefix("cfg(").and_then(|s| s.strip_suffix(')')) {
|
||||||
let s = &s[4..s.len() - 1];
|
|
||||||
s.parse().map(Platform::Cfg)
|
s.parse().map(Platform::Cfg)
|
||||||
} else {
|
} else {
|
||||||
Platform::validate_named_platform(s)?;
|
Platform::validate_named_platform(s)?;
|
||||||
|
|
|
@ -131,8 +131,11 @@ pub fn validate_crate_contents(
|
||||||
(name, contents)
|
(name, contents)
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
assert!(expected_crate_name.ends_with(".crate"));
|
let base_crate_name = Path::new(
|
||||||
let base_crate_name = Path::new(&expected_crate_name[..expected_crate_name.len() - 6]);
|
expected_crate_name
|
||||||
|
.strip_suffix(".crate")
|
||||||
|
.expect("must end with .crate"),
|
||||||
|
);
|
||||||
let actual_files: HashSet<PathBuf> = files.keys().cloned().collect();
|
let actual_files: HashSet<PathBuf> = files.keys().cloned().collect();
|
||||||
let expected_files: HashSet<PathBuf> = expected_files
|
let expected_files: HashSet<PathBuf> = expected_files
|
||||||
.iter()
|
.iter()
|
||||||
|
|
|
@ -106,17 +106,18 @@ fn list_commands(config: &Config) -> BTreeMap<String, CommandInfo> {
|
||||||
};
|
};
|
||||||
for entry in entries.filter_map(|e| e.ok()) {
|
for entry in entries.filter_map(|e| e.ok()) {
|
||||||
let path = entry.path();
|
let path = entry.path();
|
||||||
let filename = match path.file_name().and_then(|s| s.to_str()) {
|
let Some(filename) = path.file_name().and_then(|s| s.to_str()) else {
|
||||||
Some(filename) => filename,
|
|
||||||
_ => continue,
|
|
||||||
};
|
|
||||||
if !filename.starts_with(prefix) || !filename.ends_with(suffix) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
};
|
||||||
|
let Some(name) = filename
|
||||||
|
.strip_prefix(prefix)
|
||||||
|
.and_then(|s| s.strip_suffix(suffix))
|
||||||
|
else {
|
||||||
|
continue;
|
||||||
|
};
|
||||||
if is_executable(entry.path()) {
|
if is_executable(entry.path()) {
|
||||||
let end = filename.len() - suffix.len();
|
|
||||||
commands.insert(
|
commands.insert(
|
||||||
filename[prefix.len()..end].to_string(),
|
name.to_string(),
|
||||||
CommandInfo::External { path: path.clone() },
|
CommandInfo::External { path: path.clone() },
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -338,7 +338,7 @@ impl EncodableResolve {
|
||||||
let mut to_remove = Vec::new();
|
let mut to_remove = Vec::new();
|
||||||
for (k, v) in metadata.iter().filter(|p| p.0.starts_with(prefix)) {
|
for (k, v) in metadata.iter().filter(|p| p.0.starts_with(prefix)) {
|
||||||
to_remove.push(k.to_string());
|
to_remove.push(k.to_string());
|
||||||
let k = &k[prefix.len()..];
|
let k = k.strip_prefix(prefix).unwrap();
|
||||||
let enc_id: EncodablePackageId = k
|
let enc_id: EncodablePackageId = k
|
||||||
.parse()
|
.parse()
|
||||||
.with_context(|| internal("invalid encoding of checksum in lockfile"))?;
|
.with_context(|| internal("invalid encoding of checksum in lockfile"))?;
|
||||||
|
@ -601,8 +601,8 @@ impl FromStr for EncodablePackageId {
|
||||||
let version = s.next();
|
let version = s.next();
|
||||||
let source_id = match s.next() {
|
let source_id = match s.next() {
|
||||||
Some(s) => {
|
Some(s) => {
|
||||||
if s.starts_with('(') && s.ends_with(')') {
|
if let Some(s) = s.strip_prefix('(').and_then(|s| s.strip_suffix(')')) {
|
||||||
Some(SourceId::from_url(&s[1..s.len() - 1])?)
|
Some(SourceId::from_url(s)?)
|
||||||
} else {
|
} else {
|
||||||
anyhow::bail!("invalid serialized PackageId")
|
anyhow::bail!("invalid serialized PackageId")
|
||||||
}
|
}
|
||||||
|
|
|
@ -216,9 +216,8 @@ impl<'config> ConfigMapAccess<'config> {
|
||||||
// `CARGO_PROFILE_DEV_PACKAGE_`
|
// `CARGO_PROFILE_DEV_PACKAGE_`
|
||||||
let env_prefix = format!("{}_", de.key.as_env_key());
|
let env_prefix = format!("{}_", de.key.as_env_key());
|
||||||
for env_key in de.config.env_keys() {
|
for env_key in de.config.env_keys() {
|
||||||
if env_key.starts_with(&env_prefix) {
|
// `CARGO_PROFILE_DEV_PACKAGE_bar_OPT_LEVEL = 3`
|
||||||
// `CARGO_PROFILE_DEV_PACKAGE_bar_OPT_LEVEL = 3`
|
if let Some(rest) = env_key.strip_prefix(&env_prefix) {
|
||||||
let rest = &env_key[env_prefix.len()..];
|
|
||||||
// `rest = bar_OPT_LEVEL`
|
// `rest = bar_OPT_LEVEL`
|
||||||
let part = rest.splitn(2, '_').next().unwrap();
|
let part = rest.splitn(2, '_').next().unwrap();
|
||||||
// `part = "bar"`
|
// `part = "bar"`
|
||||||
|
|
|
@ -63,8 +63,7 @@ impl Rustc {
|
||||||
let extract = |field: &str| -> CargoResult<&str> {
|
let extract = |field: &str| -> CargoResult<&str> {
|
||||||
verbose_version
|
verbose_version
|
||||||
.lines()
|
.lines()
|
||||||
.find(|l| l.starts_with(field))
|
.find_map(|l| l.strip_prefix(field))
|
||||||
.map(|l| &l[field.len()..])
|
|
||||||
.ok_or_else(|| {
|
.ok_or_else(|| {
|
||||||
anyhow::format_err!(
|
anyhow::format_err!(
|
||||||
"`rustc -vV` didn't have a line for `{}`, got:\n{}",
|
"`rustc -vV` didn't have a line for `{}`, got:\n{}",
|
||||||
|
|
|
@ -2292,9 +2292,9 @@ fn failed_install_retains_temp_directory() {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
// Find the path in the output.
|
// Find the path in the output.
|
||||||
let start = stderr.find("found at `").unwrap() + 10;
|
let stderr = stderr.split_once("found at `").unwrap().1;
|
||||||
let end = stderr[start..].find('.').unwrap() - 1;
|
let end = stderr.find('.').unwrap() - 1;
|
||||||
let path = Path::new(&stderr[start..(end + start)]);
|
let path = Path::new(&stderr[..end]);
|
||||||
assert!(path.exists());
|
assert!(path.exists());
|
||||||
assert!(path.join("release/deps").exists());
|
assert!(path.join("release/deps").exists());
|
||||||
}
|
}
|
||||||
|
|
|
@ -453,10 +453,8 @@ fn verify_lto(output: &Output, krate: &str, krate_info: &str, expected_lto: Lto)
|
||||||
krate, krate_info, line, line2, stderr
|
krate, krate_info, line, line2, stderr
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
let actual_lto = if let Some(index) = line.find("-C lto=") {
|
let actual_lto = if let Some((_, line)) = line.split_once("-C lto=") {
|
||||||
let s = &line[index..];
|
let mode = line.splitn(2, ' ').next().unwrap();
|
||||||
let end = s.find(' ').unwrap();
|
|
||||||
let mode = &line[index..index + end];
|
|
||||||
if mode == "off" {
|
if mode == "off" {
|
||||||
Lto::Off
|
Lto::Off
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -184,10 +184,9 @@ fn known_host_works() {
|
||||||
// Validate the fingerprint while we're here.
|
// Validate the fingerprint while we're here.
|
||||||
let fingerprint = stderr
|
let fingerprint = stderr
|
||||||
.lines()
|
.lines()
|
||||||
.find(|line| line.starts_with(" The ECDSA key fingerprint"))
|
.find_map(|line| line.strip_prefix(" The ECDSA key fingerprint"))
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.trim();
|
.trim();
|
||||||
let fingerprint = &fingerprint[30..];
|
|
||||||
let finger_out = sshd.exec(&["ssh-keygen", "-l", "-f", "/etc/ssh/ssh_host_ecdsa_key.pub"]);
|
let finger_out = sshd.exec(&["ssh-keygen", "-l", "-f", "/etc/ssh/ssh_host_ecdsa_key.pub"]);
|
||||||
let gen_finger = std::str::from_utf8(&finger_out.stdout).unwrap();
|
let gen_finger = std::str::from_utf8(&finger_out.stdout).unwrap();
|
||||||
// <key-size> <fingerprint> <comments…>
|
// <key-size> <fingerprint> <comments…>
|
||||||
|
|
Loading…
Reference in a new issue