Add parse_metadata to prase metadata

Signed-off-by: hi-rustin <rustin.liu@gmail.com>
This commit is contained in:
hi-rustin 2023-12-09 14:00:18 +08:00
parent 9ebe3b332a
commit 56fdb1d0b6
2 changed files with 21 additions and 2 deletions

View file

@ -734,6 +734,25 @@ impl BuildOutput {
}
}
fn parse_metadata<'a>(
whence: &str,
line: &str,
data: &'a str,
) -> CargoResult<(&'a str, &'a str)> {
let mut iter = data.splitn(2, "=");
let key = iter.next();
let value = iter.next();
match (key, value) {
(Some(a), Some(b)) => Ok((a, b.trim_end())),
_ => bail!(
"invalid output in {whence}: `{line}`\n\
Expected a line with `cargo::metadata=KEY=VALUE` with an `=` character, \
but none was found.\n\
{DOCS_LINK_SUGGESTION}",
),
}
}
for line in input.split(|b| *b == b'\n') {
let line = match str::from_utf8(line) {
Ok(line) => line.trim(),
@ -910,7 +929,7 @@ impl BuildOutput {
"rerun-if-changed" => rerun_if_changed.push(PathBuf::from(value)),
"rerun-if-env-changed" => rerun_if_env_changed.push(value.to_string()),
"metadata" => {
let (key, value) = parse_directive(whence.as_str(), line, &value)?;
let (key, value) = parse_metadata(whence.as_str(), line, &value)?;
metadata.push((key.to_owned(), value.to_owned()));
}
_ => bail!(

View file

@ -5169,7 +5169,7 @@ fn wrong_output() {
"\
[COMPILING] foo [..]
error: invalid output in build script of `foo v0.0.1 ([ROOT]/foo)`: `cargo:example`
Expected a line with `cargo::KEY=VALUE` with an `=` character, but none was found.
Expected a line with `cargo::metadata=KEY=VALUE` with an `=` character, but none was found.
See https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script \
for more information about build script outputs.
",