dysql_tpl/simple/
simple_value.rs1
2use chrono::{NaiveDateTime, DateTime, Utc, Local, FixedOffset};
3use paste::paste;
4use uuid::Uuid;
5
6use super::SimpleError;
7use super::SimpleInnerError;
8
9#[derive(Debug)]
10pub struct RawStr(pub * const str);
11
12unsafe impl Send for RawStr {}
13unsafe impl Sync for RawStr {}
14
15impl RawStr {
16 pub fn as_str(&self) -> Result<&str, SimpleError> {
18 let val = self.0;
19 let val: &str = unsafe {&*val};
20 Ok(val)
21 }
22}
23
24#[derive(Debug)]
25pub struct RawString(pub * const String);
26
27unsafe impl Send for RawString {}
28unsafe impl Sync for RawString {}
29
30impl RawString {
31 pub fn as_string(&self) -> Result<&String, SimpleError> {
33 let val = self.0;
34 let val: &String = unsafe {&*val};
35 Ok(val)
36 }
37}
38
39macro_rules! impl_simple_value_varaint {
40 (
41 $($vtype: ty),*
42 ) => {
43 paste! {
44 #[allow(non_camel_case_types)]
45 #[derive(Debug)]
46 pub enum SimpleValue {
47 $(
48 [<t_ $vtype>]($vtype),
49 )*
50 t_str(RawStr),
51 t_String(RawString),
52 t_Utc(DateTime<Utc>),
53 t_DateTime_Local(DateTime<Local>),
54 t_DateTime_FixedOffset(DateTime<FixedOffset>),
55 None(Option<i32>),
56 }
57 }
58 }
59}
60
61impl_simple_value_varaint!(usize, isize, i64, u64, i32, u32, i16, u16, i8, u8, i128, u128, f32, f64, bool, char, Uuid, NaiveDateTime);
62
63impl SimpleValue {
64 pub fn as_str(&self) -> Result<&str, SimpleError> {
66 if let SimpleValue::t_str(val) = self {
67 let val = val.0;
68 let val: &str = unsafe {&*val};
69 Ok(val)
70 } else {
71 Err(SimpleInnerError(format!("value: '{:?}' convert to &str failed", self)).into())
72 }
73 }
74
75 pub fn as_string(&self) -> Result<&String, SimpleError> {
77 if let SimpleValue::t_String(val) = self {
78 let val = val.0;
79 let val: &String = unsafe {&*val};
80 Ok(val)
81 } else {
82 Err(SimpleInnerError(format!("value: '{:?}' convert to &String failed", self)).into())
83 }
84 }
85}