strut_rabbitmq/util/
morph.rs

1use lapin::types::{AMQPValue, ShortString};
2
3/// Artificial trait implemented for a few types like [`AMQPValue`] or
4/// [`ShortString`] to allow infallibly morphing (instantiating) them from
5/// standard Rust types.
6pub trait Morph<T> {
7    /// Flexibly generates an instance of `Self` from a value of type `T`.
8    fn morph(from: T) -> Self;
9}
10
11/// Implement [`Morph`] for [`AMQPValue`].
12const _: () = {
13    impl Morph<bool> for AMQPValue {
14        fn morph(from: bool) -> Self {
15            Self::Boolean(from)
16        }
17    }
18
19    impl Morph<&str> for AMQPValue {
20        fn morph(from: &str) -> Self {
21            Self::LongString(from.as_bytes().into())
22        }
23    }
24
25    impl Morph<String> for AMQPValue {
26        fn morph(from: String) -> Self {
27            Self::LongString(from.into_bytes().into())
28        }
29    }
30
31    impl Morph<i8> for AMQPValue {
32        fn morph(from: i8) -> Self {
33            Self::ShortShortInt(from)
34        }
35    }
36
37    impl Morph<i16> for AMQPValue {
38        fn morph(from: i16) -> Self {
39            Self::ShortInt(from)
40        }
41    }
42
43    impl Morph<i32> for AMQPValue {
44        fn morph(from: i32) -> Self {
45            Self::LongInt(from)
46        }
47    }
48
49    impl Morph<i64> for AMQPValue {
50        fn morph(from: i64) -> Self {
51            Self::LongLongInt(from)
52        }
53    }
54
55    impl Morph<isize> for AMQPValue {
56        fn morph(from: isize) -> Self {
57            Self::LongLongInt(from as i64)
58        }
59    }
60
61    impl Morph<u8> for AMQPValue {
62        fn morph(from: u8) -> Self {
63            Self::ShortShortUInt(from)
64        }
65    }
66
67    impl Morph<u16> for AMQPValue {
68        fn morph(from: u16) -> Self {
69            Self::ShortUInt(from)
70        }
71    }
72
73    impl Morph<u32> for AMQPValue {
74        fn morph(from: u32) -> Self {
75            Self::LongUInt(from)
76        }
77    }
78
79    impl Morph<u64> for AMQPValue {
80        fn morph(from: u64) -> Self {
81            Self::Timestamp(from)
82        }
83    }
84
85    impl Morph<usize> for AMQPValue {
86        fn morph(from: usize) -> Self {
87            Self::Timestamp(from as u64)
88        }
89    }
90
91    impl Morph<f32> for AMQPValue {
92        fn morph(from: f32) -> Self {
93            Self::Float(from)
94        }
95    }
96
97    impl Morph<f64> for AMQPValue {
98        fn morph(from: f64) -> Self {
99            Self::Double(from)
100        }
101    }
102};
103
104/// Implement [`Morph`] for [`ShortString`].
105const _: () = {
106    impl Morph<bool> for ShortString {
107        fn morph(from: bool) -> Self {
108            from.to_string().into()
109        }
110    }
111
112    impl Morph<&str> for ShortString {
113        fn morph(from: &str) -> Self {
114            from.into()
115        }
116    }
117
118    impl Morph<String> for ShortString {
119        fn morph(from: String) -> Self {
120            from.into()
121        }
122    }
123
124    impl Morph<i8> for ShortString {
125        fn morph(from: i8) -> Self {
126            from.to_string().into()
127        }
128    }
129
130    impl Morph<i16> for ShortString {
131        fn morph(from: i16) -> Self {
132            from.to_string().into()
133        }
134    }
135
136    impl Morph<i32> for ShortString {
137        fn morph(from: i32) -> Self {
138            from.to_string().into()
139        }
140    }
141
142    impl Morph<i64> for ShortString {
143        fn morph(from: i64) -> Self {
144            from.to_string().into()
145        }
146    }
147
148    impl Morph<isize> for ShortString {
149        fn morph(from: isize) -> Self {
150            from.to_string().into()
151        }
152    }
153
154    impl Morph<u8> for ShortString {
155        fn morph(from: u8) -> Self {
156            from.to_string().into()
157        }
158    }
159
160    impl Morph<u16> for ShortString {
161        fn morph(from: u16) -> Self {
162            from.to_string().into()
163        }
164    }
165
166    impl Morph<u32> for ShortString {
167        fn morph(from: u32) -> Self {
168            from.to_string().into()
169        }
170    }
171
172    impl Morph<u64> for ShortString {
173        fn morph(from: u64) -> Self {
174            from.to_string().into()
175        }
176    }
177
178    impl Morph<usize> for ShortString {
179        fn morph(from: usize) -> Self {
180            from.to_string().into()
181        }
182    }
183
184    impl Morph<f32> for ShortString {
185        fn morph(from: f32) -> Self {
186            from.to_string().into()
187        }
188    }
189
190    impl Morph<f64> for ShortString {
191        fn morph(from: f64) -> Self {
192            from.to_string().into()
193        }
194    }
195};