Skip to content

Commit

Permalink
refactor: Metadata now uses custom deserializer (#142)
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 authored Aug 22, 2022
1 parent 97d8285 commit d073023
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/specification.rs
Original file line number Diff line number Diff line change
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 d073023

Please sign in to comment.