1use super::{Null, Type};
2
3#[derive(Clone, Debug, PartialEq)]
9pub enum Value {
10    Null,
12    Integer(i64),
14    Real(f64),
16    Text(String),
18    Blob(Vec<u8>),
20}
21
22impl From<Null> for Value {
23    #[inline]
24    fn from(_: Null) -> Value {
25        Value::Null
26    }
27}
28
29impl From<bool> for Value {
30    #[inline]
31    fn from(i: bool) -> Value {
32        Value::Integer(i as i64)
33    }
34}
35
36impl From<isize> for Value {
37    #[inline]
38    fn from(i: isize) -> Value {
39        Value::Integer(i as i64)
40    }
41}
42
43#[cfg(feature = "i128_blob")]
44#[cfg_attr(docsrs, doc(cfg(feature = "i128_blob")))]
45impl From<i128> for Value {
46    #[inline]
47    fn from(i: i128) -> Value {
48        Value::Blob(i128::to_be_bytes(i ^ (1_i128 << 127)).to_vec())
51    }
52}
53
54#[cfg(feature = "uuid")]
55#[cfg_attr(docsrs, doc(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    #[must_use]
133    pub fn data_type(&self) -> Type {
134        match *self {
135            Value::Null => Type::Null,
136            Value::Integer(_) => Type::Integer,
137            Value::Real(_) => Type::Real,
138            Value::Text(_) => Type::Text,
139            Value::Blob(_) => Type::Blob,
140        }
141    }
142}