Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message for an array value in the manifest #10944

Merged
merged 2 commits into from
Aug 16, 2022
Merged
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
47 changes: 47 additions & 0 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,43 @@ impl<T> MaybeWorkspace<T> {
}
}

fn maybe_workspace_vec_string<'de, D>(
deserializer: D,
) -> Result<Option<MaybeWorkspace<Vec<String>>>, D::Error>
where
D: de::Deserializer<'de>,
{
struct Visitor;

impl<'de> de::Visitor<'de> for Visitor {
type Value = Option<MaybeWorkspace<Vec<String>>>;

fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("vector of strings")
}

fn visit_seq<V>(self, v: V) -> Result<Self::Value, V::Error>
where
V: de::SeqAccess<'de>,
{
let seq = de::value::SeqAccessDeserializer::new(v);
let defined = Vec::<String>::deserialize(seq).map(MaybeWorkspace::Defined)?;
Ok(Some(defined))
}

fn visit_map<V>(self, map: V) -> Result<Self::Value, V::Error>
where
V: de::MapAccess<'de>,
{
let mvd = de::value::MapAccessDeserializer::new(map);
let workspace = TomlWorkspaceField::deserialize(mvd).map(MaybeWorkspace::Workspace)?;
Ok(Some(workspace))
}
}

deserializer.deserialize_any(Visitor)
}

#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct TomlWorkspaceField {
workspace: bool,
Expand All @@ -1060,6 +1097,8 @@ pub struct TomlProject {
name: InternedString,
#[serde(deserialize_with = "version_trim_whitespace")]
version: MaybeWorkspace<semver::Version>,
#[serde(default)]
#[serde(deserialize_with = "maybe_workspace_vec_string")]
authors: Option<MaybeWorkspace<Vec<String>>>,
build: Option<StringOrBool>,
metabuild: Option<StringOrVec>,
Expand All @@ -1068,7 +1107,11 @@ pub struct TomlProject {
#[serde(rename = "forced-target")]
forced_target: Option<String>,
links: Option<String>,
#[serde(default)]
#[serde(deserialize_with = "maybe_workspace_vec_string")]
exclude: Option<MaybeWorkspace<Vec<String>>>,
#[serde(default)]
#[serde(deserialize_with = "maybe_workspace_vec_string")]
include: Option<MaybeWorkspace<Vec<String>>>,
publish: Option<MaybeWorkspace<VecStringOrBool>>,
epage marked this conversation as resolved.
Show resolved Hide resolved
workspace: Option<String>,
Expand All @@ -1084,7 +1127,11 @@ pub struct TomlProject {
homepage: Option<MaybeWorkspace<String>>,
documentation: Option<MaybeWorkspace<String>>,
readme: Option<MaybeWorkspace<StringOrBool>>,
#[serde(default)]
#[serde(deserialize_with = "maybe_workspace_vec_string")]
keywords: Option<MaybeWorkspace<Vec<String>>>,
#[serde(default)]
#[serde(deserialize_with = "maybe_workspace_vec_string")]
categories: Option<MaybeWorkspace<Vec<String>>>,
license: Option<MaybeWorkspace<String>>,
license_file: Option<MaybeWorkspace<String>>,
Expand Down
24 changes: 24 additions & 0 deletions tests/testsuite/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1706,6 +1706,30 @@ Caused by:
.run();
}

#[cargo_test]
fn cargo_metadata_with_invalid_authors_field() {
let p = project()
.file("src/foo.rs", "")
.file(
"Cargo.toml",
r#"
[package]
authors = ""
"#,
)
.build();

p.cargo("metadata")
.with_status(101)
.with_stderr(
r#"[ERROR] failed to parse manifest at `[..]`

Caused by:
invalid type: string "", expected vector of strings for key `package.authors`"#,
)
.run();
}

const MANIFEST_OUTPUT: &str = r#"
{
"packages": [{
Expand Down