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
use sqlx_core::encode::Encode;
use sqlx_core::{types::Type, decode::Decode};
use ydb_grpc_bindings::generated::ydb;
use ydb_grpc_bindings::generated::ydb::value::Value;
use ydb_grpc_bindings::generated::ydb::r#type::PrimitiveTypeId;
use super::prelude::*;

#[derive(Debug)]
struct AnotherType;
impl std::fmt::Display for AnotherType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Debug::fmt(self, f)
    }
}
impl std::error::Error for AnotherType {}

macro_rules! ydb_type {
    ($($t:ty = ($info:ident, $val:ident),)+) => {
        $(
        impl Type<Ydb> for $t {
            fn type_info() -> YdbTypeInfo {
                YdbTypeInfo::Primitive(PrimitiveTypeId::$info)
            }
        }
        
        impl Decode<'_, Ydb> for $t {
            fn decode(value: &YdbValue) -> Result<Self, sqlx_core::error::BoxDynError> {
                match value.value() {
                    Value::$val(v) => Ok(v.clone() as $t),
                    _ => Err(Box::new(AnotherType))
                }
            }
        }

        impl<S: ToString + Clone> Type<Ydb> for (S, $t) {
            fn type_info() -> YdbTypeInfo {
                YdbTypeInfo::Primitive(PrimitiveTypeId::$info)
            }
        }
        
        impl<'q, S: ToString + Clone> Encode<'q, Ydb> for (S, $t) {
            fn encode_by_ref(&self, buf: &mut YdbArgumentBuffer) -> sqlx_core::encode::IsNull {
                let value = ydb::Value {
                    value: Some(ydb::value::Value::$val(self.1.clone().into())),
                    ..Default::default()
                };
                let value = ydb::TypedValue {
                    r#type: Some(ydb::Type{r#type: Some(ydb::r#type::Type::TypeId(PrimitiveTypeId::$info.into()))}),
                    value: Some(value),
                };
                buf.insert(self.0.to_string(), value);
                sqlx_core::encode::IsNull::No
            }
        }
    )+}
}


ydb_type! {
    bool = (Bool, BoolValue),
    i8  = (Int8, Int32Value),
    u8  = (Uint8, Uint32Value),
    i16 = (Int16, Int32Value),
    u16 = (Uint16, Uint32Value),
    i32 = (Int32, Int32Value),
    u32 = (Uint32, Uint32Value),
    i64 = (Int64, Int64Value),
    u64 = (Uint64, Uint64Value),
    f32 = (Float, FloatValue),
    f64 = (Double, DoubleValue),
    Vec<u8> = (String, BytesValue),
    String = (Utf8, TextValue),
}