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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
// 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::result_code::MqttError;
use alloc::string::String;
use alloc::vec::Vec;
use serde::{Serialize, Serializer};
#[cfg(feature = "std")]
use std::io::IoSlice;

// SSO buffer size configuration - priority-based selection for maximum size
#[cfg(feature = "sso-lv20")]
const SSO_BUFFER_SIZE: usize = 48; // Highest priority: 48 bytes
#[cfg(all(
    not(feature = "sso-lv20"),
    any(feature = "sso-lv10", feature = "sso-min-64bit")
))]
const SSO_BUFFER_SIZE: usize = 24; // Second priority: 24 bytes
#[cfg(all(
    not(any(feature = "sso-lv20", feature = "sso-lv10", feature = "sso-min-64bit")),
    feature = "sso-min-32bit"
))]
const SSO_BUFFER_SIZE: usize = 12; // Third priority: 12 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

// Determine data threshold
#[cfg(any(
    feature = "sso-min-32bit",
    feature = "sso-min-64bit",
    feature = "sso-lv10",
    feature = "sso-lv20"
))]
const SSO_DATA_THRESHOLD: usize = SSO_BUFFER_SIZE - 2;

/// MQTT String representation with pre-encoded byte buffer
///
/// This struct represents UTF-8 strings as specified in the MQTT protocol specification.
/// It efficiently stores string data with a 2-byte length prefix, following the MQTT
/// wire format for string fields.
///
/// The string data is stored in a pre-encoded format internally, which includes:
/// - 2 bytes for the length prefix (big-endian u16)
/// - The UTF-8 encoded string bytes
///
/// This approach provides several benefits:
/// - Zero-copy serialization to network buffers
/// - Efficient memory usage with single allocation
/// - Guaranteed MQTT protocol compliance
/// - Fast size calculations
/// - UTF-8 validation at construction time
///
/// # Size Limits
///
/// The maximum size of string data is 65,535 bytes (2^16 - 1), as specified
/// by the MQTT protocol. Attempting to create an `MqttString` with larger
/// data will result in an error.
///
/// # UTF-8 Validation
///
/// All string data is validated to be valid UTF-8 at construction time.
/// Once created, the string is guaranteed to be valid UTF-8, allowing
/// for safe conversion to `&str` without runtime checks.
///
/// # Examples
///
/// ```ignore
/// use mqtt_protocol_core::mqtt;
///
/// // Create from string literal
/// let mqtt_str = mqtt::packet::MqttString::new("hello world").unwrap();
///
/// // Access the string content
/// assert_eq!(mqtt_str.as_str(), "hello world");
/// assert_eq!(mqtt_str.len(), 11);
///
/// // Get the complete encoded buffer (length prefix + UTF-8 bytes)
/// let encoded = mqtt_str.as_bytes();
/// assert_eq!(encoded.len(), 13); // 2 bytes length + 11 bytes data
///
/// // String operations
/// assert!(mqtt_str.contains('w'));
/// assert!(mqtt_str.starts_with("hello"));
/// ```
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
#[allow(clippy::large_enum_variant)]
pub enum MqttString {
    #[cfg(any(
        feature = "sso-min-32bit",
        feature = "sso-min-64bit",
        feature = "sso-lv10",
        feature = "sso-lv20"
    ))]
    Small([u8; SSO_BUFFER_SIZE]),
    Large(Vec<u8>),
}

impl MqttString {
    /// Create a new MqttString from a string
    ///
    /// Creates an `MqttString` instance from the provided string data.
    /// The string is converted to UTF-8 bytes and stored with a 2-byte length prefix.
    ///
    /// # Parameters
    ///
    /// * `s` - String data to store. Can be any type that implements `AsRef<str>`
    ///   such as `&str`, `String`, or `Cow<str>`
    ///
    /// # Returns
    ///
    /// * `Ok(MqttString)` - Successfully created string
    /// * `Err(MqttError::MalformedPacket)` - If string length exceeds 65,535 bytes
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use mqtt_protocol_core::mqtt;
    ///
    /// // From string literal
    /// let mqtt_str = mqtt::packet::MqttString::new("hello").unwrap();
    ///
    /// // From String
    /// let owned = String::from("world");
    /// let mqtt_str = mqtt::packet::MqttString::new(owned).unwrap();
    ///
    /// // UTF-8 strings are supported
    /// let mqtt_str = mqtt::packet::MqttString::new("hello").unwrap();
    /// ```
    pub fn new(s: impl AsRef<str>) -> Result<Self, MqttError> {
        let s_ref = s.as_ref();
        let len = s_ref.len();

        if len > 65535 {
            return Err(MqttError::MalformedPacket);
        }

        let total_encoded_len = 2 + len;

        // Try to fit in Small variant if SSO is enabled
        #[cfg(any(
            feature = "sso-min-32bit",
            feature = "sso-min-64bit",
            feature = "sso-lv10",
            feature = "sso-lv20"
        ))]
        if len <= SSO_DATA_THRESHOLD {
            let mut buffer = [0u8; SSO_BUFFER_SIZE];
            buffer[0] = (len >> 8) as u8;
            buffer[1] = len as u8;
            buffer[2..2 + len].copy_from_slice(s_ref.as_bytes());
            return Ok(Self::Small(buffer));
        }

        // Fallback to Large variant
        let mut encoded = Vec::with_capacity(total_encoded_len);
        encoded.push((len >> 8) as u8);
        encoded.push(len as u8);
        encoded.extend_from_slice(s_ref.as_bytes());

        Ok(Self::Large(encoded))
    }

    /// Get the complete encoded byte sequence including length prefix
    ///
    /// Returns the complete internal buffer, which includes the 2-byte length prefix
    /// followed by the UTF-8 encoded string bytes. This is the exact format used
    /// in MQTT wire protocol.
    ///
    /// # Returns
    ///
    /// A byte slice containing [length_high, length_low, utf8_bytes...]
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use mqtt_protocol_core::mqtt;
    ///
    /// let mqtt_str = mqtt::packet::MqttString::new("hi").unwrap();
    /// let bytes = mqtt_str.as_bytes();
    /// assert_eq!(bytes, &[0x00, 0x02, b'h', b'i']);
    /// ```
    pub fn as_bytes(&self) -> &[u8] {
        match self {
            MqttString::Large(encoded) => encoded,
            #[cfg(any(
                feature = "sso-min-32bit",
                feature = "sso-min-64bit",
                feature = "sso-lv10",
                feature = "sso-lv20"
            ))]
            MqttString::Small(buffer) => {
                let len = ((buffer[0] as usize) << 8) | (buffer[1] as usize);
                &buffer[..2 + len]
            }
        }
    }

    /// Get the string content as a string slice
    ///
    /// Returns a string slice containing the UTF-8 string data,
    /// excluding the 2-byte length prefix. This operation is zero-cost
    /// since UTF-8 validity was verified at construction time.
    ///
    /// # Returns
    ///
    /// A string slice containing the UTF-8 string data
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use mqtt_protocol_core::mqtt;
    ///
    /// let mqtt_str = mqtt::packet::MqttString::new("hello world").unwrap();
    /// assert_eq!(mqtt_str.as_str(), "hello world");
    /// ```
    pub fn as_str(&self) -> &str {
        // SAFETY: UTF-8 validity verified during MqttString creation or decode
        // Also, no direct modification of encoded field is provided,
        // ensuring buffer immutability
        let data = match self {
            MqttString::Large(encoded) => &encoded[2..],
            #[cfg(any(
                feature = "sso-min-32bit",
                feature = "sso-min-64bit",
                feature = "sso-lv10",
                feature = "sso-lv20"
            ))]
            MqttString::Small(buffer) => {
                let len = ((buffer[0] as usize) << 8) | (buffer[1] as usize);
                &buffer[2..2 + len]
            }
        };
        unsafe { core::str::from_utf8_unchecked(data) }
    }

    /// Get the length of the string data in bytes
    ///
    /// Returns the number of bytes in the UTF-8 string data,
    /// excluding the 2-byte length prefix. Note that this is
    /// the byte length, not the character count.
    ///
    /// # Returns
    ///
    /// The length of the string data in bytes
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use mqtt_protocol_core::mqtt;
    ///
    /// let ascii = mqtt::packet::MqttString::new("hello").unwrap();
    /// assert_eq!(ascii.len(), 5);
    ///
    /// // UTF-8 strings: byte length != character count
    /// let utf8 = mqtt::packet::MqttString::new("hello").unwrap();
    /// assert_eq!(utf8.len(), 5); // 5 characters
    /// ```
    pub fn len(&self) -> usize {
        match self {
            MqttString::Large(encoded) => encoded.len() - 2,
            #[cfg(any(
                feature = "sso-min-32bit",
                feature = "sso-min-64bit",
                feature = "sso-lv10",
                feature = "sso-lv20"
            ))]
            MqttString::Small(buffer) => ((buffer[0] as usize) << 8) | (buffer[1] as usize),
        }
    }

    /// Check if the string is empty
    ///
    /// Returns `true` if the string contains no characters,
    /// `false` otherwise.
    ///
    /// # Returns
    ///
    /// `true` if the string is empty, `false` otherwise
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use mqtt_protocol_core::mqtt;
    ///
    /// let empty = mqtt::packet::MqttString::new("").unwrap();
    /// assert!(empty.is_empty());
    ///
    /// let text = mqtt::packet::MqttString::new("x").unwrap();
    /// assert!(!text.is_empty());
    /// ```
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Get the total encoded size including the length field
    ///
    /// Returns the total number of bytes in the encoded representation,
    /// including the 2-byte length prefix and the UTF-8 string data.
    ///
    /// # Returns
    ///
    /// The total size in bytes (length prefix + string data)
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use mqtt_protocol_core::mqtt;
    ///
    /// let mqtt_str = mqtt::packet::MqttString::new("hello").unwrap();
    /// assert_eq!(mqtt_str.size(), 7); // 2 bytes prefix + 5 bytes data
    /// assert_eq!(mqtt_str.len(), 5);  // Only string length
    /// ```
    pub fn size(&self) -> usize {
        match self {
            MqttString::Large(encoded) => encoded.len(),
            #[cfg(any(
                feature = "sso-min-32bit",
                feature = "sso-min-64bit",
                feature = "sso-lv10",
                feature = "sso-lv20"
            ))]
            MqttString::Small(buffer) => {
                let len = ((buffer[0] as usize) << 8) | (buffer[1] as usize);
                2 + len
            }
        }
    }

    /// Create IoSlice buffers for efficient network I/O
    ///
    /// Returns a vector of `IoSlice` objects that can be used for vectored I/O
    /// operations, allowing zero-copy writes to network sockets.
    ///
    /// # Returns
    ///
    /// A vector containing a single `IoSlice` referencing the complete encoded buffer
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use mqtt_protocol_core::mqtt;
    /// use std::io::IoSlice;
    ///
    /// let mqtt_str = mqtt::packet::MqttString::new("data").unwrap();
    /// let buffers = mqtt_str.to_buffers();
    /// // Can be used with vectored write operations
    /// // socket.write_vectored(&buffers)?;
    /// ```
    #[cfg(feature = "std")]
    pub fn to_buffers(&self) -> Vec<IoSlice<'_>> {
        match self {
            MqttString::Large(encoded) => vec![IoSlice::new(encoded)],
            #[cfg(any(
                feature = "sso-min-32bit",
                feature = "sso-min-64bit",
                feature = "sso-lv10",
                feature = "sso-lv20"
            ))]
            MqttString::Small(buffer) => {
                let len = ((buffer[0] as usize) << 8) | (buffer[1] as usize);
                vec![IoSlice::new(&buffer[..2 + len])]
            }
        }
    }

    /// Create a continuous buffer containing the complete packet data
    ///
    /// Returns a vector containing all packet bytes in a single continuous buffer.
    /// This method is compatible with no-std environments and provides an alternative
    /// to [`to_buffers()`] when vectored I/O is not needed.
    ///
    /// # Returns
    ///
    /// A vector containing the complete encoded buffer
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use mqtt_protocol_core::mqtt;
    ///
    /// let mqtt_str = mqtt::packet::MqttString::new("data").unwrap();
    /// let buffer = mqtt_str.to_continuous_buffer();
    /// // buffer contains all packet bytes
    /// ```
    ///
    /// [`to_buffers()`]: #method.to_buffers
    pub fn to_continuous_buffer(&self) -> Vec<u8> {
        self.as_bytes().to_vec()
    }

    /// Parse string data from a byte sequence
    ///
    /// Decodes MQTT string data from a byte buffer according to the MQTT protocol.
    /// The buffer must start with a 2-byte length prefix followed by valid UTF-8 bytes.
    ///
    /// # Parameters
    ///
    /// * `data` - Byte buffer containing the encoded string data
    ///
    /// # Returns
    ///
    /// * `Ok((MqttString, bytes_consumed))` - Successfully parsed string and number of bytes consumed
    /// * `Err(MqttError::MalformedPacket)` - If the buffer is too short, malformed, or contains invalid UTF-8
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use mqtt_protocol_core::mqtt;
    ///
    /// // Buffer: [length_high, length_low, utf8_bytes...]
    /// let buffer = &[0x00, 0x05, b'h', b'e', b'l', b'l', b'o'];
    /// let (mqtt_str, consumed) = mqtt::packet::MqttString::decode(buffer).unwrap();
    ///
    /// assert_eq!(mqtt_str.as_str(), "hello");
    /// assert_eq!(consumed, 7);
    /// ```
    pub fn decode(data: &[u8]) -> Result<(Self, usize), MqttError> {
        if data.len() < 2 {
            return Err(MqttError::MalformedPacket);
        }

        let string_len = ((data[0] as usize) << 8) | (data[1] as usize);
        if data.len() < 2 + string_len {
            return Err(MqttError::MalformedPacket);
        }

        // Verify UTF-8 validity - return MQTT error on parse failure
        if core::str::from_utf8(&data[2..2 + string_len]).is_err() {
            return Err(MqttError::MalformedPacket);
        }

        let total_encoded_len = 2 + string_len;

        // Try to fit in Small variant if SSO is enabled
        #[cfg(any(
            feature = "sso-min-32bit",
            feature = "sso-min-64bit",
            feature = "sso-lv10",
            feature = "sso-lv20"
        ))]
        if string_len <= SSO_DATA_THRESHOLD {
            let mut buffer = [0u8; SSO_BUFFER_SIZE];
            buffer[0] = data[0];
            buffer[1] = data[1];
            buffer[2..2 + string_len].copy_from_slice(&data[2..2 + string_len]);
            return Ok((Self::Small(buffer), total_encoded_len));
        }

        // Fallback to Large variant
        let mut encoded = Vec::with_capacity(total_encoded_len);
        encoded.extend_from_slice(&data[0..total_encoded_len]);

        Ok((Self::Large(encoded), total_encoded_len))
    }

    /// Check if the string contains a specific character
    ///
    /// Returns `true` if the string contains the specified character,
    /// `false` otherwise.
    ///
    /// # Parameters
    ///
    /// * `c` - The character to search for
    ///
    /// # Returns
    ///
    /// `true` if the character is found, `false` otherwise
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use mqtt_protocol_core::mqtt;
    ///
    /// let mqtt_str = mqtt::packet::MqttString::new("hello world").unwrap();
    /// assert!(mqtt_str.contains('w'));
    /// assert!(!mqtt_str.contains('x'));
    /// ```
    pub fn contains(&self, c: char) -> bool {
        self.as_str().contains(c)
    }

    /// Check if the string starts with the specified prefix
    ///
    /// Returns `true` if the string starts with the given prefix,
    /// `false` otherwise.
    ///
    /// # Parameters
    ///
    /// * `prefix` - The prefix string to check for
    ///
    /// # Returns
    ///
    /// `true` if the string starts with the prefix, `false` otherwise
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use mqtt_protocol_core::mqtt;
    ///
    /// let mqtt_str = mqtt::packet::MqttString::new("hello world").unwrap();
    /// assert!(mqtt_str.starts_with("hello"));
    /// assert!(!mqtt_str.starts_with("world"));
    /// ```
    pub fn starts_with(&self, prefix: &str) -> bool {
        self.as_str().starts_with(prefix)
    }

    /// Check if the string ends with the specified suffix
    ///
    /// Returns `true` if the string ends with the given suffix,
    /// `false` otherwise.
    ///
    /// # Parameters
    ///
    /// * `suffix` - The suffix string to check for
    ///
    /// # Returns
    ///
    /// `true` if the string ends with the suffix, `false` otherwise
    ///
    /// # Examples
    ///
    /// ```ignore
    /// use mqtt_protocol_core::mqtt;
    ///
    /// let mqtt_str = mqtt::packet::MqttString::new("hello world").unwrap();
    /// assert!(mqtt_str.ends_with("world"));
    /// assert!(!mqtt_str.ends_with("hello"));
    /// ```
    pub fn ends_with(&self, suffix: &str) -> bool {
        self.as_str().ends_with(suffix)
    }
}

/// Implementation of `AsRef<str>` for `MqttString`
///
/// Returns the string content when the `MqttString` is used in contexts
/// expecting a string slice reference.
impl AsRef<str> for MqttString {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

/// Implementation of `Display` for `MqttString`
///
/// Formats the string content for display purposes.
/// This allows `MqttString` to be used with `println!`, `format!`, etc.
impl core::fmt::Display for MqttString {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// Implementation of `Deref` for `MqttString`
///
/// Allows `MqttString` to be used directly as a string slice in many contexts
/// through automatic dereferencing. This enables method calls like `mqtt_str.len()`
/// to work directly on the string content.
impl core::ops::Deref for MqttString {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        self.as_str()
    }
}

/// Implementation of `Serialize` for `MqttString`
///
/// Serializes the string content as a string value.
/// This is useful for JSON serialization and other serialization formats.
impl Serialize for MqttString {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        self.as_str().serialize(serializer)
    }
}

/// Implementation of `PartialEq<str>` for `MqttString`
///
/// Allows direct comparison between `MqttString` and `str`.
impl core::cmp::PartialEq<str> for MqttString {
    fn eq(&self, other: &str) -> bool {
        self.as_str() == other
    }
}

/// Implementation of `PartialEq<&str>` for `MqttString`
///
/// Allows direct comparison between `MqttString` and `&str`.
impl core::cmp::PartialEq<&str> for MqttString {
    fn eq(&self, other: &&str) -> bool {
        self.as_str() == *other
    }
}

/// Implementation of `PartialEq<String>` for `MqttString`
///
/// Allows direct comparison between `MqttString` and `String`.
impl core::cmp::PartialEq<String> for MqttString {
    fn eq(&self, other: &String) -> bool {
        self.as_str() == other.as_str()
    }
}

/// Implementation of `Hash` for `MqttString`
///
/// Hashes the string content, allowing `MqttString` to be used in hash-based
/// collections like `HashMap` and `HashSet`.
impl core::hash::Hash for MqttString {
    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
        self.as_str().hash(state);
    }
}

/// Implementation of `Default` for `MqttString`
///
/// Creates an empty `MqttString` with zero-length string content.
/// The internal buffer contains only the 2-byte length prefix (0x00, 0x00).
impl Default for MqttString {
    fn default() -> Self {
        Self::new("").unwrap()
    }
}

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

/// Implementation of `TryFrom<&str>` for `MqttString`
///
/// Converts a string slice to `MqttString`. This is a convenient way to
/// create `MqttString` instances from string literals.
impl TryFrom<&str> for MqttString {
    type Error = MqttError;

    fn try_from(s: &str) -> Result<Self, Self::Error> {
        Self::new(s)
    }
}

/// Implementation of `TryFrom<String>` for `MqttString`
///
/// Converts an owned `String` to `MqttString`. This is a convenient way to
/// create `MqttString` instances from owned strings.
impl TryFrom<String> for MqttString {
    type Error = MqttError;

    fn try_from(s: String) -> Result<Self, Self::Error> {
        Self::new(s)
    }
}

/// Implementation of `TryFrom<&String>` for `MqttString`
///
/// Converts a `String` reference to `MqttString`.
impl TryFrom<&String> for MqttString {
    type Error = MqttError;

    fn try_from(s: &String) -> Result<Self, Self::Error> {
        Self::new(s.as_str())
    }
}

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

    #[test]
    fn test_empty_string() {
        let string = MqttString::new("").unwrap();
        assert_eq!(string.len(), 0);
        assert!(string.is_empty());
        assert_eq!(string.as_str(), "");
        assert_eq!(string.as_bytes(), &[0x00, 0x00]);
    }

    #[test]
    fn test_small_string() {
        let string = MqttString::new("hello").unwrap();
        assert_eq!(string.len(), 5);
        assert!(!string.is_empty());
        assert_eq!(string.as_str(), "hello");
        assert_eq!(
            string.as_bytes(),
            &[0x00, 0x05, b'h', b'e', b'l', b'l', b'o']
        );
    }

    #[cfg(feature = "std")]
    #[test]
    fn test_to_buffers() {
        let data = "buffer test";
        let string = MqttString::new(data).unwrap();
        let buffers = string.to_buffers();

        // Both variants should return 1 buffer containing the encoded data
        assert_eq!(buffers.len(), 1);

        // Verify the buffer contains the complete encoded data
        let buffer_data: &[u8] = &buffers[0];
        assert_eq!(buffer_data, string.as_bytes());
    }

    #[test]
    fn test_string_variants() {
        // Test small data (Small variant if SSO is enabled)
        let small_data = "small";
        let string = MqttString::new(small_data).unwrap();

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

        #[cfg(not(any(
            feature = "sso-min-32bit",
            feature = "sso-min-64bit",
            feature = "sso-lv10",
            feature = "sso-lv20"
        )))]
        assert!(matches!(string, MqttString::Large(_)));

        // Test medium-size data that fits in sso-lv20 but not smaller SSO buffers
        let medium_data = "This is a medium-size string that is longer than small SSO buffers but fits in the largest one"; // ~90 chars
        let string = MqttString::new(medium_data).unwrap();

        // With sso-lv20 (48 bytes), this should be Large (exceeds 48 bytes)
        // With smaller SSO features, this should also be Large
        assert!(matches!(string, MqttString::Large(_)));

        // Test data that should always be Large variant (larger than largest SSO buffer)
        let very_large_data = "This is a very long string that exceeds even the largest SSO buffer size to ensure it's always stored in the Large variant";
        let string = MqttString::new(very_large_data).unwrap();
        assert!(matches!(string, MqttString::Large(_)));
    }
}