1 use super::{Null, Type}; 2 3 /// Owning [dynamic type value](http://sqlite.org/datatype3.html). Value's type is typically 4 /// dictated by SQLite (not by the caller). 5 /// 6 /// See [`ValueRef`](crate::types::ValueRef) for a non-owning dynamic type 7 /// value. 8 #[derive(Clone, Debug, PartialEq)] 9 pub enum Value { 10 /// The value is a `NULL` value. 11 Null, 12 /// The value is a signed integer. 13 Integer(i64), 14 /// The value is a floating point number. 15 Real(f64), 16 /// The value is a text string. 17 Text(String), 18 /// The value is a blob of data 19 Blob(Vec<u8>), 20 } 21 22 impl From<Null> for Value { 23 #[inline] from(_: Null) -> Value24 fn from(_: Null) -> Value { 25 Value::Null 26 } 27 } 28 29 impl From<bool> for Value { 30 #[inline] from(i: bool) -> Value31 fn from(i: bool) -> Value { 32 Value::Integer(i as i64) 33 } 34 } 35 36 impl From<isize> for Value { 37 #[inline] from(i: isize) -> Value38 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")))] 45 impl From<i128> for Value { 46 #[inline] from(i: i128) -> Value47 fn from(i: i128) -> Value { 48 // We store these biased (e.g. with the most significant bit flipped) 49 // so that comparisons with negative numbers work properly. 50 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")))] 56 impl From<uuid::Uuid> for Value { 57 #[inline] from(id: uuid::Uuid) -> Value58 fn from(id: uuid::Uuid) -> Value { 59 Value::Blob(id.as_bytes().to_vec()) 60 } 61 } 62 63 macro_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 74 from_i64!(i8); 75 from_i64!(i16); 76 from_i64!(i32); 77 from_i64!(u8); 78 from_i64!(u16); 79 from_i64!(u32); 80 81 impl From<i64> for Value { 82 #[inline] from(i: i64) -> Value83 fn from(i: i64) -> Value { 84 Value::Integer(i) 85 } 86 } 87 88 impl From<f32> for Value { 89 #[inline] from(f: f32) -> Value90 fn from(f: f32) -> Value { 91 Value::Real(f.into()) 92 } 93 } 94 95 impl From<f64> for Value { 96 #[inline] from(f: f64) -> Value97 fn from(f: f64) -> Value { 98 Value::Real(f) 99 } 100 } 101 102 impl From<String> for Value { 103 #[inline] from(s: String) -> Value104 fn from(s: String) -> Value { 105 Value::Text(s) 106 } 107 } 108 109 impl From<Vec<u8>> for Value { 110 #[inline] from(v: Vec<u8>) -> Value111 fn from(v: Vec<u8>) -> Value { 112 Value::Blob(v) 113 } 114 } 115 116 impl<T> From<Option<T>> for Value 117 where 118 T: Into<Value>, 119 { 120 #[inline] from(v: Option<T>) -> Value121 fn from(v: Option<T>) -> Value { 122 match v { 123 Some(x) => x.into(), 124 None => Value::Null, 125 } 126 } 127 } 128 129 impl Value { 130 /// Returns SQLite fundamental datatype. 131 #[inline] 132 #[must_use] data_type(&self) -> Type133 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 } 143