tiberius/
macros.rs

1macro_rules! uint_enum {
2    ($( #[$gattr:meta] )* pub enum $ty:ident { $( $( #[$attr:meta] )* $variant:ident = $val:expr,)* }) => {
3        uint_enum!($( #[$gattr ])* (pub) enum $ty { $( $( #[$attr] )* $variant = $val, )* });
4    };
5    ($( #[$gattr:meta] )* enum $ty:ident { $( $( #[$attr:meta] )* $variant:ident = $val:expr,)* }) => {
6        #[allow(missing_docs)]
7        uint_enum!($( #[$gattr ])* () enum $ty { $( $( #[$attr] )* $variant = $val, )* });
8    };
9
10    ($( #[$gattr:meta] )* ( $($vis:tt)* ) enum $ty:ident { $( $( #[$attr:meta] )* $variant:ident = $val:expr,)* }) => {
11        #[derive(Debug, Copy, Clone, PartialEq, Eq)]
12        $( #[$gattr] )*
13        #[allow(missing_docs)]
14        $( $vis )* enum $ty {
15            $( $( #[$attr ])* $variant = $val, )*
16        }
17
18        impl ::std::convert::TryFrom<u8> for $ty {
19            type Error = ();
20            fn try_from(n: u8) -> ::std::result::Result<$ty, ()> {
21                match n {
22                    $( x if x == $ty::$variant as u8 => Ok($ty::$variant), )*
23                    _ => Err(()),
24                }
25            }
26        }
27
28        impl ::std::convert::TryFrom<u32> for $ty {
29            type Error = ();
30            fn try_from(n: u32) -> ::std::result::Result<$ty, ()> {
31                match n {
32                    $( x if x == $ty::$variant as u32 => Ok($ty::$variant), )*
33                    _ => Err(()),
34                }
35            }
36        }
37    }
38}
39
40macro_rules! to_sql {
41    ($target:ident, $( $ty:ty: ($variant:expr, $val:expr) ;)* ) => {
42        $(
43            impl crate::ToSql for $ty {
44                fn to_sql(&self) -> crate::tds::codec::ColumnData<'_> {
45                    let $target = self;
46                    $variant(Some($val))
47                }
48            }
49
50            impl crate::ToSql for Option<$ty> {
51                fn to_sql(&self) -> crate::tds::codec::ColumnData<'_> {
52                    match self {
53                        Some(val) => {
54                            let $target = val;
55                            $variant(Some($val))
56                        },
57                        None => $variant(None)
58                    }
59                }
60            }
61
62            impl crate::ToSql for &Option<$ty> {
63                fn to_sql(&self) -> crate::tds::codec::ColumnData<'_> {
64                    match self {
65                        Some(val) => {
66                            let $target = val;
67                            $variant(Some($val))
68                        },
69                        None => $variant(None)
70                    }
71                }
72            }
73        )*
74    };
75}
76
77macro_rules! into_sql {
78    ($target:ident, $( $ty:ty: ($variant:expr, $val:expr) ;)* ) => {
79        $(
80            impl<'a> crate::IntoSql<'a> for $ty {
81                fn into_sql(self) -> crate::tds::codec::ColumnData<'a> {
82                    let $target = self;
83                    $variant(Some($val))
84                }
85            }
86
87            impl<'a> crate::IntoSql<'a> for Option<$ty> {
88                fn into_sql(self) -> crate::tds::codec::ColumnData<'a> {
89                    match self {
90                        Some(val) => {
91                            let $target = val;
92                            $variant(Some($val))
93                        },
94                        None => $variant(None)
95                    }
96                }
97            }
98        )*
99    }
100}
101
102macro_rules! from_sql {
103    ($( $ty:ty: $($pat:pat => ($borrowed_val:expr, $owned_val:expr)),* );* ) => {
104        $(
105            impl<'a> crate::FromSql<'a> for $ty {
106                fn from_sql(data: &'a crate::tds::codec::ColumnData<'static>) -> crate::Result<Option<Self>> {
107                    match data {
108                        $( $pat => Ok($borrowed_val), )*
109                        _ => Err(crate::Error::Conversion(format!("cannot interpret {:?} as an {} value", data, stringify!($ty)).into()))
110                    }
111                }
112            }
113
114            impl crate::FromSqlOwned for $ty {
115                fn from_sql_owned(data: crate::tds::codec::ColumnData<'static>) -> crate::Result<Option<Self>> {
116                    match data {
117                        $( $pat => Ok($owned_val), )*
118                        _ => Err(crate::Error::Conversion(format!("cannot interpret {:?} as an {} value", data, stringify!($ty)).into()))
119                    }
120                }
121            }
122        )*
123    };
124    ($( $ty:ty: $($pat:pat => $borrowed_val:expr),* );* ) => {
125        $(
126            impl<'a> crate::FromSql<'a> for $ty {
127                fn from_sql(data: &'a crate::tds::codec::ColumnData<'static>) -> crate::Result<Option<Self>> {
128                    match data {
129                        $( $pat => Ok($borrowed_val), )*
130                        _ => Err(crate::Error::Conversion(format!("cannot interpret {:?} as an {} value", data, stringify!($ty)).into()))
131                    }
132                }
133            }
134
135            impl crate::FromSqlOwned for $ty {
136                fn from_sql_owned(data: crate::tds::codec::ColumnData<'static>) -> crate::Result<Option<Self>> {
137                    match data {
138                        $( $pat => Ok($borrowed_val), )*
139                        _ => Err(crate::Error::Conversion(format!("cannot interpret {:?} as an {} value", data, stringify!($ty)).into()))
140                    }
141                }
142            }
143        )*
144    };
145}