-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathvalue.rs
125 lines (110 loc) · 4.22 KB
/
value.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use std::borrow::Cow;
use crate::any::{Any, AnyTypeInfo, AnyTypeInfoKind};
use crate::database::Database;
use crate::error::BoxDynError;
use crate::types::Type;
use crate::value::{Value, ValueRef};
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum AnyValueKind<'a> {
Null(AnyTypeInfoKind),
Bool(bool),
SmallInt(i16),
Integer(i32),
BigInt(i64),
Real(f32),
Double(f64),
Text(Cow<'a, str>),
Blob(Cow<'a, [u8]>),
}
impl AnyValueKind<'_> {
fn type_info(&self) -> AnyTypeInfo {
AnyTypeInfo {
kind: match self {
AnyValueKind::Null(_) => AnyTypeInfoKind::Null,
AnyValueKind::Bool(_) => AnyTypeInfoKind::Bool,
AnyValueKind::SmallInt(_) => AnyTypeInfoKind::SmallInt,
AnyValueKind::Integer(_) => AnyTypeInfoKind::Integer,
AnyValueKind::BigInt(_) => AnyTypeInfoKind::BigInt,
AnyValueKind::Real(_) => AnyTypeInfoKind::Real,
AnyValueKind::Double(_) => AnyTypeInfoKind::Double,
AnyValueKind::Text(_) => AnyTypeInfoKind::Text,
AnyValueKind::Blob(_) => AnyTypeInfoKind::Blob,
},
}
}
pub(in crate::any) fn unexpected<Expected: Type<Any>>(&self) -> Result<Expected, BoxDynError> {
Err(format!("expected {}, got {:?}", Expected::type_info(), self).into())
}
pub(in crate::any) fn try_integer<T>(&self) -> Result<T, BoxDynError>
where
T: Type<Any> + TryFrom<i16> + TryFrom<i32> + TryFrom<i64>,
BoxDynError: From<<T as TryFrom<i16>>::Error>,
BoxDynError: From<<T as TryFrom<i32>>::Error>,
BoxDynError: From<<T as TryFrom<i64>>::Error>,
{
Ok(match self {
AnyValueKind::SmallInt(i) => (*i).try_into()?,
AnyValueKind::Integer(i) => (*i).try_into()?,
AnyValueKind::BigInt(i) => (*i).try_into()?,
_ => return self.unexpected(),
})
}
}
#[derive(Clone, Debug)]
pub struct AnyValue {
#[doc(hidden)]
pub kind: AnyValueKind<'static>,
}
#[derive(Clone, Debug)]
pub struct AnyValueRef<'a> {
pub(crate) kind: AnyValueKind<'a>,
}
impl Value for AnyValue {
type Database = Any;
fn as_ref(&self) -> <Self::Database as Database>::ValueRef<'_> {
AnyValueRef {
kind: match &self.kind {
AnyValueKind::Null(k) => AnyValueKind::Null(*k),
AnyValueKind::Bool(b) => AnyValueKind::Bool(*b),
AnyValueKind::SmallInt(i) => AnyValueKind::SmallInt(*i),
AnyValueKind::Integer(i) => AnyValueKind::Integer(*i),
AnyValueKind::BigInt(i) => AnyValueKind::BigInt(*i),
AnyValueKind::Real(r) => AnyValueKind::Real(*r),
AnyValueKind::Double(d) => AnyValueKind::Double(*d),
AnyValueKind::Text(t) => AnyValueKind::Text(Cow::Borrowed(t)),
AnyValueKind::Blob(b) => AnyValueKind::Blob(Cow::Borrowed(b)),
},
}
}
fn type_info(&self) -> Cow<'_, <Self::Database as Database>::TypeInfo> {
Cow::Owned(self.kind.type_info())
}
fn is_null(&self) -> bool {
matches!(self.kind, AnyValueKind::Null(_))
}
}
impl<'a> ValueRef<'a> for AnyValueRef<'a> {
type Database = Any;
fn to_owned(&self) -> <Self::Database as Database>::Value {
AnyValue {
kind: match &self.kind {
AnyValueKind::Null(k) => AnyValueKind::Null(*k),
AnyValueKind::Bool(b) => AnyValueKind::Bool(*b),
AnyValueKind::SmallInt(i) => AnyValueKind::SmallInt(*i),
AnyValueKind::Integer(i) => AnyValueKind::Integer(*i),
AnyValueKind::BigInt(i) => AnyValueKind::BigInt(*i),
AnyValueKind::Real(r) => AnyValueKind::Real(*r),
AnyValueKind::Double(d) => AnyValueKind::Double(*d),
AnyValueKind::Text(t) => AnyValueKind::Text(Cow::Owned(t.to_string())),
AnyValueKind::Blob(b) => AnyValueKind::Blob(Cow::Owned(b.to_vec())),
},
}
}
fn type_info(&self) -> Cow<'_, <Self::Database as Database>::TypeInfo> {
Cow::Owned(self.kind.type_info())
}
fn is_null(&self) -> bool {
matches!(self.kind, AnyValueKind::Null(_))
}
}