cdk_sql_common/
value.rs

1//! Generic Rust value representation for data from the database
2
3/// Generic Value representation of data from the any database
4#[derive(Clone, Debug, PartialEq)]
5pub enum Value {
6    /// The value is a `NULL` value.
7    Null,
8    /// The value is a signed integer.
9    Integer(i64),
10    /// The value is a floating point number.
11    Real(f64),
12    /// The value is a text string.
13    Text(String),
14    /// The value is a blob of data
15    Blob(Vec<u8>),
16}
17
18impl From<String> for Value {
19    fn from(value: String) -> Self {
20        Self::Text(value)
21    }
22}
23
24impl From<&str> for Value {
25    fn from(value: &str) -> Self {
26        Self::Text(value.to_owned())
27    }
28}
29
30impl From<&&str> for Value {
31    fn from(value: &&str) -> Self {
32        Self::Text(value.to_string())
33    }
34}
35
36impl From<Vec<u8>> for Value {
37    fn from(value: Vec<u8>) -> Self {
38        Self::Blob(value)
39    }
40}
41
42impl From<&[u8]> for Value {
43    fn from(value: &[u8]) -> Self {
44        Self::Blob(value.to_owned())
45    }
46}
47
48impl From<u8> for Value {
49    fn from(value: u8) -> Self {
50        Self::Integer(value.into())
51    }
52}
53
54impl From<i64> for Value {
55    fn from(value: i64) -> Self {
56        Self::Integer(value)
57    }
58}
59
60impl From<u32> for Value {
61    fn from(value: u32) -> Self {
62        Self::Integer(value.into())
63    }
64}
65
66impl From<bool> for Value {
67    fn from(value: bool) -> Self {
68        Self::Integer(if value { 1 } else { 0 })
69    }
70}
71
72impl<T> From<Option<T>> for Value
73where
74    T: Into<Value>,
75{
76    fn from(value: Option<T>) -> Self {
77        match value {
78            Some(v) => v.into(),
79            None => Value::Null,
80        }
81    }
82}