Skip to main content

sql_rs/types/
value.rs

1use serde::{Deserialize, Serialize};
2use std::fmt;
3use std::hash::{Hash, Hasher};
4
5#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
6pub enum Value {
7    Null,
8    Integer(i64),
9    Float(f64),
10    Text(String),
11    Blob(Vec<u8>),
12    Boolean(bool),
13}
14
15impl Eq for Value {}
16
17impl Hash for Value {
18    fn hash<H: Hasher>(&self, state: &mut H) {
19        match self {
20            Value::Null => 0.hash(state),
21            Value::Integer(i) => {
22                1.hash(state);
23                i.hash(state);
24            }
25            Value::Float(f) => {
26                2.hash(state);
27                f.to_bits().hash(state);
28            }
29            Value::Text(s) => {
30                3.hash(state);
31                s.hash(state);
32            }
33            Value::Blob(b) => {
34                4.hash(state);
35                b.hash(state);
36            }
37            Value::Boolean(b) => {
38                5.hash(state);
39                b.hash(state);
40            }
41        }
42    }
43}
44
45impl Value {
46    pub fn type_name(&self) -> &'static str {
47        match self {
48            Value::Null => "null",
49            Value::Integer(_) => "integer",
50            Value::Float(_) => "float",
51            Value::Text(_) => "text",
52            Value::Blob(_) => "blob",
53            Value::Boolean(_) => "boolean",
54        }
55    }
56    
57    pub fn as_i64(&self) -> Option<i64> {
58        match self {
59            Value::Integer(i) => Some(*i),
60            _ => None,
61        }
62    }
63    
64    pub fn as_f64(&self) -> Option<f64> {
65        match self {
66            Value::Float(f) => Some(*f),
67            Value::Integer(i) => Some(*i as f64),
68            _ => None,
69        }
70    }
71    
72    pub fn as_str(&self) -> Option<&str> {
73        match self {
74            Value::Text(s) => Some(s),
75            _ => None,
76        }
77    }
78    
79    pub fn as_bool(&self) -> Option<bool> {
80        match self {
81            Value::Boolean(b) => Some(*b),
82            _ => None,
83        }
84    }
85}
86
87impl fmt::Display for Value {
88    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89        match self {
90            Value::Null => write!(f, "NULL"),
91            Value::Integer(i) => write!(f, "{}", i),
92            Value::Float(fl) => write!(f, "{}", fl),
93            Value::Text(s) => write!(f, "{}", s),
94            Value::Blob(b) => write!(f, "<blob {} bytes>", b.len()),
95            Value::Boolean(b) => write!(f, "{}", b),
96        }
97    }
98}
99
100impl From<i64> for Value {
101    fn from(i: i64) -> Self {
102        Value::Integer(i)
103    }
104}
105
106impl From<f64> for Value {
107    fn from(f: f64) -> Self {
108        Value::Float(f)
109    }
110}
111
112impl From<String> for Value {
113    fn from(s: String) -> Self {
114        Value::Text(s)
115    }
116}
117
118impl From<&str> for Value {
119    fn from(s: &str) -> Self {
120        Value::Text(s.to_string())
121    }
122}
123
124impl From<bool> for Value {
125    fn from(b: bool) -> Self {
126        Value::Boolean(b)
127    }
128}
129
130impl From<Vec<u8>> for Value {
131    fn from(v: Vec<u8>) -> Self {
132        Value::Blob(v)
133    }
134}