Skip to content

Commit 236310e

Browse files
committed
use split_once for cleaner code
1 parent d619310 commit 236310e

File tree

6 files changed

+48
-75
lines changed

6 files changed

+48
-75
lines changed

crates/mdman/src/main.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,16 @@ fn process_args() -> Result<Options, Error> {
9898
let man = args
9999
.next()
100100
.ok_or_else(|| format_err!("--man requires a value"))?;
101-
let parts: Vec<_> = man.splitn(2, '=').collect();
102-
let key_parts: Vec<_> = parts[0].splitn(2, ':').collect();
103-
if parts.len() != 2 || key_parts.len() != 2 {
104-
bail!("--man expected value with form name:1=link");
105-
}
106-
let section: u8 = key_parts[1].parse().with_context(|| {
107-
format!("expected unsigned integer for section, got `{}`", parts[1])
101+
let parts = man.split_once('=').ok_or_else(|| {
102+
anyhow::format_err!("--man expected value with form name:1=link")
103+
})?;
104+
let key_parts = parts.0.split_once(':').ok_or_else(|| {
105+
anyhow::format_err!("--man expected value with form name:1=link")
106+
})?;
107+
let section: u8 = key_parts.1.parse().with_context(|| {
108+
format!("expected unsigned integer for section, got `{}`", parts.1)
108109
})?;
109-
man_map.insert((key_parts[0].to_string(), section), parts[1].to_string());
110+
man_map.insert((key_parts.0.to_string(), section), parts.1.to_string());
110111
}
111112
s => {
112113
sources.push(PathBuf::from(s));

src/cargo/core/compiler/custom_build.rs

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -681,32 +681,24 @@ impl BuildOutput {
681681
Ok(line) => line.trim(),
682682
Err(..) => continue,
683683
};
684-
let mut iter = line.splitn(2, ':');
685-
if iter.next() != Some("cargo") {
686-
// skip this line since it doesn't start with "cargo:"
687-
continue;
688-
}
689-
let data = match iter.next() {
690-
Some(val) => {
684+
let data = match line.split_once(':') {
685+
Some(("cargo", val)) => {
691686
if val.starts_with(":") {
692687
// Line started with `cargo::`.
693-
bail!("unsupported output in {}: `{}`\n\
688+
bail!("unsupported output in {whence}: `{line}`\n\
694689
Found a `cargo::key=value` build directive which is reserved for future use.\n\
695690
Either change the directive to `cargo:key=value` syntax (note the single `:`) or upgrade your version of Rust.\n\
696691
See https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script \
697-
for more information about build script outputs.", whence, line);
692+
for more information about build script outputs.");
698693
}
699694
val
700695
}
701-
None => continue,
696+
_ => continue,
702697
};
703698

704699
// getting the `key=value` part of the line
705-
let mut iter = data.splitn(2, '=');
706-
let key = iter.next();
707-
let value = iter.next();
708-
let (key, value) = match (key, value) {
709-
(Some(a), Some(b)) => (a, b.trim_end()),
700+
let (key, value) = match data.split_once('=') {
701+
Some((a,b)) => (a, b.trim_end()),
710702
// Line started with `cargo:` but didn't match `key=value`.
711703
_ => bail!("invalid output in {}: `{}`\n\
712704
Expected a line with `cargo:key=value` with an `=` character, \
@@ -765,9 +757,7 @@ impl BuildOutput {
765757
check_and_add_target!("bin", Target::is_bin, LinkArgTarget::Bin);
766758
}
767759
"rustc-link-arg-bin" => {
768-
let mut parts = value.splitn(2, '=');
769-
let bin_name = parts.next().unwrap().to_string();
770-
let arg = parts.next().ok_or_else(|| {
760+
let (bin_name, arg) = value.split_once('=').ok_or_else(|| {
771761
anyhow::format_err!(
772762
"invalid instruction `cargo:{}={}` from {}\n\
773763
The instruction should have the form cargo:{}=BIN=ARG",
@@ -790,7 +780,10 @@ impl BuildOutput {
790780
bin_name
791781
);
792782
}
793-
linker_args.push((LinkArgTarget::SingleBin(bin_name), arg.to_string()));
783+
linker_args.push((
784+
LinkArgTarget::SingleBin(bin_name.to_owned()),
785+
arg.to_string(),
786+
));
794787
}
795788
"rustc-link-arg-tests" => {
796789
check_and_add_target!("test", Target::is_test, LinkArgTarget::Test);
@@ -936,12 +929,9 @@ impl BuildOutput {
936929
///
937930
/// [`cargo:rustc-env`]: https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#rustc-env
938931
pub fn parse_rustc_env(value: &str, whence: &str) -> CargoResult<(String, String)> {
939-
let mut iter = value.splitn(2, '=');
940-
let name = iter.next();
941-
let val = iter.next();
942-
match (name, val) {
943-
(Some(n), Some(v)) => Ok((n.to_owned(), v.to_owned())),
944-
_ => bail!("Variable rustc-env has no value in {}: {}", whence, value),
932+
match value.split_once('=') {
933+
Some((n, v)) => Ok((n.to_owned(), v.to_owned())),
934+
_ => bail!("Variable rustc-env has no value in {whence}: {value}"),
945935
}
946936
}
947937
}

src/cargo/core/compiler/mod.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -634,19 +634,9 @@ where
634634
{
635635
let mut search_path = vec![];
636636
for dir in paths {
637-
let dir = match dir.to_str() {
638-
Some(s) => {
639-
let mut parts = s.splitn(2, '=');
640-
match (parts.next(), parts.next()) {
641-
(Some("native"), Some(path))
642-
| (Some("crate"), Some(path))
643-
| (Some("dependency"), Some(path))
644-
| (Some("framework"), Some(path))
645-
| (Some("all"), Some(path)) => path.into(),
646-
_ => dir.clone(),
647-
}
648-
}
649-
None => dir.clone(),
637+
let dir = match dir.to_str().and_then(|s| s.split_once("=")) {
638+
Some(("native" | "crate" | "dependency" | "framework" | "all", path)) => path.into(),
639+
_ => dir.clone(),
650640
};
651641
if dir.starts_with(&root_output) {
652642
search_path.push(dir);

src/cargo/core/package_id_spec.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -123,24 +123,20 @@ impl PackageIdSpec {
123123
)
124124
})?;
125125
match frag {
126-
Some(fragment) => {
127-
let mut parts = fragment.splitn(2, [':', '@']);
128-
let name_or_version = parts.next().unwrap();
129-
match parts.next() {
130-
Some(part) => {
131-
let version = part.to_semver()?;
132-
(InternedString::new(name_or_version), Some(version))
133-
}
134-
None => {
135-
if name_or_version.chars().next().unwrap().is_alphabetic() {
136-
(InternedString::new(name_or_version), None)
137-
} else {
138-
let version = name_or_version.to_semver()?;
139-
(InternedString::new(path_name), Some(version))
140-
}
126+
Some(fragment) => match fragment.split_once([':', '@']) {
127+
Some((name_or_version, part)) => {
128+
let version = part.to_semver()?;
129+
(InternedString::new(name_or_version), Some(version))
130+
}
131+
None => {
132+
if fragment.chars().next().unwrap().is_alphabetic() {
133+
(InternedString::new(&fragment), None)
134+
} else {
135+
let version = fragment.to_semver()?;
136+
(InternedString::new(path_name), Some(version))
141137
}
142138
}
143-
}
139+
},
144140
None => (InternedString::new(path_name), None),
145141
}
146142
};

src/cargo/core/source/source_id.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,8 @@ impl SourceId {
148148
/// 656c58fb7c5ef5f12bc747f");
149149
/// ```
150150
pub fn from_url(string: &str) -> CargoResult<SourceId> {
151-
let mut parts = string.splitn(2, '+');
152-
let kind = parts.next().unwrap();
153-
let url = parts
154-
.next()
151+
let (kind, url) = string
152+
.split_once('+')
155153
.ok_or_else(|| anyhow::format_err!("invalid source `{}`", string))?;
156154

157155
match kind {

src/cargo/sources/registry/index.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -587,15 +587,13 @@ impl<'cfg> RegistryIndex<'cfg> {
587587
// `<pkg>=<p_req>o-><f_req>` where `<pkg>` is the name of a crate on
588588
// this source, `<p_req>` is the version installed and `<f_req> is the
589589
// version requested (argument to `--precise`).
590-
let precise = match source_id.precise() {
591-
Some(p) if p.starts_with(name) && p[name.len()..].starts_with('=') => {
592-
let mut vers = p[name.len() + 1..].splitn(2, "->");
593-
let current_vers = vers.next().unwrap().to_semver().unwrap();
594-
let requested_vers = vers.next().unwrap().to_semver().unwrap();
595-
Some((current_vers, requested_vers))
596-
}
597-
_ => None,
598-
};
590+
let precise = source_id
591+
.precise()
592+
.filter(|p| p.starts_with(name) && p[name.len()..].starts_with('='))
593+
.map(|p| {
594+
let (current, requested) = p[name.len() + 1..].split_once("->").unwrap();
595+
(current.to_semver().unwrap(), requested.to_semver().unwrap())
596+
});
599597
let summaries = summaries.filter(|s| match &precise {
600598
Some((current, requested)) => {
601599
if req.matches(current) {

0 commit comments

Comments
 (0)