Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Commit

Permalink
Merge pull request #120 from macisamuele/maci-issue-119-default-support
Browse files Browse the repository at this point in the history
Add Default implementation for Value
  • Loading branch information
dtolnay committed Dec 1, 2018
2 parents 35ab8fc + 91ab3e9 commit 3a1a324
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/value/mod.rs
Expand Up @@ -40,6 +40,47 @@ pub enum Value {
Mapping(Mapping),
}

/// The default value is `Value::Null`.
///
/// This is useful for handling omitted `Value` fields when deserializing.
///
/// # Examples
///
/// ```rust
/// # #[macro_use]
/// # extern crate serde_derive;
/// #
/// # extern crate serde_yaml;
/// #
/// use serde_yaml::Value;
///
/// #[derive(Deserialize)]
/// struct Settings {
/// level: i32,
/// #[serde(default)]
/// extras: Value,
/// }
///
/// # fn try_main() -> Result<(), serde_yaml::Error> {
/// let data = r#" { "level": 42 } "#;
/// let s: Settings = serde_yaml::from_str(data)?;
///
/// assert_eq!(s.level, 42);
/// assert_eq!(s.extras, Value::Null);
/// #
/// # Ok(())
/// # }
/// #
/// # fn main() {
/// # try_main().unwrap()
/// # }
/// ```
impl Default for Value {
fn default() -> Value {
Value::Null
}
}

/// A YAML sequence in which the elements are `serde_yaml::Value`.
pub type Sequence = Vec<Value>;

Expand Down
5 changes: 5 additions & 0 deletions tests/test_serde.rs
Expand Up @@ -49,6 +49,11 @@ where
serde_yaml::from_str::<serde::de::IgnoredAny>(yaml).unwrap();
}

#[test]
fn test_default() {
assert_eq!(Value::default(), Value::Null);
}

#[test]
fn test_int() {
let thing = 256;
Expand Down

0 comments on commit 3a1a324

Please sign in to comment.