1 use std::fmt;
2 
3 use super::{ToValue, Value, Primitive};
4 
5 impl ToValue for usize {
to_value(&self) -> Value6     fn to_value(&self) -> Value {
7         Value::from(*self)
8     }
9 }
10 
11 impl<'v> From<usize> for Value<'v> {
from(value: usize) -> Self12     fn from(value: usize) -> Self {
13         Value::from_primitive(Primitive::Unsigned(value as u64))
14     }
15 }
16 
17 impl ToValue for isize {
to_value(&self) -> Value18     fn to_value(&self) -> Value {
19         Value::from(*self)
20     }
21 }
22 
23 impl<'v> From<isize> for Value<'v> {
from(value: isize) -> Self24     fn from(value: isize) -> Self {
25         Value::from_primitive(Primitive::Signed(value as i64))
26     }
27 }
28 
29 impl ToValue for u8 {
to_value(&self) -> Value30     fn to_value(&self) -> Value {
31         Value::from(*self)
32     }
33 }
34 
35 impl<'v> From<u8> for Value<'v> {
from(value: u8) -> Self36     fn from(value: u8) -> Self {
37         Value::from_primitive(Primitive::Unsigned(value as u64))
38     }
39 }
40 
41 impl ToValue for u16 {
to_value(&self) -> Value42     fn to_value(&self) -> Value {
43         Value::from(*self)
44     }
45 }
46 
47 impl<'v> From<u16> for Value<'v> {
from(value: u16) -> Self48     fn from(value: u16) -> Self {
49         Value::from_primitive(Primitive::Unsigned(value as u64))
50     }
51 }
52 
53 impl ToValue for u32 {
to_value(&self) -> Value54     fn to_value(&self) -> Value {
55         Value::from(*self)
56     }
57 }
58 
59 impl<'v> From<u32> for Value<'v> {
from(value: u32) -> Self60     fn from(value: u32) -> Self {
61         Value::from_primitive(Primitive::Unsigned(value as u64))
62     }
63 }
64 
65 impl ToValue for u64 {
to_value(&self) -> Value66     fn to_value(&self) -> Value {
67         Value::from(*self)
68     }
69 }
70 
71 impl<'v> From<u64> for Value<'v> {
from(value: u64) -> Self72     fn from(value: u64) -> Self {
73         Value::from_primitive(Primitive::Unsigned(value))
74     }
75 }
76 
77 impl ToValue for i8 {
to_value(&self) -> Value78     fn to_value(&self) -> Value {
79         Value::from(*self)
80     }
81 }
82 
83 impl<'v> From<i8> for Value<'v> {
from(value: i8) -> Self84     fn from(value: i8) -> Self {
85         Value::from_primitive(Primitive::Signed(value as i64))
86     }
87 }
88 
89 impl ToValue for i16 {
to_value(&self) -> Value90     fn to_value(&self) -> Value {
91         Value::from(*self)
92     }
93 }
94 
95 impl<'v> From<i16> for Value<'v> {
from(value: i16) -> Self96     fn from(value: i16) -> Self {
97         Value::from_primitive(Primitive::Signed(value as i64))
98     }
99 }
100 
101 impl ToValue for i32 {
to_value(&self) -> Value102     fn to_value(&self) -> Value {
103         Value::from(*self)
104     }
105 }
106 
107 impl<'v> From<i32> for Value<'v> {
from(value: i32) -> Self108     fn from(value: i32) -> Self {
109         Value::from_primitive(Primitive::Signed(value as i64))
110     }
111 }
112 
113 impl ToValue for i64 {
to_value(&self) -> Value114     fn to_value(&self) -> Value {
115         Value::from(*self)
116     }
117 }
118 
119 impl<'v> From<i64> for Value<'v> {
from(value: i64) -> Self120     fn from(value: i64) -> Self {
121         Value::from_primitive(Primitive::Signed(value))
122     }
123 }
124 
125 impl ToValue for f32 {
to_value(&self) -> Value126     fn to_value(&self) -> Value {
127         Value::from(*self)
128     }
129 }
130 
131 impl<'v> From<f32> for Value<'v> {
from(value: f32) -> Self132     fn from(value: f32) -> Self {
133         Value::from_primitive(Primitive::Float(value as f64))
134     }
135 }
136 
137 impl ToValue for f64 {
to_value(&self) -> Value138     fn to_value(&self) -> Value {
139         Value::from(*self)
140     }
141 }
142 
143 impl<'v> From<f64> for Value<'v> {
from(value: f64) -> Self144     fn from(value: f64) -> Self {
145         Value::from_primitive(Primitive::Float(value))
146     }
147 }
148 
149 impl ToValue for bool {
to_value(&self) -> Value150     fn to_value(&self) -> Value {
151         Value::from(*self)
152     }
153 }
154 
155 impl<'v> From<bool> for Value<'v> {
from(value: bool) -> Self156     fn from(value: bool) -> Self {
157         Value::from_primitive(Primitive::Bool(value))
158     }
159 }
160 
161 impl ToValue for char {
to_value(&self) -> Value162     fn to_value(&self) -> Value {
163         Value::from(*self)
164     }
165 }
166 
167 impl<'v> From<char> for Value<'v> {
from(value: char) -> Self168     fn from(value: char) -> Self {
169         Value::from_primitive(Primitive::Char(value))
170     }
171 }
172 
173 impl<'v> ToValue for &'v str {
to_value(&self) -> Value174     fn to_value(&self) -> Value {
175         Value::from(*self)
176     }
177 }
178 
179 impl<'v> From<&'v str> for Value<'v> {
from(value: &'v str) -> Self180     fn from(value: &'v str) -> Self {
181         Value::from_primitive(Primitive::Str(value))
182     }
183 }
184 
185 impl ToValue for () {
to_value(&self) -> Value186     fn to_value(&self) -> Value {
187         Value::from_primitive(Primitive::None)
188     }
189 }
190 
191 impl<T> ToValue for Option<T>
192 where
193     T: ToValue,
194 {
to_value(&self) -> Value195     fn to_value(&self) -> Value {
196         match *self {
197             Some(ref value) => value.to_value(),
198             None => Value::from_primitive(Primitive::None),
199         }
200     }
201 }
202 
203 impl<'v> ToValue for fmt::Arguments<'v> {
to_value(&self) -> Value204     fn to_value(&self) -> Value {
205         Value::from_debug(self)
206     }
207 }
208 
209 #[cfg(feature = "std")]
210 mod std_support {
211     use super::*;
212 
213     use std::borrow::Cow;
214 
215     impl<T> ToValue for Box<T>
216     where
217         T: ToValue + ?Sized,
218     {
to_value(&self) -> Value219         fn to_value(&self) -> Value {
220             (**self).to_value()
221         }
222     }
223 
224     impl ToValue for String {
to_value(&self) -> Value225         fn to_value(&self) -> Value {
226             Value::from_primitive(Primitive::Str(&*self))
227         }
228     }
229 
230     impl<'v> ToValue for Cow<'v, str> {
to_value(&self) -> Value231         fn to_value(&self) -> Value {
232             Value::from_primitive(Primitive::Str(&*self))
233         }
234     }
235 }
236 
237 #[cfg(test)]
238 mod tests {
239     use super::*;
240     use kv::value::test::Token;
241 
242     #[test]
test_to_value_display()243     fn test_to_value_display() {
244         assert_eq!(42u64.to_value().to_string(), "42");
245         assert_eq!(42i64.to_value().to_string(), "42");
246         assert_eq!(42.01f64.to_value().to_string(), "42.01");
247         assert_eq!(true.to_value().to_string(), "true");
248         assert_eq!('a'.to_value().to_string(), "'a'");
249         assert_eq!(format_args!("a {}", "value").to_value().to_string(), "a value");
250         assert_eq!("a loong string".to_value().to_string(), "\"a loong string\"");
251         assert_eq!(Some(true).to_value().to_string(), "true");
252         assert_eq!(().to_value().to_string(), "None");
253         assert_eq!(Option::None::<bool>.to_value().to_string(), "None");
254     }
255 
256     #[test]
test_to_value_structured()257     fn test_to_value_structured() {
258         assert_eq!(42u64.to_value().to_token(), Token::U64(42));
259         assert_eq!(42i64.to_value().to_token(), Token::I64(42));
260         assert_eq!(42.01f64.to_value().to_token(), Token::F64(42.01));
261         assert_eq!(true.to_value().to_token(), Token::Bool(true));
262         assert_eq!('a'.to_value().to_token(), Token::Char('a'));
263         assert_eq!(format_args!("a {}", "value").to_value().to_token(), Token::Str("a value".into()));
264         assert_eq!("a loong string".to_value().to_token(), Token::Str("a loong string".into()));
265         assert_eq!(Some(true).to_value().to_token(), Token::Bool(true));
266         assert_eq!(().to_value().to_token(), Token::None);
267         assert_eq!(Option::None::<bool>.to_value().to_token(), Token::None);
268     }
269 }
270