serde_yaml/value/
from.rs

1use crate::{
2  Mapping,
3  Value,
4};
5
6// Implement a bunch of conversion to make it easier to create YAML values
7// on the fly.
8
9macro_rules! from_number {
10    ($($ty:ident)*) => {
11        $(
12            impl From<$ty> for Value {
13                fn from(n: $ty) -> Self {
14                    Value::Number(n.into())
15                }
16            }
17        )*
18    };
19}
20
21from_number! {
22    i8 i16 i32 i64 isize
23    u8 u16 u32 u64 usize
24    f32 f64
25}
26
27impl From<bool> for Value {
28  /// Convert boolean to `Value`
29  ///
30  /// # Examples
31  ///
32  /// ```
33  /// use serde_yaml::Value;
34  ///
35  /// let b = false;
36  /// let x: Value = b.into();
37  /// ```
38  fn from(f: bool) -> Self {
39    Value::Bool(f)
40  }
41}
42
43impl From<String> for Value {
44  /// Convert `String` to `Value`
45  ///
46  /// # Examples
47  ///
48  /// ```
49  /// use serde_yaml::Value;
50  ///
51  /// let s: String = "lorem".to_string();
52  /// let x: Value = s.into();
53  /// ```
54  fn from(f: String) -> Self {
55    Value::String(f)
56  }
57}
58
59impl From<&str> for Value {
60  /// Convert string slice to `Value`
61  ///
62  /// # Examples
63  ///
64  /// ```
65  /// use serde_yaml::Value;
66  ///
67  /// let s: &str = "lorem";
68  /// let x: Value = s.into();
69  /// ```
70  fn from(f: &str) -> Self {
71    Value::String(f.to_string())
72  }
73}
74
75use std::borrow::Cow;
76
77impl<'a> From<Cow<'a, str>> for Value {
78  /// Convert copy-on-write string to `Value`
79  ///
80  /// # Examples
81  ///
82  /// ```
83  /// use serde_yaml::Value;
84  /// use std::borrow::Cow;
85  ///
86  /// let s: Cow<str> = Cow::Borrowed("lorem");
87  /// let x: Value = s.into();
88  /// ```
89  ///
90  /// ```
91  /// use serde_yaml::Value;
92  /// use std::borrow::Cow;
93  ///
94  /// let s: Cow<str> = Cow::Owned("lorem".to_string());
95  /// let x: Value = s.into();
96  /// ```
97  fn from(f: Cow<'a, str>) -> Self {
98    Value::String(f.to_string())
99  }
100}
101
102impl From<Mapping> for Value {
103  /// Convert map (with string keys) to `Value`
104  ///
105  /// # Examples
106  ///
107  /// ```
108  /// use serde_yaml::{Mapping, Value};
109  ///
110  /// let mut m = Mapping::new();
111  /// m.insert("Lorem".into(), "ipsum".into());
112  /// let x: Value = m.into();
113  /// ```
114  fn from(f: Mapping) -> Self {
115    Value::Mapping(f)
116  }
117}
118
119impl<T: Into<Value>> From<Vec<T>> for Value {
120  /// Convert a `Vec` to `Value`
121  ///
122  /// # Examples
123  ///
124  /// ```
125  /// use serde_yaml::Value;
126  ///
127  /// let v = vec!["lorem", "ipsum", "dolor"];
128  /// let x: Value = v.into();
129  /// ```
130  fn from(f: Vec<T>) -> Self {
131    Value::Sequence(f.into_iter().map(Into::into).collect())
132  }
133}
134
135impl<'a, T: Clone + Into<Value>> From<&'a [T]> for Value {
136  /// Convert a slice to `Value`
137  ///
138  /// # Examples
139  ///
140  /// ```
141  /// use serde_yaml::Value;
142  ///
143  /// let v: &[&str] = &["lorem", "ipsum", "dolor"];
144  /// let x: Value = v.into();
145  /// ```
146  fn from(f: &'a [T]) -> Self {
147    Value::Sequence(f.iter().cloned().map(Into::into).collect())
148  }
149}
150
151impl<T: Into<Value>> FromIterator<T> for Value {
152  /// Convert an iteratable type to a YAML sequence
153  ///
154  /// # Examples
155  ///
156  /// ```
157  /// use serde_yaml::Value;
158  ///
159  /// let v = std::iter::repeat(42).take(5);
160  /// let x: Value = v.collect();
161  /// ```
162  ///
163  /// ```
164  /// use serde_yaml::Value;
165  ///
166  /// let v: Vec<_> = vec!["lorem", "ipsum", "dolor"];
167  /// let x: Value = v.into_iter().collect();
168  /// ```
169  ///
170  /// ```
171  /// use std::iter::FromIterator;
172  /// use serde_yaml::Value;
173  ///
174  /// let x: Value = Value::from_iter(vec!["lorem", "ipsum", "dolor"]);
175  /// ```
176  fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
177    let vec = iter.into_iter().map(T::into).collect();
178
179    Value::Sequence(vec)
180  }
181}