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