Files
cdk/crates/cdk-sql-common/src/value.rs
Cesar Rodas 349c773406 Introduce cdk-sql-common
The primary purpose of this new crate is to have a common and shared codebase
for all SQL storage systems. It would force us to write standard SQL using best
practices for all databases.

This crate has been extracted from #878
2025-07-29 11:31:23 -03:00

83 lines
1.6 KiB
Rust

//! Generic Rust value representation for data from the database
/// Generic Value representation of data from the any database
#[derive(Clone, Debug, PartialEq)]
pub enum Value {
/// The value is a `NULL` value.
Null,
/// The value is a signed integer.
Integer(i64),
/// The value is a floating point number.
Real(f64),
/// The value is a text string.
Text(String),
/// The value is a blob of data
Blob(Vec<u8>),
}
impl From<String> for Value {
fn from(value: String) -> Self {
Self::Text(value)
}
}
impl From<&str> for Value {
fn from(value: &str) -> Self {
Self::Text(value.to_owned())
}
}
impl From<&&str> for Value {
fn from(value: &&str) -> Self {
Self::Text(value.to_string())
}
}
impl From<Vec<u8>> for Value {
fn from(value: Vec<u8>) -> Self {
Self::Blob(value)
}
}
impl From<&[u8]> for Value {
fn from(value: &[u8]) -> Self {
Self::Blob(value.to_owned())
}
}
impl From<u8> for Value {
fn from(value: u8) -> Self {
Self::Integer(value.into())
}
}
impl From<i64> for Value {
fn from(value: i64) -> Self {
Self::Integer(value)
}
}
impl From<u32> for Value {
fn from(value: u32) -> Self {
Self::Integer(value.into())
}
}
impl From<bool> for Value {
fn from(value: bool) -> Self {
Self::Integer(if value { 1 } else { 0 })
}
}
impl<T> From<Option<T>> for Value
where
T: Into<Value>,
{
fn from(value: Option<T>) -> Self {
match value {
Some(v) => v.into(),
None => Value::Null,
}
}
}