1use super::{Null, Type};
2
3#[derive(Clone, Debug, PartialEq)]
8pub enum Value {
9 Null,
11 Integer(i64),
13 Real(f64),
15 Text(String),
17 Blob(Vec<u8>),
19}
20
21impl From<Null> for Value {
22 #[inline]
23 fn from(_: Null) -> Value {
24 Value::Null
25 }
26}
27
28impl From<bool> for Value {
29 #[inline]
30 fn from(i: bool) -> Value {
31 Value::Integer(i as i64)
32 }
33}
34
35impl From<isize> for Value {
36 #[inline]
37 fn from(i: isize) -> Value {
38 Value::Integer(i as i64)
39 }
40}
41
42#[cfg(feature = "i128_blob")]
43impl From<i128> for Value {
44 #[inline]
45 fn from(i: i128) -> Value {
46 use byteorder::{BigEndian, ByteOrder};
47 let mut buf = vec![0u8; 16];
48 BigEndian::write_i128(&mut buf, i ^ (1i128 << 127));
51 Value::Blob(buf)
52 }
53}
54
55#[cfg(feature = "uuid")]
56impl From<uuid::Uuid> for Value {
57 #[inline]
58 fn from(id: uuid::Uuid) -> Value {
59 Value::Blob(id.as_bytes().to_vec())
60 }
61}
62
63macro_rules! from_i64(
64 ($t:ty) => (
65 impl From<$t> for Value {
66 #[inline]
67 fn from(i: $t) -> Value {
68 Value::Integer(i64::from(i))
69 }
70 }
71 )
72);
73
74from_i64!(i8);
75from_i64!(i16);
76from_i64!(i32);
77from_i64!(u8);
78from_i64!(u16);
79from_i64!(u32);
80
81impl From<i64> for Value {
82 #[inline]
83 fn from(i: i64) -> Value {
84 Value::Integer(i)
85 }
86}
87
88impl From<f32> for Value {
89 #[inline]
90 fn from(f: f32) -> Value {
91 Value::Real(f.into())
92 }
93}
94
95impl From<f64> for Value {
96 #[inline]
97 fn from(f: f64) -> Value {
98 Value::Real(f)
99 }
100}
101
102impl From<String> for Value {
103 #[inline]
104 fn from(s: String) -> Value {
105 Value::Text(s)
106 }
107}
108
109impl From<Vec<u8>> for Value {
110 #[inline]
111 fn from(v: Vec<u8>) -> Value {
112 Value::Blob(v)
113 }
114}
115
116impl<T> From<Option<T>> for Value
117where
118 T: Into<Value>,
119{
120 #[inline]
121 fn from(v: Option<T>) -> Value {
122 match v {
123 Some(x) => x.into(),
124 None => Value::Null,
125 }
126 }
127}
128
129impl Value {
130 #[inline]
132 pub fn data_type(&self) -> Type {
133 match *self {
134 Value::Null => Type::Null,
135 Value::Integer(_) => Type::Integer,
136 Value::Real(_) => Type::Real,
137 Value::Text(_) => Type::Text,
138 Value::Blob(_) => Type::Blob,
139 }
140 }
141}