strut_rabbitmq/util/amqp_properties/push.rs
1use crate::util::field_table::push::Push;
2use crate::util::morph::Morph;
3use lapin::protocol::basic::AMQPProperties;
4use lapin::types::{AMQPValue, FieldTable, ShortString};
5
6/// Artificial trait implemented for [`AMQPProperties`] to allow conveniently
7/// inserting the content type, coercing it from various types.
8pub trait PushContentType<T> {
9 /// Inserts the content type into these [`AMQPProperties`], if it can be
10 /// coerced from type `T`.
11 fn push_content_type(self, value: T) -> Self;
12}
13
14/// Implements [`PushContentType`] for every type `T` for which the underlying
15/// [`ShortString`] implements [`Morph`].
16impl<T> PushContentType<T> for AMQPProperties
17where
18 ShortString: Morph<T>,
19{
20 fn push_content_type(self, value: T) -> Self {
21 self.with_content_type(ShortString::morph(value))
22 }
23}
24
25/// Artificial trait implemented for [`AMQPProperties`] to allow conveniently
26/// inserting the content encoding, coercing it from various types.
27pub trait PushContentEncoding<T> {
28 /// Inserts the content encoding into these [`AMQPProperties`], if it can
29 /// be coerced from type `T`.
30 fn push_content_encoding(self, value: T) -> Self;
31}
32
33/// Implements [`PushContentEncoding`] for every type `T` for which the underlying
34/// [`ShortString`] implements [`Morph`].
35impl<T> PushContentEncoding<T> for AMQPProperties
36where
37 ShortString: Morph<T>,
38{
39 fn push_content_encoding(self, value: T) -> Self {
40 self.with_content_encoding(ShortString::morph(value))
41 }
42}
43
44/// Artificial trait implemented for [`AMQPProperties`] to allow conveniently
45/// inserting the header value by key, coercing it from various types.
46pub trait PushHeader<T> {
47 /// Inserts the header value by key into these [`AMQPProperties`], if it
48 /// can be coerced from type `T`.
49 ///
50 /// It is **not** recommended to use this for building up headers one
51 /// key-value at a time, because this implementation clones the underlying
52 /// [`FieldTable`] every time. Better to build a field table externally and
53 /// then set it with a single call to
54 /// [`with_headers`](AMQPProperties::with_headers).
55 fn push_header(self, key: &str, value: T) -> Self;
56}
57
58/// Implements [`PushHeader`] for every type `T` for which the underlying
59/// [`AMQPValue`] implements [`Morph`].
60impl<T> PushHeader<T> for AMQPProperties
61where
62 AMQPValue: Morph<T>,
63{
64 fn push_header(self, key: &str, value: T) -> Self {
65 // We can only clone existing headers to add more
66 let mut new_headers = if let Some(headers) = self.headers() {
67 headers.clone()
68 } else {
69 FieldTable::default()
70 };
71
72 // Push new header
73 new_headers.push(key, value);
74
75 self.with_headers(new_headers)
76 }
77}
78
79/// Artificial trait implemented for [`AMQPProperties`] to allow conveniently
80/// inserting the correlation ID, coercing it from various types.
81pub trait PushCorrelationId<T> {
82 /// Inserts the correlation ID into these [`AMQPProperties`], if it can be
83 /// coerced from type `T`.
84 fn push_correlation_id(self, value: T) -> Self;
85}
86
87/// Implements [`PushCorrelationId`] for every type `T` for which the underlying
88/// [`AMQPValue`] implements [`Morph`].
89impl<T> PushCorrelationId<T> for AMQPProperties
90where
91 ShortString: Morph<T>,
92{
93 fn push_correlation_id(self, value: T) -> Self {
94 self.with_correlation_id(ShortString::morph(value))
95 }
96}
97
98/// Artificial trait implemented for [`AMQPProperties`] to allow conveniently
99/// inserting the “reply-to” value, coercing it from various types.
100pub trait PushReplyTo<T> {
101 /// Inserts the “reply-to” value into these [`AMQPProperties`], if it can
102 /// be coerced from type `T`.
103 fn push_reply_to(self, value: T) -> Self;
104}
105
106/// Implements [`PushReplyTo`] for every type `T` for which the underlying
107/// [`AMQPValue`] implements [`Morph`].
108impl<T> PushReplyTo<T> for AMQPProperties
109where
110 ShortString: Morph<T>,
111{
112 fn push_reply_to(self, value: T) -> Self {
113 self.with_reply_to(ShortString::morph(value))
114 }
115}
116
117/// Artificial trait implemented for [`AMQPProperties`] to allow conveniently
118/// inserting the expiration value, coercing it from various types.
119pub trait PushExpiration<T> {
120 /// Inserts the expiration value into these [`AMQPProperties`], if it can
121 /// be coerced from type `T`.
122 fn push_expiration(self, value: T) -> Self;
123}
124
125/// Implements [`PushExpiration`] for every type `T` for which the underlying
126/// [`AMQPValue`] implements [`Morph`].
127impl<T> PushExpiration<T> for AMQPProperties
128where
129 ShortString: Morph<T>,
130{
131 fn push_expiration(self, value: T) -> Self {
132 self.with_expiration(ShortString::morph(value))
133 }
134}
135
136/// Artificial trait implemented for [`AMQPProperties`] to allow conveniently
137/// inserting the message ID, coercing it from various types.
138pub trait PushMessageId<T> {
139 /// Inserts the message ID into these [`AMQPProperties`], if it can be
140 /// coerced from type `T`.
141 fn push_message_id(self, value: T) -> Self;
142}
143
144/// Implements [`PushMessageId`] for every type `T` for which the underlying
145/// [`ShortString`] implements [`Morph`].
146impl<T> PushMessageId<T> for AMQPProperties
147where
148 ShortString: Morph<T>,
149{
150 fn push_message_id(self, value: T) -> Self {
151 self.with_message_id(ShortString::morph(value))
152 }
153}
154
155/// Artificial trait implemented for [`AMQPProperties`] to allow conveniently
156/// inserting the message kind, coercing it from various types.
157pub trait PushKind<T> {
158 /// Inserts the message kind into these [`AMQPProperties`], if it can be
159 /// coerced from type `T`.
160 fn push_kind(self, value: T) -> Self;
161}
162
163/// Implements [`PushKind`] for every type `T` for which the underlying
164/// [`ShortString`] implements [`Morph`].
165impl<T> PushKind<T> for AMQPProperties
166where
167 ShortString: Morph<T>,
168{
169 fn push_kind(self, value: T) -> Self {
170 self.with_type(ShortString::morph(value))
171 }
172}
173
174/// Artificial trait implemented for [`AMQPProperties`] to allow conveniently
175/// inserting the user ID, coercing it from various types.
176pub trait PushUserId<T> {
177 /// Inserts the user ID into these [`AMQPProperties`], if it is can be
178 /// coerced from type `T`.
179 fn push_user_id(self, value: T) -> Self;
180}
181
182/// Implements [`PushUserId`] for every type `T` for which the underlying
183/// [`ShortString`] implements [`Morph`].
184impl<T> PushUserId<T> for AMQPProperties
185where
186 ShortString: Morph<T>,
187{
188 fn push_user_id(self, value: T) -> Self {
189 self.with_user_id(ShortString::morph(value))
190 }
191}
192
193/// Artificial trait implemented for [`AMQPProperties`] to allow conveniently
194/// inserting the app ID, coercing it from various types.
195pub trait PushAppId<T> {
196 /// Inserts the app ID into these [`AMQPProperties`], if it can be coerced
197 /// from type `T`.
198 fn push_app_id(self, value: T) -> Self;
199}
200
201/// Implements [`PushAppId`] for every type `T` for which the underlying
202/// [`ShortString`] implements [`Morph`].
203impl<T> PushAppId<T> for AMQPProperties
204where
205 ShortString: Morph<T>,
206{
207 fn push_app_id(self, value: T) -> Self {
208 self.with_app_id(ShortString::morph(value))
209 }
210}
211
212/// Artificial trait implemented for [`AMQPProperties`] to allow conveniently
213/// inserting the cluster ID, coercing it from various types.
214pub trait PushClusterId<T> {
215 /// Inserts the cluster ID into these [`AMQPProperties`], if it can be
216 /// coerced from type `T`.
217 fn push_cluster_id(self, value: T) -> Self;
218}
219
220/// Implements [`PushClusterId`] for every type `T` for which the underlying
221/// [`ShortString`] implements [`Morph`].
222impl<T> PushClusterId<T> for AMQPProperties
223where
224 ShortString: Morph<T>,
225{
226 fn push_cluster_id(self, value: T) -> Self {
227 self.with_cluster_id(ShortString::morph(value))
228 }
229}