aldrin_core/
key_impls.rs

1use crate::tags::{self, KeyTag, KeyTagImpl, PrimaryKeyTag};
2use crate::{DeserializeError, DeserializeKey, SerializeError, SerializeKey};
3use std::borrow::Cow;
4use uuid::Uuid;
5
6macro_rules! impl_primitive {
7    { $ty:ty $( :primary $primary:ty )? } => {
8        impl_primitive! {
9            $ty $( :primary $primary )?
10            :tag tags::U8, tags::I8, tags::U16, tags::I16,
11                 tags::U32, tags::I32, tags::U64, tags::I64
12        }
13    };
14
15    { $ty:ty $( :primary $primary:ty )? :tag $( $tag:ty ),+ } => {
16        $(
17            impl PrimaryKeyTag for $ty {
18                type KeyTag = $primary;
19            }
20        )?
21
22        $(
23            impl SerializeKey<$tag> for $ty {
24                fn try_as_key(&self) -> Result<<$tag as KeyTagImpl>::Key<'_>, SerializeError> {
25                    (*self).try_into().map_err(|_| SerializeError::UnexpectedValue)
26                }
27            }
28
29            impl DeserializeKey<$tag> for $ty {
30                fn try_from_key(key: <$tag as KeyTagImpl>::Key<'_>,) -> Result<Self, DeserializeError> {
31                    key.try_into().map_err(|_| DeserializeError::UnexpectedValue)
32                }
33            }
34        )+
35    };
36}
37
38impl_primitive!(u8 :primary tags::U8);
39impl_primitive!(i8 :primary tags::I8);
40impl_primitive!(u16 :primary tags::U16);
41impl_primitive!(i16 :primary tags::I16);
42impl_primitive!(u32 :primary tags::U32);
43impl_primitive!(i32 :primary tags::I32);
44impl_primitive!(u64 :primary tags::U64);
45impl_primitive!(i64 :primary tags::I64);
46impl_primitive!(usize :primary tags::U64);
47impl_primitive!(isize :primary tags::I64);
48impl_primitive!(u128);
49impl_primitive!(i128);
50
51impl PrimaryKeyTag for String {
52    type KeyTag = tags::String;
53}
54
55impl SerializeKey<tags::String> for String {
56    fn try_as_key(&self) -> Result<Cow<'_, str>, SerializeError> {
57        Ok(Cow::Borrowed(self))
58    }
59}
60
61impl DeserializeKey<tags::String> for String {
62    fn try_from_key(key: Cow<str>) -> Result<Self, DeserializeError> {
63        Ok(key.into_owned())
64    }
65}
66
67impl PrimaryKeyTag for str {
68    type KeyTag = tags::String;
69}
70
71impl SerializeKey<tags::String> for str {
72    fn try_as_key(&self) -> Result<Cow<'_, Self>, SerializeError> {
73        Ok(Cow::Borrowed(self))
74    }
75}
76
77impl PrimaryKeyTag for Uuid {
78    type KeyTag = tags::Uuid;
79}
80
81impl SerializeKey<tags::Uuid> for Uuid {
82    fn try_as_key(&self) -> Result<Self, SerializeError> {
83        Ok(*self)
84    }
85}
86
87impl DeserializeKey<tags::Uuid> for Uuid {
88    fn try_from_key(key: Self) -> Result<Self, DeserializeError> {
89        Ok(key)
90    }
91}
92
93impl<T: PrimaryKeyTag + ?Sized> PrimaryKeyTag for &T {
94    type KeyTag = T::KeyTag;
95}
96
97impl<T: KeyTag, U: SerializeKey<T> + ?Sized> SerializeKey<T> for &U {
98    fn try_as_key(&self) -> Result<<T::Impl as KeyTagImpl>::Key<'_>, SerializeError> {
99        (**self).try_as_key()
100    }
101}
102
103impl<T: PrimaryKeyTag + ?Sized> PrimaryKeyTag for &mut T {
104    type KeyTag = T::KeyTag;
105}
106
107impl<T: KeyTag, U: SerializeKey<T> + ?Sized> SerializeKey<T> for &mut U {
108    fn try_as_key(&self) -> Result<<T::Impl as KeyTagImpl>::Key<'_>, SerializeError> {
109        (**self).try_as_key()
110    }
111}
112
113impl<T: PrimaryKeyTag + ?Sized> PrimaryKeyTag for Box<T> {
114    type KeyTag = T::KeyTag;
115}
116
117impl<T: KeyTag, U: SerializeKey<T> + ?Sized> SerializeKey<T> for Box<U> {
118    fn try_as_key(&self) -> Result<<T::Impl as KeyTagImpl>::Key<'_>, SerializeError> {
119        (**self).try_as_key()
120    }
121}
122
123impl<T: KeyTag, U: DeserializeKey<T>> DeserializeKey<T> for Box<U> {
124    fn try_from_key(key: <T::Impl as KeyTagImpl>::Key<'_>) -> Result<Self, DeserializeError> {
125        U::try_from_key(key).map(Self::new)
126    }
127}