mqtt-protocol-core 0.7.7

A Sans-I/O style MQTT protocol library for Rust that supports both MQTT v5.0 and v3.1.1.
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
// MIT License
//
// Copyright (c) 2025 Takatoshi Kondo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

use crate::mqtt::Arc;
use alloc::{string::String, vec::Vec};
use serde::{Serialize, Serializer};

// SSO buffer size configuration - priority-based selection for maximum size
#[cfg(feature = "sso-lv20")]
const SSO_BUFFER_SIZE: usize = 255; // Highest priority: 255 bytes
#[cfg(all(not(feature = "sso-lv20"), feature = "sso-lv10"))]
const SSO_BUFFER_SIZE: usize = 127; // Second priority: 127 bytes
#[cfg(all(
    not(any(feature = "sso-lv20", feature = "sso-lv10")),
    feature = "sso-min-64bit"
))]
const SSO_BUFFER_SIZE: usize = 31; // Third priority: 31 bytes
#[cfg(all(
    not(any(feature = "sso-lv20", feature = "sso-lv10", feature = "sso-min-64bit")),
    feature = "sso-min-32bit"
))]
const SSO_BUFFER_SIZE: usize = 15; // Fourth priority: 15 bytes
#[cfg(not(any(
    feature = "sso-min-32bit",
    feature = "sso-min-64bit",
    feature = "sso-lv10",
    feature = "sso-lv20"
)))]
#[allow(dead_code)]
const SSO_BUFFER_SIZE: usize = 0; // No SSO features enabled

// Length type is always u8 since all buffer sizes fit in u8 range (max 255)
#[cfg(any(
    feature = "sso-min-32bit",
    feature = "sso-min-64bit",
    feature = "sso-lv10",
    feature = "sso-lv20"
))]
type LengthType = u8;

#[cfg(not(any(
    feature = "sso-min-32bit",
    feature = "sso-min-64bit",
    feature = "sso-lv10",
    feature = "sso-lv20"
)))]
#[allow(dead_code)]
type LengthType = u8;

/// A reference-counted byte payload with slice semantics
///
/// `ArcPayload` provides an efficient way to handle byte data by using `Arc<[u8]>`
/// for reference counting, combined with offset and length information to represent
/// a slice view of the underlying data. This allows for zero-copy sharing of payload
/// data across multiple consumers while maintaining slice-like semantics.
#[derive(Clone)]
#[allow(clippy::large_enum_variant)]
pub enum ArcPayload {
    #[cfg(any(
        feature = "sso-min-32bit",
        feature = "sso-min-64bit",
        feature = "sso-lv10",
        feature = "sso-lv20"
    ))]
    Small([u8; SSO_BUFFER_SIZE], LengthType), // buffer, actual_length
    Large {
        data: Arc<[u8]>,
        start: usize,
        length: usize,
    },
}

impl PartialEq for ArcPayload {
    fn eq(&self, other: &Self) -> bool {
        self.as_slice() == other.as_slice()
    }
}

impl Eq for ArcPayload {}

impl Serialize for ArcPayload {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        self.as_slice().serialize(serializer)
    }
}

impl ArcPayload {
    /// Create a new `ArcPayload` from reference-counted data with specified range
    ///
    /// Creates a new payload that represents a slice view of the provided `Arc<[u8]>` data
    /// starting at the specified offset with the given length.
    ///
    /// # Parameters
    ///
    /// * `data` - The reference-counted byte data
    /// * `start` - The starting offset within the data
    /// * `length` - The length of the payload slice
    ///
    /// # Panics
    ///
    /// Panics in debug mode if `start + length > data.len()` (payload out of bounds)
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use alloc::sync::Arc;
    /// use mqtt_protocol_core::mqtt::ArcPayload;
    ///
    /// let data = Arc::from(&b"hello world"[..]);
    /// let payload = ArcPayload::new(data, 0, 5); // "hello"
    /// ```
    pub fn new(data: Arc<[u8]>, start: usize, length: usize) -> Self {
        debug_assert!(start + length <= data.len(), "payload out of bounds",);

        #[cfg(any(
            feature = "sso-min-32bit",
            feature = "sso-min-64bit",
            feature = "sso-lv10",
            feature = "sso-lv20"
        ))]
        if length <= SSO_BUFFER_SIZE {
            let mut buffer = [0u8; SSO_BUFFER_SIZE];
            buffer[..length].copy_from_slice(&data[start..start + length]);
            return Self::Small(buffer, length as LengthType);
        }

        Self::Large {
            data,
            start,
            length,
        }
    }

    /// Get a slice view of the payload data
    ///
    /// Returns a byte slice representing the payload data within the specified range.
    ///
    /// # Returns
    ///
    /// A `&[u8]` slice of the payload data
    pub fn as_slice(&self) -> &[u8] {
        match self {
            ArcPayload::Large {
                data,
                start,
                length,
            } => &data[*start..*start + *length],
            #[cfg(any(
                feature = "sso-min-32bit",
                feature = "sso-min-64bit",
                feature = "sso-lv10",
                feature = "sso-lv20"
            ))]
            ArcPayload::Small(buffer, length) => &buffer[..*length as usize],
        }
    }

    /// Get the length of the payload
    ///
    /// Returns the number of bytes in the payload slice.
    ///
    /// # Returns
    ///
    /// The length of the payload in bytes
    pub fn len(&self) -> usize {
        match self {
            ArcPayload::Large { length, .. } => *length,
            #[cfg(any(
                feature = "sso-min-32bit",
                feature = "sso-min-64bit",
                feature = "sso-lv10",
                feature = "sso-lv20"
            ))]
            ArcPayload::Small(_, length) => *length as usize,
        }
    }

    /// Check if the payload is empty
    ///
    /// Returns `true` if the payload contains no bytes.
    ///
    /// # Returns
    ///
    /// `true` if the payload length is zero, `false` otherwise
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Get a reference to the underlying `Arc<[u8]>` data
    ///
    /// Returns a reference to the reference-counted byte array that contains
    /// the actual data. This provides access to the full underlying data,
    /// not just the slice view represented by this payload.
    ///
    /// # Returns
    ///
    /// A reference to the underlying `Arc<[u8]>` data
    pub fn arc_data(&self) -> Option<&Arc<[u8]>> {
        match self {
            ArcPayload::Large { data, .. } => Some(data),
            #[cfg(any(
                feature = "sso-min-32bit",
                feature = "sso-min-64bit",
                feature = "sso-lv10",
                feature = "sso-lv20"
            ))]
            ArcPayload::Small(_, _) => None, // Small variant doesn't use Arc data
        }
    }
}

impl Default for ArcPayload {
    fn default() -> Self {
        #[cfg(any(
            feature = "sso-min-32bit",
            feature = "sso-min-64bit",
            feature = "sso-lv10",
            feature = "sso-lv20"
        ))]
        return ArcPayload::Small([0u8; SSO_BUFFER_SIZE], 0 as LengthType);

        #[cfg(not(any(
            feature = "sso-min-32bit",
            feature = "sso-min-64bit",
            feature = "sso-lv10",
            feature = "sso-lv20"
        )))]
        return ArcPayload::Large {
            data: Arc::from(&[] as &[u8]),
            start: 0,
            length: 0,
        };
    }
}

impl core::fmt::Debug for ArcPayload {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("ArcPayload")
            .field("data", &self.as_slice())
            .field("len", &self.len())
            .finish()
    }
}

/// Trait for converting various types into `ArcPayload`
///
/// This trait provides a uniform interface for converting different data types
/// into `ArcPayload` instances. It allows for convenient creation of payloads
/// from common types like strings, byte slices, vectors, and arrays.
pub trait IntoPayload {
    /// Convert the value into an `ArcPayload`
    ///
    /// # Returns
    ///
    /// An `ArcPayload` containing the converted data
    fn into_payload(self) -> ArcPayload;
}

/// Convert a string slice (`&str`) into an `ArcPayload`
impl IntoPayload for &str {
    fn into_payload(self) -> ArcPayload {
        let bytes = self.as_bytes();

        #[cfg(any(
            feature = "sso-min-32bit",
            feature = "sso-min-64bit",
            feature = "sso-lv10",
            feature = "sso-lv20"
        ))]
        if bytes.len() <= SSO_BUFFER_SIZE {
            let mut buffer = [0u8; SSO_BUFFER_SIZE];
            buffer[..bytes.len()].copy_from_slice(bytes);
            return ArcPayload::Small(buffer, bytes.len() as LengthType);
        }

        ArcPayload::Large {
            data: Arc::from(bytes),
            start: 0,
            length: bytes.len(),
        }
    }
}

/// Convert an owned string (`String`) into an `ArcPayload`
impl IntoPayload for String {
    fn into_payload(self) -> ArcPayload {
        let bytes = self.as_bytes();

        #[cfg(any(
            feature = "sso-min-32bit",
            feature = "sso-min-64bit",
            feature = "sso-lv10",
            feature = "sso-lv20"
        ))]
        if bytes.len() <= SSO_BUFFER_SIZE {
            let mut buffer = [0u8; SSO_BUFFER_SIZE];
            buffer[..bytes.len()].copy_from_slice(bytes);
            return ArcPayload::Small(buffer, bytes.len() as LengthType);
        }

        ArcPayload::Large {
            data: Arc::from(bytes),
            start: 0,
            length: bytes.len(),
        }
    }
}

/// Convert a byte slice (`&[u8]`) into an `ArcPayload`
impl IntoPayload for &[u8] {
    fn into_payload(self) -> ArcPayload {
        #[cfg(any(
            feature = "sso-min-32bit",
            feature = "sso-min-64bit",
            feature = "sso-lv10",
            feature = "sso-lv20"
        ))]
        if self.len() <= SSO_BUFFER_SIZE {
            let mut buffer = [0u8; SSO_BUFFER_SIZE];
            buffer[..self.len()].copy_from_slice(self);
            return ArcPayload::Small(buffer, self.len() as LengthType);
        }

        ArcPayload::Large {
            data: Arc::from(self),
            start: 0,
            length: self.len(),
        }
    }
}

/// Convert an owned byte vector (`Vec<u8>`) into an `ArcPayload`
impl IntoPayload for Vec<u8> {
    fn into_payload(self) -> ArcPayload {
        #[cfg(any(
            feature = "sso-min-32bit",
            feature = "sso-min-64bit",
            feature = "sso-lv10",
            feature = "sso-lv20"
        ))]
        if self.len() <= SSO_BUFFER_SIZE {
            let mut buffer = [0u8; SSO_BUFFER_SIZE];
            buffer[..self.len()].copy_from_slice(&self);
            return ArcPayload::Small(buffer, self.len() as LengthType);
        }

        let len = self.len();
        ArcPayload::Large {
            data: Arc::from(self),
            start: 0,
            length: len,
        }
    }
}

/// Convert a reference to a byte vector (`&Vec<u8>`) into an `ArcPayload`
impl IntoPayload for &Vec<u8> {
    fn into_payload(self) -> ArcPayload {
        let slice: &[u8] = self.as_slice();

        #[cfg(any(
            feature = "sso-min-32bit",
            feature = "sso-min-64bit",
            feature = "sso-lv10",
            feature = "sso-lv20"
        ))]
        if slice.len() <= SSO_BUFFER_SIZE {
            let mut buffer = [0u8; SSO_BUFFER_SIZE];
            buffer[..slice.len()].copy_from_slice(slice);
            return ArcPayload::Small(buffer, slice.len() as LengthType);
        }

        ArcPayload::Large {
            data: Arc::from(slice),
            start: 0,
            length: slice.len(),
        }
    }
}

/// Convert a reference to a byte array (`&[u8; N]`) into an `ArcPayload`
impl<const N: usize> IntoPayload for &[u8; N] {
    fn into_payload(self) -> ArcPayload {
        let slice: &[u8] = self.as_slice();

        #[cfg(any(
            feature = "sso-min-32bit",
            feature = "sso-min-64bit",
            feature = "sso-lv10",
            feature = "sso-lv20"
        ))]
        if slice.len() <= SSO_BUFFER_SIZE {
            let mut buffer = [0u8; SSO_BUFFER_SIZE];
            buffer[..slice.len()].copy_from_slice(slice);
            return ArcPayload::Small(buffer, slice.len() as LengthType);
        }

        ArcPayload::Large {
            data: Arc::from(slice),
            start: 0,
            length: slice.len(),
        }
    }
}

/// Convert an `Arc<[u8]>` directly into an `ArcPayload`
impl IntoPayload for Arc<[u8]> {
    fn into_payload(self) -> ArcPayload {
        let len = self.len();

        #[cfg(any(
            feature = "sso-min-32bit",
            feature = "sso-min-64bit",
            feature = "sso-lv10",
            feature = "sso-lv20"
        ))]
        if len <= SSO_BUFFER_SIZE {
            let mut buffer = [0u8; SSO_BUFFER_SIZE];
            buffer[..len].copy_from_slice(&self);
            return ArcPayload::Small(buffer, len as LengthType);
        }

        ArcPayload::Large {
            data: self,
            start: 0,
            length: len,
        }
    }
}

/// Convert unit type (`()`) into an empty `ArcPayload`
impl IntoPayload for () {
    fn into_payload(self) -> ArcPayload {
        ArcPayload::default() // Empty payload
    }
}

/// Identity conversion for `ArcPayload` (no-op)
impl IntoPayload for ArcPayload {
    fn into_payload(self) -> ArcPayload {
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_empty_payload() {
        let payload = ArcPayload::default();
        assert_eq!(payload.len(), 0);
        assert!(payload.is_empty());
        assert_eq!(payload.as_slice(), &[] as &[u8]);
    }

    #[test]
    fn test_small_payload() {
        let data = b"hello";
        let payload = data.into_payload();
        assert_eq!(payload.len(), 5);
        assert!(!payload.is_empty());
        assert_eq!(payload.as_slice(), b"hello");
    }

    #[test]
    fn test_payload_variants() {
        // Test small data (Small variant if any SSO feature is enabled)
        let small_data = b"small";
        let payload = small_data.into_payload();

        #[cfg(any(
            feature = "sso-min-32bit",
            feature = "sso-min-64bit",
            feature = "sso-lv10",
            feature = "sso-lv20"
        ))]
        assert!(matches!(payload, ArcPayload::Small(_, _)));

        #[cfg(not(any(
            feature = "sso-min-32bit",
            feature = "sso-min-64bit",
            feature = "sso-lv10",
            feature = "sso-lv20"
        )))]
        assert!(matches!(payload, ArcPayload::Large { .. }));

        // Test data that should be Large if using smaller SSO buffers, but Small with sso-lv20
        let medium_data = vec![0u8; 200]; // 200 bytes - larger than most SSO buffers but smaller than sso-lv20
        let _payload = medium_data.into_payload();

        // With sso-lv20 (255 bytes), this should be Small
        #[cfg(feature = "sso-lv20")]
        assert!(matches!(_payload, ArcPayload::Small(_, _)));

        // Without sso-lv20, this should be Large (exceeds other SSO buffer sizes)
        #[cfg(all(
            any(
                feature = "sso-min-32bit",
                feature = "sso-min-64bit",
                feature = "sso-lv10"
            ),
            not(feature = "sso-lv20")
        ))]
        assert!(matches!(_payload, ArcPayload::Large { .. }));

        // Test data that should always be Large variant (larger than largest SSO buffer)
        let very_large_data = b"This is a very long payload that exceeds even the largest SSO buffer size of 255 bytes. It should definitely be stored in the Large variant regardless of which SSO feature flags are enabled. This ensures consistent behavior across all configurations and provides a reliable test case.";
        let payload = very_large_data.into_payload();
        assert!(matches!(payload, ArcPayload::Large { .. }));
    }

    #[test]
    fn test_arc_data_access() {
        let small_data = b"test";
        let small_payload = small_data.into_payload();

        // Use data larger than largest SSO buffer to ensure Large variant
        let very_large_data = b"This is a very long payload that exceeds even the largest SSO buffer size of 255 bytes. It should definitely be stored in the Large variant regardless of which SSO feature flags are enabled. This ensures consistent behavior across all configurations and provides a reliable test case for arc_data access.";
        let large_payload = very_large_data.into_payload();

        // Small variant should return None for arc_data when SSO is enabled
        #[cfg(any(
            feature = "sso-min-32bit",
            feature = "sso-min-64bit",
            feature = "sso-lv10",
            feature = "sso-lv20"
        ))]
        if let ArcPayload::Small(_, _) = small_payload {
            assert!(small_payload.arc_data().is_none());
        }

        // Without SSO, small data also uses Large variant
        #[cfg(not(any(
            feature = "sso-min-32bit",
            feature = "sso-min-64bit",
            feature = "sso-lv10",
            feature = "sso-lv20"
        )))]
        assert!(small_payload.arc_data().is_some());

        // Large variant should always return Some for arc_data
        assert!(large_payload.arc_data().is_some());
    }

    #[test]
    fn test_into_payload_implementations() {
        // Test various types
        let str_payload = "hello".into_payload();
        assert_eq!(str_payload.as_slice(), b"hello");

        let string_payload = String::from("world").into_payload();
        assert_eq!(string_payload.as_slice(), b"world");

        let vec_payload = vec![1, 2, 3, 4].into_payload();
        assert_eq!(vec_payload.as_slice(), &[1, 2, 3, 4]);

        let arr_payload = (&[5, 6, 7, 8]).into_payload();
        assert_eq!(arr_payload.as_slice(), &[5, 6, 7, 8]);

        let unit_payload = ().into_payload();
        assert!(unit_payload.is_empty());
    }
}