serde_yaml/value/from.rs
1use crate::{Mapping, Value};
2
3// Implement a bunch of conversion to make it easier to create YAML values
4// on the fly.
5
6macro_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
18from_number! {
19 i8 i16 i32 i64 isize
20 u8 u16 u32 u64 usize
21 f32 f64
22}
23
24impl 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 /// ```
35 fn from(f: bool) -> Self {
36 Value::Bool(f)
37 }
38}
39
40impl 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 /// ```
51 fn from(f: String) -> Self {
52 Value::String(f)
53 }
54}
55
56impl<'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 /// ```
67 fn from(f: &str) -> Self {
68 Value::String(f.to_string())
69 }
70}
71
72use std::borrow::Cow;
73
74impl<'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 /// ```
94 fn from(f: Cow<'a, str>) -> Self {
95 Value::String(f.to_string())
96 }
97}
98
99impl 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 /// ```
111 fn from(f: Mapping) -> Self {
112 Value::Mapping(f)
113 }
114}
115
116impl<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 /// ```
127 fn from(f: Vec<T>) -> Self {
128 Value::Sequence(f.into_iter().map(Into::into).collect())
129 }
130}
131
132impl<'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 /// ```
143 fn from(f: &'a [T]) -> Self {
144 Value::Sequence(f.iter().cloned().map(Into::into).collect())
145 }
146}
147
148impl<T: Into<Value>> FromIterator<T> for Value {
149 /// Convert an iteratable type to a YAML sequence
150 ///
151 /// # Examples
152 ///
153 /// ```
154 /// use serde_yaml::Value;
155 ///
156 /// let v = std::iter::repeat(42).take(5);
157 /// let x: Value = v.collect();
158 /// ```
159 ///
160 /// ```
161 /// use serde_yaml::Value;
162 ///
163 /// let v: Vec<_> = vec!["lorem", "ipsum", "dolor"];
164 /// let x: Value = v.into_iter().collect();
165 /// ```
166 ///
167 /// ```
168 /// use std::iter::FromIterator;
169 /// use serde_yaml::Value;
170 ///
171 /// let x: Value = Value::from_iter(vec!["lorem", "ipsum", "dolor"]);
172 /// ```
173 fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
174 let vec = iter.into_iter().map(T::into).collect();
175
176 Value::Sequence(vec)
177 }
178}