Skip to content

Commit

Permalink
refactor: Metadata now uses custom deserializer
Browse files Browse the repository at this point in the history
serde_yaml 0.9.7 resulted in a regression,
accepting `metadata:` (empty metadata mappings)
instead of returning Err.

See dtolnay/serde-yaml#304 and dtolnay/serde-yaml#312

This change uses a custom deserializer to error
in this case.
  • Loading branch information
molpopgen committed Aug 22, 2022
1 parent 97d8285 commit 4e4605b
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/specification.rs
Expand Up @@ -2336,10 +2336,27 @@ impl DemeDefaults {
/// ```
#[derive(Clone, Default, Debug, Serialize, Deserialize, Eq, PartialEq)]
pub struct Metadata {
#[serde(flatten)]
#[serde(flatten, deserialize_with = "require_non_empty_metadata")]
metadata: std::collections::BTreeMap<String, serde_yaml::Value>,
}

fn require_non_empty_metadata<'de, D>(
deserializer: D,
) -> Result<std::collections::BTreeMap<String, serde_yaml::Value>, D::Error>
where
D: serde::Deserializer<'de>,
{
let buf = std::collections::BTreeMap::<String, serde_yaml::Value>::deserialize(deserializer)?;

if !buf.is_empty() {
Ok(buf)
} else {
Err(serde::de::Error::custom(
"metadata: cannot be an empty mapping".to_string(),
))
}
}

impl Metadata {
/// `true` if metadata is present, `false` otherwise
fn is_empty(&self) -> bool {
Expand Down

0 comments on commit 4e4605b

Please sign in to comment.