Skip to content

Commit

Permalink
refactor(manifest): Process the package.cargo-features error in serde
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Aug 24, 2023
1 parent 511c17c commit 8927ea9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
32 changes: 21 additions & 11 deletions src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,6 @@ fn read_manifest_from_str(
parse_document(contents, pretty_filename, config)?
};

// Provide a helpful error message for a common user error.
if let Some(package) = toml.get("package").or_else(|| toml.get("project")) {
if let Some(feats) = package.get("cargo-features") {
bail!(
"cargo-features = {} was found in the wrong location: it \
should be set at the top of Cargo.toml before any tables",
feats
);
}
}

let mut unused = BTreeSet::new();
let manifest: TomlManifest = serde_ignored::deserialize(toml.into_deserializer(), |path| {
let mut key = String::new();
Expand Down Expand Up @@ -1536,6 +1525,10 @@ pub struct TomlPackage {
repository: Option<MaybeWorkspaceString>,
resolver: Option<String>,

// Provide a helpful error message for a common user error.
#[serde(rename = "cargo-features", skip_serializing)]
_invalid_cargo_features: Option<InvalidCargoFeatures>,

// Note that this field must come last due to the way toml serialization
// works which requires tables to be emitted after all values.
metadata: Option<toml::Value>,
Expand Down Expand Up @@ -3546,3 +3539,20 @@ impl TomlLintLevel {
}
}
}

#[derive(Copy, Clone, Debug)]
#[non_exhaustive]
struct InvalidCargoFeatures {}

impl<'de> de::Deserialize<'de> for InvalidCargoFeatures {
fn deserialize<D>(_d: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
use serde::de::Error as _;

Err(D::Error::custom(
"the field `cargo-features` should be set at the top of Cargo.toml before any tables",
))
}
}
4 changes: 2 additions & 2 deletions tests/testsuite/cargo_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,8 @@ fn wrong_position() {
error: failed to parse manifest at [..]
Caused by:
cargo-features = [\"test-dummy-unstable\"] was found in the wrong location: it \
should be set at the top of Cargo.toml before any tables
the field `cargo-features` should be set at the top of Cargo.toml before any tables
in `package.cargo-features`
",
)
.run();
Expand Down

0 comments on commit 8927ea9

Please sign in to comment.