mirror of
https://github.com/aljazceru/cdk.git
synced 2025-12-28 01:55:08 +01:00
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
83 lines
1.6 KiB
Rust
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,
|
|
}
|
|
}
|
|
}
|