-
Notifications
You must be signed in to change notification settings - Fork 216
/
scalars.rs
82 lines (76 loc) 路 2.55 KB
/
scalars.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
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
/// Prisma's builtin scalar types.
#[derive(Debug, Copy, PartialEq, Clone, Serialize, Deserialize, Eq, Hash)]
pub enum ScalarType {
Int,
Float,
Decimal,
Boolean,
String,
DateTime,
}
impl ScalarType {
pub fn from_str(s: &str) -> Result<Self, String> {
match s {
"Int" => Ok(ScalarType::Int),
"Float" => Ok(ScalarType::Float),
"Decimal" => Ok(ScalarType::Decimal),
"Boolean" => Ok(ScalarType::Boolean),
"String" => Ok(ScalarType::String),
"DateTime" => Ok(ScalarType::DateTime),
_ => Err(format!("type {} is not a known scalar type.", s)),
}
}
}
impl ToString for ScalarType {
fn to_string(&self) -> String {
match self {
ScalarType::Int => String::from("Int"),
ScalarType::Float => String::from("Float"),
ScalarType::Decimal => String::from("Decimal"),
ScalarType::Boolean => String::from("Boolean"),
ScalarType::String => String::from("String"),
ScalarType::DateTime => String::from("DateTime"),
}
}
}
/// Value types for Prisma's builtin scalar types.
#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub enum ScalarValue {
Int(i32),
Float(f64),
Decimal(f64),
Boolean(bool),
String(String),
DateTime(DateTime<Utc>),
ConstantLiteral(String),
}
impl ScalarValue {
pub fn get_type(&self) -> ScalarType {
match self {
ScalarValue::Int(_) => ScalarType::Int,
ScalarValue::Float(_) => ScalarType::Float,
ScalarValue::Decimal(_) => ScalarType::Decimal,
ScalarValue::Boolean(_) => ScalarType::Boolean,
ScalarValue::String(_) => ScalarType::String,
ScalarValue::DateTime(_) => ScalarType::DateTime,
ScalarValue::ConstantLiteral(_) => {
panic!("Constant literal values do not map to a base type and should never surface.")
}
}
}
}
impl ToString for ScalarValue {
fn to_string(&self) -> String {
match self {
ScalarValue::Int(val) => val.to_string(),
ScalarValue::Float(val) => val.to_string(),
ScalarValue::Decimal(val) => val.to_string(),
ScalarValue::Boolean(val) => val.to_string(),
ScalarValue::String(val) => val.to_string(),
ScalarValue::DateTime(val) => val.to_string(),
ScalarValue::ConstantLiteral(value) => value.to_string(),
}
}
}