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
use crate::error;
use crate::frame::Version;
use crate::query;
use num::BigInt;
use std::io::{Cursor, Write};

/// Trait that should be implemented by all types that wish to be serialized to a buffer.
pub trait Serialize {
    /// Serializes given value using the cursor.
    fn serialize(&self, cursor: &mut Cursor<&mut Vec<u8>>, version: Version);

    /// Wrapper for easily starting hierarchical serialization.
    fn serialize_to_vec(&self, version: Version) -> Vec<u8> {
        let mut buf = vec![];

        self.serialize(&mut Cursor::new(&mut buf), version);
        buf
    }
}

/// `FromBytes` should be used to parse an array of bytes into a structure.
pub trait FromBytes {
    /// It gets and array of bytes and should return an implementor struct.
    fn from_bytes(bytes: &[u8]) -> error::Result<Self>
    where
        Self: Sized;
}

/// `FromCursor` should be used to get parsed structure from an `io:Cursor`
/// which bound to an array of bytes.
pub trait FromCursor {
    /// Tries to parse Self from a cursor of bytes.
    fn from_cursor(cursor: &mut Cursor<&[u8]>, version: Version) -> error::Result<Self>
    where
        Self: Sized;
}

/// The trait that allows transformation of `Self` to CDRS query values.
pub trait IntoQueryValues {
    fn into_query_values(self) -> query::QueryValues;
}

pub trait TryFromRow: Sized {
    fn try_from_row(row: crate::types::rows::Row) -> error::Result<Self>;
}

pub trait TryFromUdt: Sized {
    fn try_from_udt(udt: crate::types::udt::Udt) -> error::Result<Self>;
}

impl<const S: usize> Serialize for [u8; S] {
    fn serialize(&self, cursor: &mut Cursor<&mut Vec<u8>>, _version: Version) {
        let _ = cursor.write(self);
    }
}

impl Serialize for &[u8] {
    #[inline]
    fn serialize(&self, cursor: &mut Cursor<&mut Vec<u8>>, _version: Version) {
        let _ = cursor.write(self);
    }
}

impl Serialize for Vec<u8> {
    #[inline]
    fn serialize(&self, cursor: &mut Cursor<&mut Vec<u8>>, _version: Version) {
        let _ = cursor.write(self);
    }
}

impl Serialize for BigInt {
    #[inline]
    fn serialize(&self, cursor: &mut Cursor<&mut Vec<u8>>, _version: Version) {
        let _ = cursor.write(&self.to_signed_bytes_be());
    }
}

macro_rules! impl_serialized {
    ($t:ty) => {
        impl Serialize for $t {
            #[inline]
            fn serialize(&self, cursor: &mut Cursor<&mut Vec<u8>>, _version: Version) {
                let _ = cursor.write(&self.to_be_bytes());
            }
        }
    };
}

impl_serialized!(i8);
impl_serialized!(i16);
impl_serialized!(i32);
impl_serialized!(i64);
impl_serialized!(u8);
impl_serialized!(u16);
impl_serialized!(u32);
impl_serialized!(u64);