Skip to main content

rullst_orm/
value.rs

1//! Dynamic runtime value encapsulation for SQL parameter binding and model mapping.
2
3/// Enum dinĂ¢mico para encapsular qualquer tipo que possa ser associado ao banco de dados pelo Macro.
4#[derive(Clone, Debug)]
5pub enum RullstValue {
6    String(String),
7    Int(i32),
8    Float(f64),
9    Bool(bool),
10}
11
12impl From<&str> for RullstValue {
13    fn from(s: &str) -> Self {
14        RullstValue::String(s.to_string())
15    }
16}
17
18impl From<String> for RullstValue {
19    fn from(s: String) -> Self {
20        RullstValue::String(s)
21    }
22}
23
24impl From<i32> for RullstValue {
25    fn from(i: i32) -> Self {
26        RullstValue::Int(i)
27    }
28}
29
30impl From<f64> for RullstValue {
31    fn from(f: f64) -> Self {
32        RullstValue::Float(f)
33    }
34}
35
36impl From<bool> for RullstValue {
37    fn from(b: bool) -> Self {
38        RullstValue::Bool(b)
39    }
40}
41
42impl TryFrom<RullstValue> for String {
43    type Error = &'static str;
44    fn try_from(val: RullstValue) -> Result<Self, Self::Error> {
45        match val {
46            RullstValue::String(s) => Ok(s),
47            _ => Err("Not a string"),
48        }
49    }
50}
51
52impl TryFrom<RullstValue> for i32 {
53    type Error = &'static str;
54    fn try_from(val: RullstValue) -> Result<Self, Self::Error> {
55        match val {
56            RullstValue::Int(i) => Ok(i),
57            _ => Err("Not an i32"),
58        }
59    }
60}
61
62impl TryFrom<RullstValue> for f64 {
63    type Error = &'static str;
64    fn try_from(val: RullstValue) -> Result<Self, Self::Error> {
65        match val {
66            RullstValue::Float(f) => Ok(f),
67            _ => Err("Not an f64"),
68        }
69    }
70}
71
72impl TryFrom<RullstValue> for bool {
73    type Error = &'static str;
74    fn try_from(val: RullstValue) -> Result<Self, Self::Error> {
75        match val {
76            RullstValue::Bool(b) => Ok(b),
77            _ => Err("Not a bool"),
78        }
79    }
80}