ramqp 0.6.6

Async AMQP 1.0 client for Rust on tokio — connects to RabbitMQ 4.x, ActiveMQ Artemis, and other AMQP 1.0 brokers.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
//! The [`Encode`] trait and its primitive implementations, plus the single-pass
//! composite/list/map/array encoders.
//!
//! Encoding never pre-computes a size by trial serialization. Compound headers
//! reserve a 4-byte length placeholder up front and **backpatch** it once the
//! body bytes are known — one pass over the data, never two.

use bytes::{BufMut, Bytes, BytesMut};
use ordered_float::OrderedFloat;
use uuid::Uuid;

use super::primitives::{OrderedMap, Symbol, codes};

/// A type that can be written to the AMQP 1.0 wire format.
///
/// Implementations write a complete constructor + value (a "typed" encoding),
/// so an `Option<T>` field encodes as an explicit `null` when `None`.
pub trait Encode {
    /// Append the typed encoding of `self` to `buf`.
    fn encode(&self, buf: &mut BytesMut);
}

/// Write a bare `null` (`0x40`).
pub fn encode_null(buf: &mut BytesMut) {
    buf.put_u8(codes::NULL);
}

fn encode_var(buf: &mut BytesMut, code8: u8, code32: u8, bytes: &[u8]) {
    if bytes.len() <= u8::MAX as usize {
        buf.put_u8(code8);
        buf.put_u8(bytes.len() as u8);
    } else {
        buf.put_u8(code32);
        buf.put_u32(bytes.len() as u32);
    }
    buf.put_slice(bytes);
}

// ---------------------------------------------------------------------------
// Primitive impls
// ---------------------------------------------------------------------------

impl Encode for bool {
    fn encode(&self, buf: &mut BytesMut) {
        buf.put_u8(if *self {
            codes::BOOL_TRUE
        } else {
            codes::BOOL_FALSE
        });
    }
}

impl Encode for u8 {
    fn encode(&self, buf: &mut BytesMut) {
        buf.put_u8(codes::UBYTE);
        buf.put_u8(*self);
    }
}

impl Encode for u16 {
    fn encode(&self, buf: &mut BytesMut) {
        buf.put_u8(codes::USHORT);
        buf.put_u16(*self);
    }
}

impl Encode for u32 {
    fn encode(&self, buf: &mut BytesMut) {
        match *self {
            0 => buf.put_u8(codes::UINT_0),
            n if n <= u8::MAX as u32 => {
                buf.put_u8(codes::SMALL_UINT);
                buf.put_u8(n as u8);
            }
            n => {
                buf.put_u8(codes::UINT);
                buf.put_u32(n);
            }
        }
    }
}

impl Encode for u64 {
    fn encode(&self, buf: &mut BytesMut) {
        match *self {
            0 => buf.put_u8(codes::ULONG_0),
            n if n <= u8::MAX as u64 => {
                buf.put_u8(codes::SMALL_ULONG);
                buf.put_u8(n as u8);
            }
            n => {
                buf.put_u8(codes::ULONG);
                buf.put_u64(n);
            }
        }
    }
}

impl Encode for i8 {
    fn encode(&self, buf: &mut BytesMut) {
        buf.put_u8(codes::BYTE);
        buf.put_i8(*self);
    }
}

impl Encode for i16 {
    fn encode(&self, buf: &mut BytesMut) {
        buf.put_u8(codes::SHORT);
        buf.put_i16(*self);
    }
}

impl Encode for i32 {
    fn encode(&self, buf: &mut BytesMut) {
        if (i8::MIN as i32..=i8::MAX as i32).contains(self) {
            buf.put_u8(codes::SMALL_INT);
            buf.put_i8(*self as i8);
        } else {
            buf.put_u8(codes::INT);
            buf.put_i32(*self);
        }
    }
}

impl Encode for i64 {
    fn encode(&self, buf: &mut BytesMut) {
        if (i8::MIN as i64..=i8::MAX as i64).contains(self) {
            buf.put_u8(codes::SMALL_LONG);
            buf.put_i8(*self as i8);
        } else {
            buf.put_u8(codes::LONG);
            buf.put_i64(*self);
        }
    }
}

impl Encode for f32 {
    fn encode(&self, buf: &mut BytesMut) {
        buf.put_u8(codes::FLOAT);
        buf.put_f32(*self);
    }
}

impl Encode for f64 {
    fn encode(&self, buf: &mut BytesMut) {
        buf.put_u8(codes::DOUBLE);
        buf.put_f64(*self);
    }
}

impl Encode for OrderedFloat<f32> {
    fn encode(&self, buf: &mut BytesMut) {
        self.0.encode(buf);
    }
}

impl Encode for OrderedFloat<f64> {
    fn encode(&self, buf: &mut BytesMut) {
        self.0.encode(buf);
    }
}

impl Encode for char {
    fn encode(&self, buf: &mut BytesMut) {
        buf.put_u8(codes::CHAR);
        buf.put_u32(*self as u32);
    }
}

impl Encode for Uuid {
    fn encode(&self, buf: &mut BytesMut) {
        buf.put_u8(codes::UUID);
        buf.put_slice(self.as_bytes());
    }
}

impl Encode for str {
    fn encode(&self, buf: &mut BytesMut) {
        encode_var(buf, codes::STR8, codes::STR32, self.as_bytes());
    }
}

impl Encode for String {
    fn encode(&self, buf: &mut BytesMut) {
        self.as_str().encode(buf);
    }
}

impl Encode for Symbol {
    fn encode(&self, buf: &mut BytesMut) {
        encode_var(buf, codes::SYM8, codes::SYM32, self.0.as_bytes());
    }
}

impl Encode for [u8] {
    fn encode(&self, buf: &mut BytesMut) {
        encode_var(buf, codes::VBIN8, codes::VBIN32, self);
    }
}

impl Encode for Bytes {
    fn encode(&self, buf: &mut BytesMut) {
        encode_var(buf, codes::VBIN8, codes::VBIN32, self);
    }
}

impl<T: Encode + ?Sized> Encode for &T {
    fn encode(&self, buf: &mut BytesMut) {
        (**self).encode(buf);
    }
}

impl<T: Encode + ?Sized> Encode for Box<T> {
    fn encode(&self, buf: &mut BytesMut) {
        (**self).encode(buf);
    }
}

impl<T: Encode> Encode for Option<T> {
    fn encode(&self, buf: &mut BytesMut) {
        match self {
            Some(v) => v.encode(buf),
            None => encode_null(buf),
        }
    }
}

impl<K: Encode, V: Encode> Encode for OrderedMap<K, V> {
    fn encode(&self, buf: &mut BytesMut) {
        encode_map_entries(buf, self.iter());
    }
}

// ---------------------------------------------------------------------------
// Compound encoders
// ---------------------------------------------------------------------------

/// Write a descriptor as a `smallulong` when it fits, otherwise a `ulong`.
pub fn encode_descriptor(buf: &mut BytesMut, code: u64) {
    if code <= u8::MAX as u64 {
        buf.put_u8(codes::SMALL_ULONG);
        buf.put_u8(code as u8);
    } else {
        buf.put_u8(codes::ULONG);
        buf.put_u64(code);
    }
}

/// Records the start offset of each encoded field so [`encode_described_list`]
/// can elide trailing `null`s in a single pass.
#[derive(Debug)]
pub struct FieldWriter<'a> {
    buf: &'a mut BytesMut,
    starts: Vec<usize>,
}

impl FieldWriter<'_> {
    /// Encode one field.
    pub fn field<E: Encode + ?Sized>(&mut self, value: &E) {
        self.starts.push(self.buf.len());
        value.encode(self.buf);
    }

    /// Encode an explicit `null` field.
    pub fn null(&mut self) {
        self.starts.push(self.buf.len());
        encode_null(self.buf);
    }

    /// Encode a `multiple` symbol field: omitted when empty, an array of symbol
    /// otherwise (always the 32-bit element width so the array is homogeneous).
    pub fn symbols(&mut self, syms: &[Symbol]) {
        self.starts.push(self.buf.len());
        if syms.is_empty() {
            encode_null(self.buf);
        } else {
            encode_symbol_array(self.buf, syms);
        }
    }

    /// Encode a mandatory `multiple` symbol field: always present as an array
    /// (never elided to `null`, even when empty).
    pub fn symbols_required(&mut self, syms: &[Symbol]) {
        self.starts.push(self.buf.len());
        encode_symbol_array(self.buf, syms);
    }
}

/// Encode a described list (the composite-type workhorse): `0x00`, descriptor,
/// then a `list32` of the fields with trailing `null`s elided.
pub fn encode_described_list(
    buf: &mut BytesMut,
    descriptor: u64,
    fields: impl FnOnce(&mut FieldWriter),
) {
    buf.put_u8(codes::DESCRIBED);
    encode_descriptor(buf, descriptor);
    buf.put_u8(codes::LIST32);
    let size_pos = buf.len();
    buf.put_u32(0);
    let count_pos = buf.len();
    buf.put_u32(0);
    let content_start = buf.len();

    let mut fw = FieldWriter {
        buf,
        starts: Vec::new(),
    };
    fields(&mut fw);
    let FieldWriter { buf, starts } = fw;

    // Elide trailing fields encoded as a bare `null` (single 0x40 byte).
    let total = starts.len();
    let mut count = total;
    while count > 0 {
        let start = starts[count - 1];
        let end = if count < total {
            starts[count]
        } else {
            buf.len()
        };
        if end - start == 1 && buf[start] == codes::NULL {
            count -= 1;
        } else {
            break;
        }
    }
    let new_len = if count == total {
        buf.len()
    } else if count == 0 {
        content_start
    } else {
        starts[count]
    };
    buf.truncate(new_len);

    let size = (buf.len() - content_start + 4) as u32;
    buf[size_pos..size_pos + 4].copy_from_slice(&size.to_be_bytes());
    buf[count_pos..count_pos + 4].copy_from_slice(&(count as u32).to_be_bytes());
}

/// Encode a `map32` from an iterator of borrowed key/value pairs.
pub fn encode_map_entries<'a, K, V, I>(buf: &mut BytesMut, entries: I)
where
    K: Encode + 'a,
    V: Encode + 'a,
    I: IntoIterator<Item = (&'a K, &'a V)>,
{
    buf.put_u8(codes::MAP32);
    let size_pos = buf.len();
    buf.put_u32(0);
    let count_pos = buf.len();
    buf.put_u32(0);
    let content_start = buf.len();

    let mut count = 0u32;
    for (k, v) in entries {
        k.encode(buf);
        v.encode(buf);
        count += 2;
    }

    let size = (buf.len() - content_start + 4) as u32;
    buf[size_pos..size_pos + 4].copy_from_slice(&size.to_be_bytes());
    buf[count_pos..count_pos + 4].copy_from_slice(&count.to_be_bytes());
}

/// Encode a homogeneous `array32` of symbols (shared `sym32` constructor).
pub fn encode_symbol_array(buf: &mut BytesMut, syms: &[Symbol]) {
    buf.put_u8(codes::ARRAY32);
    let size_pos = buf.len();
    buf.put_u32(0);
    let count_pos = buf.len();
    buf.put_u32(0);
    let content_start = buf.len();

    buf.put_u8(codes::SYM32); // shared element constructor
    for s in syms {
        buf.put_u32(s.0.len() as u32);
        buf.put_slice(s.0.as_bytes());
    }

    let size = (buf.len() - content_start + 4) as u32;
    buf[size_pos..size_pos + 4].copy_from_slice(&size.to_be_bytes());
    buf[count_pos..count_pos + 4].copy_from_slice(&(syms.len() as u32).to_be_bytes());
}

/// Reserve a `list32`/`map32`-style 4-byte length + count header, returning the
/// positions to backpatch. Used by [`crate::codec::value`] for dynamic arrays.
pub(crate) fn open_compound(buf: &mut BytesMut, code: u8) -> (usize, usize, usize) {
    buf.put_u8(code);
    let size_pos = buf.len();
    buf.put_u32(0);
    let count_pos = buf.len();
    buf.put_u32(0);
    let content_start = buf.len();
    (size_pos, count_pos, content_start)
}

/// Backpatch a header opened by [`open_compound`].
pub(crate) fn close_compound(
    buf: &mut BytesMut,
    size_pos: usize,
    count_pos: usize,
    content_start: usize,
    count: u32,
) {
    let size = (buf.len() - content_start + 4) as u32;
    buf[size_pos..size_pos + 4].copy_from_slice(&size.to_be_bytes());
    buf[count_pos..count_pos + 4].copy_from_slice(&count.to_be_bytes());
}