strut_rabbitmq/util/amqp_properties/retrieve.rs
1use crate::util::field_table::retrieve::Retrieve;
2use crate::util::Coerce;
3use lapin::protocol::basic::AMQPProperties;
4use lapin::types::{AMQPValue, ShortString};
5
6/// Artificial trait implemented for [`AMQPProperties`] to allow conveniently
7/// extracting the content type, coercing it into various types.
8pub trait RetrieveContentType<'a, T> {
9 /// Extracts the content type from these [`AMQPProperties`], if it is present
10 /// and can be coerced to type `T`.
11 fn retrieve_content_type(&'a self) -> Option<T>;
12}
13
14/// Implements [`RetrieveContentType`] for every type `T` for which the underlying
15/// [`ShortString`] implements [`Coerce`].
16impl<'a, T> RetrieveContentType<'a, T> for AMQPProperties
17where
18 ShortString: Coerce<'a, T>,
19{
20 fn retrieve_content_type(&'a self) -> Option<T> {
21 self.content_type()
22 .as_ref()
23 .map(|short_string| short_string.coerce())
24 .flatten()
25 }
26}
27
28/// Artificial trait implemented for [`AMQPProperties`] to allow conveniently
29/// extracting the content encoding, coercing it into various types.
30pub trait RetrieveContentEncoding<'a, T> {
31 /// Extracts the content encoding from these [`AMQPProperties`], if it is
32 /// present and can be coerced to type `T`.
33 fn retrieve_content_encoding(&'a self) -> Option<T>;
34}
35
36/// Implements [`RetrieveContentEncoding`] for every type `T` for which the underlying
37/// [`ShortString`] implements [`Coerce`].
38impl<'a, T> RetrieveContentEncoding<'a, T> for AMQPProperties
39where
40 ShortString: Coerce<'a, T>,
41{
42 fn retrieve_content_encoding(&'a self) -> Option<T> {
43 self.content_encoding()
44 .as_ref()
45 .map(|short_string| short_string.coerce())
46 .flatten()
47 }
48}
49
50/// Artificial trait implemented for [`AMQPProperties`] to allow conveniently
51/// extracting the header value by key, coercing it into various types.
52pub trait RetrieveHeader<'a, T> {
53 /// Extracts the header value by key from these [`AMQPProperties`], if it is
54 /// present and can be coerced to type `T`.
55 fn retrieve_header(&'a self, key: &str) -> Option<T>;
56}
57
58/// Implements [`RetrieveHeader`] for every type `T` for which the underlying
59/// [`AMQPValue`] implements [`Coerce`].
60impl<'a, T> RetrieveHeader<'a, T> for AMQPProperties
61where
62 AMQPValue: Coerce<'a, T>,
63{
64 fn retrieve_header(&'a self, key: &str) -> Option<T> {
65 self.headers()
66 .as_ref()
67 .map(|field_table| field_table.retrieve(key))
68 .flatten()
69 }
70}
71
72/// Artificial trait implemented for [`AMQPProperties`] to allow conveniently
73/// extracting the correlation ID, coercing it into various types.
74pub trait RetrieveCorrelationId<'a, T> {
75 /// Extracts the correlation ID from these [`AMQPProperties`], if it is
76 /// present and can be coerced to type `T`.
77 fn retrieve_correlation_id(&'a self) -> Option<T>;
78}
79
80/// Implements [`RetrieveCorrelationId`] for every type `T` for which the underlying
81/// [`AMQPValue`] implements [`Coerce`].
82impl<'a, T> RetrieveCorrelationId<'a, T> for AMQPProperties
83where
84 ShortString: Coerce<'a, T>,
85{
86 fn retrieve_correlation_id(&'a self) -> Option<T> {
87 self.correlation_id()
88 .as_ref()
89 .map(|short_string| short_string.coerce())
90 .flatten()
91 }
92}
93
94/// Artificial trait implemented for [`AMQPProperties`] to allow conveniently
95/// extracting the “reply-to” value, coercing it into various types.
96pub trait RetrieveReplyTo<'a, T> {
97 /// Extracts the “reply-to” value from these [`AMQPProperties`], if it is
98 /// present and can be coerced to type `T`.
99 fn retrieve_reply_to(&'a self) -> Option<T>;
100}
101
102/// Implements [`RetrieveReplyTo`] for every type `T` for which the underlying
103/// [`AMQPValue`] implements [`Coerce`].
104impl<'a, T> RetrieveReplyTo<'a, T> for AMQPProperties
105where
106 ShortString: Coerce<'a, T>,
107{
108 fn retrieve_reply_to(&'a self) -> Option<T> {
109 self.reply_to()
110 .as_ref()
111 .map(|short_string| short_string.coerce())
112 .flatten()
113 }
114}
115
116/// Artificial trait implemented for [`AMQPProperties`] to allow conveniently
117/// extracting the expiration value, coercing it into various types.
118pub trait RetrieveExpiration<'a, T> {
119 /// Extracts the expiration value from these [`AMQPProperties`], if it is
120 /// present and can be coerced to type `T`.
121 fn retrieve_expiration(&'a self) -> Option<T>;
122}
123
124/// Implements [`RetrieveExpiration`] for every type `T` for which the underlying
125/// [`AMQPValue`] implements [`Coerce`].
126impl<'a, T> RetrieveExpiration<'a, T> for AMQPProperties
127where
128 ShortString: Coerce<'a, T>,
129{
130 fn retrieve_expiration(&'a self) -> Option<T> {
131 self.expiration()
132 .as_ref()
133 .map(|short_string| short_string.coerce())
134 .flatten()
135 }
136}
137
138/// Artificial trait implemented for [`AMQPProperties`] to allow conveniently
139/// extracting the message ID, coercing it into various types.
140pub trait RetrieveMessageId<'a, T> {
141 /// Extracts the message ID from these [`AMQPProperties`], if it is present
142 /// and can be coerced to type `T`.
143 fn retrieve_message_id(&'a self) -> Option<T>;
144}
145
146/// Implements [`RetrieveMessageId`] for every type `T` for which the underlying
147/// [`ShortString`] implements [`Coerce`].
148impl<'a, T> RetrieveMessageId<'a, T> for AMQPProperties
149where
150 ShortString: Coerce<'a, T>,
151{
152 fn retrieve_message_id(&'a self) -> Option<T> {
153 self.message_id()
154 .as_ref()
155 .map(|short_string| short_string.coerce())
156 .flatten()
157 }
158}
159
160/// Artificial trait implemented for [`AMQPProperties`] to allow conveniently
161/// extracting the message kind, coercing it into various types.
162pub trait RetrieveKind<'a, T> {
163 /// Extracts the message kind from these [`AMQPProperties`], if it is present
164 /// and can be coerced to type `T`.
165 fn retrieve_kind(&'a self) -> Option<T>;
166}
167
168/// Implements [`RetrieveKind`] for every type `T` for which the underlying
169/// [`ShortString`] implements [`Coerce`].
170impl<'a, T> RetrieveKind<'a, T> for AMQPProperties
171where
172 ShortString: Coerce<'a, T>,
173{
174 fn retrieve_kind(&'a self) -> Option<T> {
175 self.kind()
176 .as_ref()
177 .map(|short_string| short_string.coerce())
178 .flatten()
179 }
180}
181
182/// Artificial trait implemented for [`AMQPProperties`] to allow conveniently
183/// extracting the user ID, coercing it into various types.
184pub trait RetrieveUserId<'a, T> {
185 /// Extracts the user ID from these [`AMQPProperties`], if it is present
186 /// and can be coerced to type `T`.
187 fn retrieve_user_id(&'a self) -> Option<T>;
188}
189
190/// Implements [`RetrieveUserId`] for every type `T` for which the underlying
191/// [`ShortString`] implements [`Coerce`].
192impl<'a, T> RetrieveUserId<'a, T> for AMQPProperties
193where
194 ShortString: Coerce<'a, T>,
195{
196 fn retrieve_user_id(&'a self) -> Option<T> {
197 self.user_id()
198 .as_ref()
199 .map(|short_string| short_string.coerce())
200 .flatten()
201 }
202}
203
204/// Artificial trait implemented for [`AMQPProperties`] to allow conveniently
205/// extracting the app ID, coercing it into various types.
206pub trait RetrieveAppId<'a, T> {
207 /// Extracts the app ID from these [`AMQPProperties`], if it is present
208 /// and can be coerced to type `T`.
209 fn retrieve_app_id(&'a self) -> Option<T>;
210}
211
212/// Implements [`RetrieveAppId`] for every type `T` for which the underlying
213/// [`ShortString`] implements [`Coerce`].
214impl<'a, T> RetrieveAppId<'a, T> for AMQPProperties
215where
216 ShortString: Coerce<'a, T>,
217{
218 fn retrieve_app_id(&'a self) -> Option<T> {
219 self.app_id()
220 .as_ref()
221 .map(|short_string| short_string.coerce())
222 .flatten()
223 }
224}
225
226/// Artificial trait implemented for [`AMQPProperties`] to allow conveniently
227/// extracting the cluster ID, coercing it into various types.
228pub trait RetrieveClusterId<'a, T> {
229 /// Extracts the cluster ID from these [`AMQPProperties`], if it is present
230 /// and can be coerced to type `T`.
231 fn retrieve_cluster_id(&'a self) -> Option<T>;
232}
233
234/// Implements [`RetrieveClusterId`] for every type `T` for which the underlying
235/// [`ShortString`] implements [`Coerce`].
236impl<'a, T> RetrieveClusterId<'a, T> for AMQPProperties
237where
238 ShortString: Coerce<'a, T>,
239{
240 fn retrieve_cluster_id(&'a self) -> Option<T> {
241 self.cluster_id()
242 .as_ref()
243 .map(|short_string| short_string.coerce())
244 .flatten()
245 }
246}