thruput-xdr-common 0.17.1

XDR Serialization Library
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
#![allow(unused_imports)]
#![allow(dead_code)]

use crate::cfg::*;
pub use crate::error::*;

use core::fmt;
use core::marker::PhantomData;
use core::ops::Deref;

/// A string type that stores arbitrary bytes with a maximum length constraint.
///
/// `StringM<MAX>` is a wrapper around `Vec<u8>` that represents string data,
/// which may or may not be valid UTF-8. This type enforces a maximum length
/// of `MAX` bytes.
///
/// This type corresponds to XDR strings as defined in
/// [RFC 4506 Section 4.11](https://www.rfc-editor.org/rfc/rfc4506#section-4.11):
/// ```text
/// string identifier<m>;
/// string identifier<>;
/// ```
///
/// # Character Encoding
///
/// **Note:** The XDR standard (RFC 4506) specifies that strings should contain
/// ASCII characters. However, this implementation does **not** enforce ASCII
/// encoding, nor does it strictly enforce UTF-8. The underlying storage is a
/// raw byte vector (`Vec<u8>`) that accepts arbitrary bytes. Users are
/// responsible for ensuring their data meets the requirements of their specific
/// XDR protocol.
///
/// # UTF-8 Conversion
///
/// - Fallible conversion to/from UTF-8 `String`: [`TryFrom`]/[`TryInto`]/[`StringM::to_utf8_string`]
/// - Lossy conversion to UTF-8 `String`: [`StringM::to_utf8_string_lossy`]
///
/// # Feature Variants
///
/// - **With `alloc` feature**: `Vec<u8>` is heap-allocated, supporting dynamic data
/// - **Without `alloc` feature**: `Vec<u8>` is `&'static [u8]`, for static data only
///
/// # Display Format and Serde Representation
///
/// When displayed or serialized (with `serde` feature), strings are shown
/// as escaped printable ASCII using [`core::fmt::Display`]/`ToString`. Non-ASCII
/// bytes are hex-escaped (e.g. `\xc3\xa9` for `é`).
///
/// Deserialization via [`core::str::FromStr`] is intentionally permissive: it
/// accepts both the canonical escaped form and raw UTF-8 input. This means
/// `"café"` and `"caf\xc3\xa9"` both deserialize to the same byte sequence,
/// but serialization always produces the escaped form.
///
/// # Type Parameters
///
/// - `MAX` - Maximum number of bytes (defaults to `u32::MAX`)
///
/// # Examples
///
/// ```ignore
/// use xdr_common::StringM;
///
/// // Create a string with max length 50 (e.g., "string name<50>")
/// let s: StringM<50> = "hello".try_into()?;
/// assert_eq!(s.to_utf8_string()?, "hello");
///
/// // Handle arbitrary bytes (may not be valid UTF-8)
/// let bytes: StringM<100> = vec![0xFF, 0xFE].try_into()?;
/// let lossy = bytes.to_utf8_string_lossy(); // Replaces invalid chars
/// ```
#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct StringM<const MAX: u32 = { u32::MAX }>(pub Vec<u8>);

#[cfg(all(feature = "serde", feature = "alloc"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "serde", feature = "alloc"))))]
impl<'de, const MAX: u32> Deserialize<'de> for StringM<MAX> {
    fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        s.parse().map_err(DeError::custom)
    }
}

#[cfg(all(feature = "serde", feature = "alloc"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "serde", feature = "alloc"))))]
impl<const MAX: u32> Serialize for StringM<MAX> {
    fn serialize<S>(&self, ser: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        if self.0.len() > Self::MAX_LEN {
            return Err(<S::Error as SerError>::custom(Error::LengthExceedsMax));
        }
        ser.collect_str(self)
    }
}

impl<const MAX: u32> core::fmt::Display for StringM<MAX> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        #[cfg(feature = "alloc")]
        let v = &self.0;
        #[cfg(not(feature = "alloc"))]
        let v = self.0;
        for b in escape_bytes::Escape::new(v) {
            write!(f, "{}", b as char)?;
        }
        Ok(())
    }
}

impl<const MAX: u32> core::fmt::Debug for StringM<MAX> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        #[cfg(feature = "alloc")]
        let v = &self.0;
        #[cfg(not(feature = "alloc"))]
        let v = self.0;
        write!(f, "StringM(")?;
        for b in escape_bytes::Escape::new(v) {
            write!(f, "{}", b as char)?;
        }
        write!(f, ")")?;
        Ok(())
    }
}

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl<const MAX: u32> core::str::FromStr for StringM<MAX> {
    type Err = Error;
    fn from_str(s: &str) -> core::result::Result<Self, Self::Err> {
        // Each escaped byte is at most 4 input chars (\xNN), so if the input
        // exceeds MAX * 4, the unescaped output will certainly exceed MAX.
        if s.len() > Self::MAX_LEN.saturating_mul(4) {
            return Err(Error::LengthExceedsMax);
        }
        escape_bytes::unescape(s.as_bytes())
            .map_err(|_| Error::Invalid)?
            .try_into()
    }
}

#[cfg(feature = "schemars")]
#[cfg_attr(docsrs, doc(cfg(feature = "schemars")))]
impl<const MAX: u32> schemars::JsonSchema for StringM<MAX> {
    fn schema_name() -> alloc::borrow::Cow<'static, str> {
        alloc::format!("StringM<{}>", MAX).into()
    }

    fn schema_id() -> alloc::borrow::Cow<'static, str> {
        alloc::format!("xdr_common::StringM<{}>", MAX).into()
    }

    fn json_schema(_generator: &mut schemars::SchemaGenerator) -> schemars::Schema {
        schemars::json_schema!({
            "type": "string",
            "x-max-unescaped-bytes": MAX,
            "description": "XDR string with escape_bytes encoding. x-max-unescaped-bytes is the byte limit of the unescaped XDR payload, not the character count of the JSON value.",
        })
    }
}

impl<const MAX: u32> Deref for StringM<MAX> {
    type Target = Vec<u8>;

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

impl<const MAX: u32> Default for StringM<MAX> {
    fn default() -> Self {
        Self(Vec::default())
    }
}

impl<const MAX: u32> StringM<MAX> {
    /// The maximum length as a compile-time constant.
    pub const MAX_LEN: usize = { MAX as usize };

    /// Returns the maximum allowed length for this string.
    #[must_use]
    #[allow(clippy::unused_self)]
    pub fn max_len(&self) -> usize {
        Self::MAX_LEN
    }

    /// Returns a reference to the underlying byte vector.
    #[must_use]
    pub fn as_vec(&self) -> &Vec<u8> {
        self.as_ref()
    }

    /// Returns the string's bytes as a slice.
    ///
    /// Usable in `const` contexts.
    #[must_use]
    pub const fn as_slice(&self) -> &[u8] {
        #[cfg(feature = "alloc")]
        let s: &[u8] = self.0.as_slice();
        #[cfg(not(feature = "alloc"))]
        let s: &[u8] = self.0;
        s
    }
}

impl<const MAX: u32> StringM<MAX> {
    /// Clones the bytes and returns them as a standard `Vec<u8>`.
    ///
    /// Requires the `alloc` feature.
    #[must_use]
    #[cfg(feature = "alloc")]
    #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
    pub fn to_vec(&self) -> Vec<u8> {
        self.into()
    }

    /// Consumes `self` and returns the underlying byte vector.
    #[must_use]
    pub fn into_vec(self) -> Vec<u8> {
        self.into()
    }
}

impl<const MAX: u32> StringM<MAX> {
    /// Converts the string to a UTF-8 `String`.
    ///
    /// Returns [`Error::InvalidUTF8`] if the bytes are not valid UTF-8.
    ///
    /// Requires the `alloc` feature.
    #[cfg(feature = "alloc")]
    #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
    pub fn to_utf8_string(&self) -> Result<String, Error> {
        self.try_into()
    }

    /// Consumes `self` and converts it to a UTF-8 `String`.
    ///
    /// Returns [`Error::InvalidUTF8`] if the bytes are not valid UTF-8.
    ///
    /// Requires the `alloc` feature.
    #[cfg(feature = "alloc")]
    #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
    pub fn into_utf8_string(self) -> Result<String, Error> {
        self.try_into()
    }

    /// Converts the string to a UTF-8 `String`, replacing invalid sequences.
    ///
    /// Invalid UTF-8 sequences are replaced with the Unicode replacement character (U+FFFD).
    ///
    /// Requires the `alloc` feature.
    #[cfg(feature = "alloc")]
    #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
    #[must_use]
    pub fn to_utf8_string_lossy(&self) -> String {
        String::from_utf8_lossy(&self.0).into_owned()
    }

    /// Consumes `self` and converts it to a UTF-8 `String`, replacing invalid sequences.
    ///
    /// Invalid UTF-8 sequences are replaced with the Unicode replacement character (U+FFFD).
    ///
    /// Requires the `alloc` feature.
    #[cfg(feature = "alloc")]
    #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
    #[must_use]
    pub fn into_utf8_string_lossy(self) -> String {
        String::from_utf8_lossy(&self.0).into_owned()
    }
}

impl<const MAX: u32> TryFrom<Vec<u8>> for StringM<MAX> {
    type Error = Error;

    fn try_from(v: Vec<u8>) -> Result<Self, Error> {
        let len: u32 = v.len().try_into().map_err(|_| Error::LengthExceedsMax)?;
        if len <= MAX {
            Ok(StringM(v))
        } else {
            Err(Error::LengthExceedsMax)
        }
    }
}

impl<const MAX: u32> From<StringM<MAX>> for Vec<u8> {
    fn from(v: StringM<MAX>) -> Self {
        v.0
    }
}

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl<const MAX: u32> From<&StringM<MAX>> for Vec<u8> {
    fn from(v: &StringM<MAX>) -> Self {
        v.0.clone()
    }
}

impl<const MAX: u32> AsRef<Vec<u8>> for StringM<MAX> {
    fn as_ref(&self) -> &Vec<u8> {
        &self.0
    }
}

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl<const MAX: u32> TryFrom<&Vec<u8>> for StringM<MAX> {
    type Error = Error;

    fn try_from(v: &Vec<u8>) -> Result<Self, Error> {
        v.as_slice().try_into()
    }
}

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl<const MAX: u32> TryFrom<&[u8]> for StringM<MAX> {
    type Error = Error;

    fn try_from(v: &[u8]) -> Result<Self, Error> {
        let len: u32 = v.len().try_into().map_err(|_| Error::LengthExceedsMax)?;
        if len <= MAX {
            Ok(StringM(v.to_vec()))
        } else {
            Err(Error::LengthExceedsMax)
        }
    }
}

impl<const MAX: u32> AsRef<[u8]> for StringM<MAX> {
    #[cfg(feature = "alloc")]
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
    #[cfg(not(feature = "alloc"))]
    fn as_ref(&self) -> &[u8] {
        self.0
    }
}

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl<const N: usize, const MAX: u32> TryFrom<[u8; N]> for StringM<MAX> {
    type Error = Error;

    fn try_from(v: [u8; N]) -> Result<Self, Error> {
        let len: u32 = v.len().try_into().map_err(|_| Error::LengthExceedsMax)?;
        if len <= MAX {
            Ok(StringM(v.to_vec()))
        } else {
            Err(Error::LengthExceedsMax)
        }
    }
}

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl<const N: usize, const MAX: u32> TryFrom<StringM<MAX>> for [u8; N] {
    type Error = StringM<MAX>;

    fn try_from(v: StringM<MAX>) -> core::result::Result<Self, Self::Error> {
        let s: [u8; N] = v.0.try_into().map_err(StringM::<MAX>)?;
        Ok(s)
    }
}

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl<const N: usize, const MAX: u32> TryFrom<&[u8; N]> for StringM<MAX> {
    type Error = Error;

    fn try_from(v: &[u8; N]) -> Result<Self, Error> {
        let len: u32 = v.len().try_into().map_err(|_| Error::LengthExceedsMax)?;
        if len <= MAX {
            Ok(StringM(v.to_vec()))
        } else {
            Err(Error::LengthExceedsMax)
        }
    }
}

#[cfg(not(feature = "alloc"))]
#[cfg_attr(docsrs, doc(cfg(not(feature = "alloc"))))]
impl<const N: usize, const MAX: u32> TryFrom<&'static [u8; N]> for StringM<MAX> {
    type Error = Error;

    fn try_from(v: &'static [u8; N]) -> Result<Self, Error> {
        let len: u32 = v.len().try_into().map_err(|_| Error::LengthExceedsMax)?;
        if len <= MAX {
            Ok(StringM(v))
        } else {
            Err(Error::LengthExceedsMax)
        }
    }
}

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl<const MAX: u32> TryFrom<&String> for StringM<MAX> {
    type Error = Error;

    fn try_from(v: &String) -> Result<Self, Error> {
        let len: u32 = v.len().try_into().map_err(|_| Error::LengthExceedsMax)?;
        if len <= MAX {
            Ok(StringM(v.as_bytes().to_vec()))
        } else {
            Err(Error::LengthExceedsMax)
        }
    }
}

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl<const MAX: u32> TryFrom<String> for StringM<MAX> {
    type Error = Error;

    fn try_from(v: String) -> Result<Self, Error> {
        let len: u32 = v.len().try_into().map_err(|_| Error::LengthExceedsMax)?;
        if len <= MAX {
            Ok(StringM(v.into()))
        } else {
            Err(Error::LengthExceedsMax)
        }
    }
}

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl<const MAX: u32> TryFrom<StringM<MAX>> for String {
    type Error = Error;

    fn try_from(v: StringM<MAX>) -> Result<Self, Error> {
        Ok(String::from_utf8(v.0)?)
    }
}

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl<const MAX: u32> TryFrom<&StringM<MAX>> for String {
    type Error = Error;

    fn try_from(v: &StringM<MAX>) -> Result<Self, Error> {
        Ok(core::str::from_utf8(v.as_ref())?.to_owned())
    }
}

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl<const MAX: u32> TryFrom<&str> for StringM<MAX> {
    type Error = Error;

    fn try_from(v: &str) -> Result<Self, Error> {
        let len: u32 = v.len().try_into().map_err(|_| Error::LengthExceedsMax)?;
        if len <= MAX {
            Ok(StringM(v.into()))
        } else {
            Err(Error::LengthExceedsMax)
        }
    }
}

#[cfg(not(feature = "alloc"))]
#[cfg_attr(docsrs, doc(cfg(not(feature = "alloc"))))]
impl<const MAX: u32> TryFrom<&'static str> for StringM<MAX> {
    type Error = Error;

    fn try_from(v: &'static str) -> Result<Self, Error> {
        let len: u32 = v.len().try_into().map_err(|_| Error::LengthExceedsMax)?;
        if len <= MAX {
            Ok(StringM(v.as_bytes()))
        } else {
            Err(Error::LengthExceedsMax)
        }
    }
}

impl<'a, const MAX: u32> TryFrom<&'a StringM<MAX>> for &'a str {
    type Error = Error;

    fn try_from(v: &'a StringM<MAX>) -> Result<Self, Error> {
        Ok(core::str::from_utf8(v.as_ref())?)
    }
}

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

    #[cfg(feature = "alloc")]
    extern crate alloc;
    #[cfg(feature = "alloc")]
    use alloc::string::ToString;
    #[cfg(feature = "alloc")]
    use alloc::vec;
    #[cfg(feature = "alloc")]
    use alloc::vec::Vec;

    #[test]
    #[cfg(feature = "alloc")]
    fn test_as_slice_const() {
        // Compile check that as_slice is const-callable. With alloc, StringM
        // values cannot be used inside const eval (Vec's destructor is not
        // const), so the const fn is exercised at runtime.
        const fn as_slice_in_const<const M: u32>(s: &StringM<M>) -> &[u8] {
            s.as_slice()
        }
        let s: StringM<100> = "xdr".try_into().unwrap();
        assert_eq!(as_slice_in_const(&s), b"xdr");
    }

    #[test]
    #[cfg(not(feature = "alloc"))]
    fn test_as_slice_const() {
        const S: StringM<8> = StringM(b"xdr");
        const SLICE: &[u8] = S.as_slice();
        assert_eq!(SLICE, b"xdr");
    }

    #[test]
    #[cfg(feature = "alloc")]
    fn test_to_utf8_string_lossy_valid_utf8() {
        // Valid UTF-8 string should pass through unchanged
        let s: StringM<100> = "Hello, World!".try_into().unwrap();
        assert_eq!(s.to_utf8_string_lossy(), "Hello, World!");
    }

    #[test]
    #[cfg(feature = "alloc")]
    fn test_to_utf8_string_lossy_invalid_utf8() {
        // Invalid UTF-8 bytes should be replaced with replacement character
        let bytes = vec![0xFF, 0xFE, 0xFD];
        let s: StringM<100> = bytes.try_into().unwrap();
        let result = s.to_utf8_string_lossy();

        // Each invalid byte should be replaced with U+FFFD
        assert_eq!(result, "\u{FFFD}\u{FFFD}\u{FFFD}");
    }

    #[test]
    #[cfg(feature = "alloc")]
    fn test_to_utf8_string_lossy_mixed() {
        // Mix of valid and invalid UTF-8
        let mut bytes = Vec::new();
        bytes.extend_from_slice(b"Hello");
        bytes.push(0xFF); // Invalid UTF-8
        bytes.extend_from_slice(b"World");

        let s: StringM<100> = bytes.try_into().unwrap();
        let result = s.to_utf8_string_lossy();

        assert_eq!(result, "Hello\u{FFFD}World");
    }

    #[test]
    #[cfg(feature = "alloc")]
    fn test_into_utf8_string_lossy_valid_utf8() {
        // Valid UTF-8 string should pass through unchanged
        let s: StringM<100> = "Test String".try_into().unwrap();
        assert_eq!(s.into_utf8_string_lossy(), "Test String");
    }

    #[test]
    #[cfg(feature = "alloc")]
    fn test_into_utf8_string_lossy_invalid_utf8() {
        // Invalid UTF-8 bytes should be replaced
        let bytes = vec![0xC0, 0xC1, 0xF5];
        let s: StringM<100> = bytes.try_into().unwrap();
        let result = s.into_utf8_string_lossy();

        // All bytes are invalid UTF-8 starts
        assert!(result.contains('\u{FFFD}'));
    }

    #[test]
    #[cfg(feature = "alloc")]
    fn test_to_utf8_string_success() {
        // Valid UTF-8 should convert successfully
        let s: StringM<100> = "Valid UTF-8".try_into().unwrap();
        assert_eq!(s.to_utf8_string().unwrap(), "Valid UTF-8");
    }

    #[test]
    #[cfg(feature = "alloc")]
    fn test_to_utf8_string_failure() {
        // Invalid UTF-8 should fail
        let bytes = vec![0xFF, 0xFE];
        let s: StringM<100> = bytes.try_into().unwrap();
        assert!(s.to_utf8_string().is_err());
    }

    #[test]
    #[cfg(feature = "alloc")]
    fn test_into_utf8_string_success() {
        // Valid UTF-8 should convert successfully
        let s: StringM<100> = "Another test".try_into().unwrap();
        assert_eq!(s.into_utf8_string().unwrap(), "Another test");
    }

    #[test]
    #[cfg(feature = "alloc")]
    fn test_into_utf8_string_failure() {
        // Invalid UTF-8 should fail
        let bytes = vec![0x80, 0x81];
        let s: StringM<100> = bytes.try_into().unwrap();
        assert!(s.into_utf8_string().is_err());
    }

    #[test]
    fn test_max_len_constraint() {
        // Test that length constraint is enforced
        let short: Result<StringM<5>, _> = "hello".try_into();
        assert!(short.is_ok());

        let too_long: Result<StringM<5>, _> = "hello world".try_into();
        assert!(too_long.is_err());
    }

    #[test]
    #[cfg(feature = "alloc")]
    fn test_empty_string() {
        let s: StringM<100> = "".try_into().unwrap();
        assert_eq!(s.to_utf8_string_lossy(), "");
        assert_eq!(s.to_utf8_string().unwrap(), "");
    }

    #[test]
    #[cfg(feature = "alloc")]
    fn test_unicode_characters() {
        // Test with various Unicode characters
        let s: StringM<100> = "Hello 世界 🌍".try_into().unwrap();
        assert_eq!(s.to_utf8_string_lossy(), "Hello 世界 🌍");
        assert_eq!(s.to_utf8_string().unwrap(), "Hello 世界 🌍");
    }

    #[test]
    #[cfg(feature = "alloc")]
    fn test_fromstr_rejects_overlength() {
        let result = "abcd".parse::<StringM<3>>();
        assert!(matches!(result, Err(Error::LengthExceedsMax)));
    }

    #[test]
    #[cfg(feature = "alloc")]
    fn test_fromstr_accepts_at_max() {
        let s = "abc".parse::<StringM<3>>().unwrap();
        assert_eq!(s.as_ref() as &[u8], b"abc");
    }

    #[test]
    #[cfg(feature = "alloc")]
    fn test_fromstr_rejects_overlength_escaped() {
        let result = "\\x61\\x62\\x63\\x64".parse::<StringM<3>>();
        assert!(matches!(result, Err(Error::LengthExceedsMax)));
    }

    #[test]
    #[cfg(feature = "alloc")]
    fn test_fromstr_roundtrip() {
        let original: StringM<10> = vec![0xFF, b'a', b'b'].try_into().unwrap();
        let displayed = original.to_string();
        let parsed: StringM<10> = displayed.parse().unwrap();
        assert_eq!(original, parsed);
    }

    #[test]
    #[cfg(all(feature = "serde", feature = "alloc"))]
    fn test_stringm_serde_serialize_rejects_overlimit() {
        let value = StringM::<1>(b"ab".to_vec());
        let err = serde_json::to_value(&value).unwrap_err();
        let msg = alloc::format!("{err}");
        assert!(msg.contains("max length exceeded"), "{msg}");
    }

    #[test]
    #[cfg(all(feature = "serde", feature = "alloc"))]
    fn test_stringm_serde_serialize_accepts_at_max() {
        let value = StringM::<2>(b"ab".to_vec());
        let json = serde_json::to_value(&value).unwrap();
        assert_eq!(json, serde_json::json!("ab"));
    }

    #[test]
    #[cfg(all(feature = "serde", feature = "alloc"))]
    fn test_stringm_serde_roundtrip() {
        // Non-UTF8 bytes round-trip via the `\xNN` escape encoding.
        let value = StringM::<10>(vec![0xc3, 0xa9, b'a']);
        let json = serde_json::to_value(&value).unwrap();
        let back: StringM<10> = serde_json::from_value(json).unwrap();
        assert_eq!(value, back);
    }

    #[test]
    #[cfg(all(feature = "serde", feature = "alloc"))]
    fn test_stringm_serde_deserialize_rejects_overlimit() {
        let err = serde_json::from_value::<StringM<1>>(serde_json::json!("abc"));
        assert!(err.is_err());
    }
}