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

Add Default implementation for Value #120

Merged
merged 1 commit into from Dec 1, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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