Skip to main content

walletkit_db/sqlite/
value.rs

1//! Parameter and column value types for the safe `SQLite` wrapper.
2
3/// A value that can be bound to a prepared statement parameter or read from
4/// a result column.
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub enum Value {
7    /// 64-bit signed integer.
8    Integer(i64),
9    /// Binary blob.
10    Blob(Vec<u8>),
11    /// UTF-8 text.
12    Text(String),
13    /// SQL NULL.
14    Null,
15}
16
17impl From<i64> for Value {
18    fn from(v: i64) -> Self {
19        Self::Integer(v)
20    }
21}
22
23impl From<Vec<u8>> for Value {
24    fn from(v: Vec<u8>) -> Self {
25        Self::Blob(v)
26    }
27}
28
29impl From<&[u8]> for Value {
30    fn from(v: &[u8]) -> Self {
31        Self::Blob(v.to_vec())
32    }
33}
34
35impl From<String> for Value {
36    fn from(v: String) -> Self {
37        Self::Text(v)
38    }
39}
40
41impl From<&str> for Value {
42    fn from(v: &str) -> Self {
43        Self::Text(v.to_string())
44    }
45}
46
47/// Convenience macro for building parameter lists.
48///
49/// Usage: `params![1_i64, blob.as_slice(), "text"]`
50#[macro_export]
51macro_rules! params {
52    ($($val:expr),* $(,)?) => {
53        &[$($crate::Value::from($val)),*][..]
54    };
55}