Enum serde_yaml::Value [−][src]
pub enum Value { Null, Bool(bool), Number(Number), String(String), Sequence(Sequence), Mapping(Mapping), }
Represents any valid YAML value.
Variants
Represents a YAML null value.
Bool(bool)
Represents a YAML boolean.
Number(Number)
Represents a YAML numerical value, whether integer or floating point.
String(String)
Represents a YAML string.
Sequence(Sequence)
Represents a YAML sequence in which the elements are
serde_yaml::Value
.
Mapping(Mapping)
Represents a YAML mapping in which the keys and values are both
serde_yaml::Value
.
Implementations
impl Value
[src][−]
pub fn get<I: Index>(&self, index: I) -> Option<&Value>
[src][−]
Index into a YAML sequence or map. A string index can be used to access a value in a map, and a usize index can be used to access an element of an sequence.
Returns None
if the type of self
does not match the type of the
index, for example if the index is a string and self
is a sequence or
a number. Also returns None
if the given key does not exist in the map
or the given index is not within the bounds of the sequence.
let object: Value = yaml(r#"{ A: 65, B: 66, C: 67 }"#); let x = object.get("A").unwrap(); assert_eq!(x, 65); let sequence: Value = yaml(r#"[ "A", "B", "C" ]"#); let x = sequence.get(2).unwrap(); assert_eq!(x, &Value::String("C".into())); assert_eq!(sequence.get("A"), None);
Square brackets can also be used to index into a value in a more concise
way. This returns Value::Null
in cases where get
would have returned
None
.
let object = yaml(r#" --- A: [a, á, à] B: [b, b́] C: [c, ć, ć̣, ḉ] 42: true "#); assert_eq!(object["B"][0], Value::String("b".into())); assert_eq!(object[Value::String("D".into())], Value::Null); assert_eq!(object["D"], Value::Null); assert_eq!(object[0]["x"]["y"]["z"], Value::Null); assert_eq!(object[42], Value::Bool(true));
pub fn get_mut<I: Index>(&mut self, index: I) -> Option<&mut Value>
[src][−]
Index into a YAML sequence or map. A string index can be used to access a value in a map, and a usize index can be used to access an element of an sequence.
Returns None
if the type of self
does not match the type of the
index, for example if the index is a string and self
is a sequence or
a number. Also returns None
if the given key does not exist in the map
or the given index is not within the bounds of the sequence.
pub fn is_null(&self) -> bool
[src][−]
Returns true if the Value
is a Null. Returns false otherwise.
For any Value on which is_null
returns true, as_null
is guaranteed
to return Some(())
.
let v: Value = serde_yaml::from_str("null").unwrap(); assert!(v.is_null());
let v: Value = serde_yaml::from_str("false").unwrap(); assert!(!v.is_null());
pub fn as_null(&self) -> Option<()>
[src][−]
If the Value
is a Null, returns (). Returns None otherwise.
let v: Value = serde_yaml::from_str("null").unwrap(); assert_eq!(v.as_null(), Some(()));
let v: Value = serde_yaml::from_str("false").unwrap(); assert_eq!(v.as_null(), None);
pub fn is_bool(&self) -> bool
[src][−]
Returns true if the Value
is a Boolean. Returns false otherwise.
For any Value on which is_boolean
returns true, as_bool
is
guaranteed to return the boolean value.
let v: Value = serde_yaml::from_str("true").unwrap(); assert!(v.is_bool());
let v: Value = serde_yaml::from_str("42").unwrap(); assert!(!v.is_bool());
pub fn as_bool(&self) -> Option<bool>
[src][−]
If the Value
is a Boolean, returns the associated bool. Returns None
otherwise.
let v: Value = serde_yaml::from_str("true").unwrap(); assert_eq!(v.as_bool(), Some(true));
let v: Value = serde_yaml::from_str("42").unwrap(); assert_eq!(v.as_bool(), None);
pub fn is_number(&self) -> bool
[src][−]
Returns true if the Value
is a Number. Returns false otherwise.
let v: Value = serde_yaml::from_str("5").unwrap(); assert!(v.is_number());
let v: Value = serde_yaml::from_str("true").unwrap(); assert!(!v.is_number());
pub fn is_i64(&self) -> bool
[src][−]
Returns true if the Value
is an integer between i64::MIN
and
i64::MAX
.
For any Value on which is_i64
returns true, as_i64
is guaranteed to
return the integer value.
let v: Value = serde_yaml::from_str("1337").unwrap(); assert!(v.is_i64());
let v: Value = serde_yaml::from_str("null").unwrap(); assert!(!v.is_i64());
pub fn as_i64(&self) -> Option<i64>
[src][−]
If the Value
is an integer, represent it as i64 if possible. Returns
None otherwise.
let v: Value = serde_yaml::from_str("1337").unwrap(); assert_eq!(v.as_i64(), Some(1337));
let v: Value = serde_yaml::from_str("false").unwrap(); assert_eq!(v.as_i64(), None);
pub fn is_u64(&self) -> bool
[src][−]
Returns true if the Value
is an integer between u64::MIN
and
u64::MAX
.
For any Value on which is_u64
returns true, as_u64
is guaranteed to
return the integer value.
let v: Value = serde_yaml::from_str("1337").unwrap(); assert!(v.is_u64());
let v: Value = serde_yaml::from_str("null").unwrap(); assert!(!v.is_u64());
pub fn as_u64(&self) -> Option<u64>
[src][−]
If the Value
is an integer, represent it as u64 if possible. Returns
None otherwise.
let v: Value = serde_yaml::from_str("1337").unwrap(); assert_eq!(v.as_u64(), Some(1337));
let v: Value = serde_yaml::from_str("false").unwrap(); assert_eq!(v.as_u64(), None);
pub fn is_f64(&self) -> bool
[src][−]
Returns true if the Value
is a number that can be represented by f64.
For any Value on which is_f64
returns true, as_f64
is guaranteed to
return the floating point value.
Currently this function returns true if and only if both is_i64
and
is_u64
return false but this is not a guarantee in the future.
let v: Value = serde_yaml::from_str("256.01").unwrap(); assert!(v.is_f64());
let v: Value = serde_yaml::from_str("true").unwrap(); assert!(!v.is_f64());
pub fn as_f64(&self) -> Option<f64>
[src][−]
If the Value
is a number, represent it as f64 if possible. Returns
None otherwise.
let v: Value = serde_yaml::from_str("13.37").unwrap(); assert_eq!(v.as_f64(), Some(13.37));
let v: Value = serde_yaml::from_str("false").unwrap(); assert_eq!(v.as_f64(), None);
pub fn is_string(&self) -> bool
[src][−]
Returns true if the Value
is a String. Returns false otherwise.
For any Value on which is_string
returns true, as_str
is guaranteed
to return the string slice.
let v: Value = serde_yaml::from_str("'lorem ipsum'").unwrap(); assert!(v.is_string());
let v: Value = serde_yaml::from_str("42").unwrap(); assert!(!v.is_string());
pub fn as_str(&self) -> Option<&str>
[src][−]
If the Value
is a String, returns the associated str. Returns None
otherwise.
let v: Value = serde_yaml::from_str("'lorem ipsum'").unwrap(); assert_eq!(v.as_str(), Some("lorem ipsum"));
let v: Value = serde_yaml::from_str("false").unwrap(); assert_eq!(v.as_str(), None);
pub fn is_sequence(&self) -> bool
[src][−]
Returns true if the Value
is a sequence. Returns false otherwise.
let v: Value = serde_yaml::from_str("[1, 2, 3]").unwrap(); assert!(v.is_sequence());
let v: Value = serde_yaml::from_str("true").unwrap(); assert!(!v.is_sequence());
pub fn as_sequence(&self) -> Option<&Sequence>
[src][−]
If the Value
is a sequence, return a reference to it if possible.
Returns None otherwise.
let v: Value = serde_yaml::from_str("[1, 2]").unwrap(); assert_eq!(v.as_sequence(), Some(&vec![Value::Number(Number::from(1)), Value::Number(Number::from(2))]));
let v: Value = serde_yaml::from_str("false").unwrap(); assert_eq!(v.as_sequence(), None);
pub fn as_sequence_mut(&mut self) -> Option<&mut Sequence>
[src][−]
If the Value
is a sequence, return a mutable reference to it if
possible. Returns None otherwise.
let mut v: Value = serde_yaml::from_str("[1]").unwrap(); let s = v.as_sequence_mut().unwrap(); s.push(Value::Number(Number::from(2))); assert_eq!(s, &vec![Value::Number(Number::from(1)), Value::Number(Number::from(2))]);
let mut v: Value = serde_yaml::from_str("false").unwrap(); assert_eq!(v.as_sequence_mut(), None);
pub fn is_mapping(&self) -> bool
[src][−]
Returns true if the Value
is a mapping. Returns false otherwise.
let v: Value = serde_yaml::from_str("a: 42").unwrap(); assert!(v.is_mapping());
let v: Value = serde_yaml::from_str("true").unwrap(); assert!(!v.is_mapping());
pub fn as_mapping(&self) -> Option<&Mapping>
[src][−]
If the Value
is a mapping, return a reference to it if possible.
Returns None otherwise.
let v: Value = serde_yaml::from_str("a: 42").unwrap(); let mut expected = Mapping::new(); expected.insert(Value::String("a".into()),Value::Number(Number::from(42))); assert_eq!(v.as_mapping(), Some(&expected));
let v: Value = serde_yaml::from_str("false").unwrap(); assert_eq!(v.as_mapping(), None);
pub fn as_mapping_mut(&mut self) -> Option<&mut Mapping>
[src][−]
If the Value
is a mapping, return a reference to it if possible.
Returns None otherwise.
let mut v: Value = serde_yaml::from_str("a: 42").unwrap(); let m = v.as_mapping_mut().unwrap(); m.insert(Value::String("b".into()), Value::Number(Number::from(21))); let mut expected = Mapping::new(); expected.insert(Value::String("a".into()), Value::Number(Number::from(42))); expected.insert(Value::String("b".into()), Value::Number(Number::from(21))); assert_eq!(m, &expected);
let mut v: Value = serde_yaml::from_str("false").unwrap(); assert_eq!(v.as_mapping_mut(), None);
Trait Implementations
impl Clone for Value
[src][+]
impl Debug for Value
[src][+]
impl Default for Value
[src][+]
impl<'de> Deserialize<'de> for Value
[src][+]
impl<'de> Deserializer<'de> for Value
[src][+]
impl Eq for Value
[src]
impl<'a, T: Clone + Into<Value>> From<&'a [T]> for Value
[src][+]
impl<'a> From<&'a str> for Value
[src][+]
impl<'a> From<Cow<'a, str>> for Value
[src][+]
impl From<Mapping> for Value
[src][+]
impl From<String> for Value
[src][+]
impl<T: Into<Value>> From<Vec<T, Global>> for Value
[src][+]
impl From<bool> for Value
[src][+]
impl From<f32> for Value
[src][+]
impl From<f64> for Value
[src][+]
impl From<i16> for Value
[src][+]
impl From<i32> for Value
[src][+]
impl From<i64> for Value
[src][+]
impl From<i8> for Value
[src][+]
impl From<isize> for Value
[src][+]
impl From<u16> for Value
[src][+]
impl From<u32> for Value
[src][+]
impl From<u64> for Value
[src][+]
impl From<u8> for Value
[src][+]
impl From<usize> for Value
[src][+]
impl<T: Into<Value>> FromIterator<T> for Value
[src][+]
impl Hash for Value
[src][+]
impl Index for Value
[src][+]
impl<'a> Index<&'a Value> for Mapping
[src][+]
impl<I> Index<I> for Value where
I: Index,
[src][+]
I: Index,
impl<'a> IndexMut<&'a Value> for Mapping
[src][+]
impl<I> IndexMut<I> for Value where
I: Index,
[src][+]
I: Index,
impl<'de> IntoDeserializer<'de, Error> for Value
[src][+]
impl<'a> PartialEq<&'a str> for Value
[src][+]
impl PartialEq<String> for Value
[src][+]
impl PartialEq<Value> for Value
[src][+]
impl PartialEq<Value> for str
[src][+]
impl PartialEq<Value> for u16
[src][+]
impl PartialEq<Value> for u32
[src][+]
impl PartialEq<Value> for u64
[src][+]
impl PartialEq<Value> for usize
[src][+]
impl PartialEq<Value> for f32
[src][+]
impl PartialEq<Value> for f64
[src][+]
impl<'a> PartialEq<Value> for &'a str
[src][+]
impl PartialEq<Value> for String
[src][+]
impl PartialEq<Value> for i8
[src][+]
impl PartialEq<Value> for i16
[src][+]
impl PartialEq<Value> for i32
[src][+]
impl PartialEq<Value> for i64
[src][+]
impl PartialEq<Value> for isize
[src][+]
impl PartialEq<Value> for u8
[src][+]
impl PartialEq<bool> for Value
[src][+]
impl PartialEq<f32> for Value
[src][+]
impl<'a> PartialEq<f32> for &'a Value
[src][+]
impl<'a> PartialEq<f32> for &'a mut Value
[src][+]
impl PartialEq<f64> for Value
[src][+]
impl<'a> PartialEq<f64> for &'a Value
[src][+]
impl<'a> PartialEq<f64> for &'a mut Value
[src][+]
impl PartialEq<i16> for Value
[src][+]
impl<'a> PartialEq<i16> for &'a Value
[src][+]
impl<'a> PartialEq<i16> for &'a mut Value
[src][+]
impl PartialEq<i32> for Value
[src][+]
impl<'a> PartialEq<i32> for &'a Value
[src][+]
impl<'a> PartialEq<i32> for &'a mut Value
[src][+]
impl PartialEq<i64> for Value
[src][+]
impl<'a> PartialEq<i64> for &'a Value
[src][+]
impl<'a> PartialEq<i64> for &'a mut Value
[src][+]
impl PartialEq<i8> for Value
[src][+]
impl<'a> PartialEq<i8> for &'a Value
[src][+]
impl<'a> PartialEq<i8> for &'a mut Value
[src][+]
impl PartialEq<isize> for Value
[src][+]
impl<'a> PartialEq<isize> for &'a Value
[src][+]
impl<'a> PartialEq<isize> for &'a mut Value
[src][+]
impl PartialEq<str> for Value
[src][+]
impl PartialEq<u16> for Value
[src][+]
impl<'a> PartialEq<u16> for &'a Value
[src][+]
impl<'a> PartialEq<u16> for &'a mut Value
[src][+]
impl PartialEq<u32> for Value
[src][+]
impl<'a> PartialEq<u32> for &'a Value
[src][+]
impl<'a> PartialEq<u32> for &'a mut Value
[src][+]
impl PartialEq<u64> for Value
[src][+]
impl<'a> PartialEq<u64> for &'a Value
[src][+]
impl<'a> PartialEq<u64> for &'a mut Value
[src][+]
impl PartialEq<u8> for Value
[src][+]
impl<'a> PartialEq<u8> for &'a Value
[src][+]
impl<'a> PartialEq<u8> for &'a mut Value
[src][+]
impl PartialEq<usize> for Value
[src][+]
impl<'a> PartialEq<usize> for &'a Value
[src][+]
impl<'a> PartialEq<usize> for &'a mut Value
[src][+]
impl PartialOrd<Value> for Value
[src][+]
impl Serialize for Value
[src][+]
Auto Trait Implementations
impl RefUnwindSafe for Value
impl Send for Value
impl Sync for Value
impl Unpin for Value
impl UnwindSafe for Value
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src][+]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src][+]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src][+]
T: ?Sized,
impl<T> DeserializeOwned for T where
T: for<'de> Deserialize<'de>,
[src]
T: for<'de> Deserialize<'de>,
impl<T> From<T> for T
[src][+]
impl<T, U> Into<U> for T where
U: From<T>,
[src][+]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src][+]
T: Clone,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src][+]
U: Into<T>,
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src][+]
U: TryFrom<T>,