rusmpp_core/decode/
owned.rs

1//! Traits for decoding `SMPP` values with owned data.
2
3use bytes::BytesMut;
4
5use crate::decode::DecodeError;
6
7/// Trait for decoding `SMPP` values from a buffer.
8///
9/// # Implementation
10///
11/// ```rust
12/// # use bytes::BytesMut;
13/// # use rusmpp_core::decode::{owned::Decode, DecodeError};
14///
15/// #[derive(Debug, PartialEq, Eq)]
16/// struct Foo {
17///     a: u8,
18///     b: u16,
19///     c: u32,
20/// }
21///
22/// impl Decode for Foo {
23///     fn decode(src: &mut BytesMut) -> Result<(Self, usize), DecodeError> {
24///         let index = 0;
25///
26///         let (a, size) = Decode::decode(src)?;
27///         let index = index + size;
28///
29///         let (b, size) = Decode::decode(src)?;
30///         let index = index + size;
31///
32///         let (c, size) = Decode::decode(src)?;
33///         let index = index + size;
34///
35///         Ok((Foo { a, b, c }, index))
36///     }
37/// }
38///
39/// let mut buf = BytesMut::from(&[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08][..]);
40///
41/// let expected = Foo {
42///     a: 0x01,
43///     b: 0x0203,
44///     c: 0x04050607,
45/// };
46///
47/// let (foo, size) = Foo::decode(&mut buf).unwrap();
48///
49/// assert_eq!(size, 7);
50/// assert_eq!(foo, expected);
51/// assert_eq!(&buf[..], &[0x08]);
52/// ```
53pub trait Decode: Sized {
54    /// Decode a value from a buffer.
55    fn decode(src: &mut BytesMut) -> Result<(Self, usize), DecodeError>;
56}
57
58/// Trait for decoding `SMPP` values from a buffer with a specified length.
59///
60/// # Implementation
61///
62/// ```rust
63/// # use bytes::BytesMut;
64/// # use rusmpp_core::{
65/// #     decode::{owned::{Decode, DecodeWithLength}, DecodeError},
66/// #     types::owned::AnyOctetString,
67/// # };
68///
69/// #[derive(Debug, PartialEq, Eq)]
70/// struct Foo {
71///     a: u8,
72///     b: u16,
73///     c: AnyOctetString,
74/// }
75///
76/// impl DecodeWithLength for Foo {
77///     fn decode(src: &mut BytesMut, length: usize) -> Result<(Self, usize), DecodeError> {
78///         let index = 0;
79///
80///         let (a, size) = Decode::decode(src)?;
81///         let index = index + size;
82///
83///         let (b, size) = Decode::decode(src)?;
84///         let index = index + size;
85///
86///         let (c, size) = AnyOctetString::decode(src, length - index)?;
87///         let index = index + size;
88///
89///         Ok((Foo { a, b, c }, index))
90///     }
91/// }
92///
93/// // Received over the wire
94/// let length = 8;
95///
96/// let mut buf = BytesMut::from(&[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09][..]);
97///
98/// let expected = Foo {
99///     a: 0x01,
100///     b: 0x0203,
101///     c: AnyOctetString::from_static_slice(&[0x04, 0x05, 0x06, 0x07, 0x08]),
102/// };
103///
104/// let (foo, size) = Foo::decode(&mut buf, length).unwrap();
105///
106/// assert_eq!(size, 8);
107/// assert_eq!(foo, expected);
108/// assert_eq!(&buf[..], &[0x09]);
109/// ```
110pub trait DecodeWithLength: Sized {
111    /// Decode a value from a buffer, with a specified length
112    fn decode(src: &mut BytesMut, length: usize) -> Result<(Self, usize), DecodeError>;
113}
114
115/// Everything that implements [`Decode`] also implements [`DecodeWithLength`] by ignoring the length.
116impl<T: Decode> DecodeWithLength for T {
117    fn decode(src: &mut BytesMut, _length: usize) -> Result<(Self, usize), DecodeError> {
118        Decode::decode(src)
119    }
120}
121
122/// Trait for decoding `SMPP` values from a buffer with a specified key and length.
123///
124/// # Implementation
125///
126/// ```rust
127/// # use bytes::BytesMut;
128/// # use rusmpp_core::{
129/// #     decode::{owned::{Decode, DecodeWithKey, DecodeWithLength}, DecodeError},
130/// #     types::owned::AnyOctetString,
131/// # };
132///
133/// #[derive(Debug, PartialEq, Eq)]
134/// enum Foo {
135///     A(u16),
136///     B(AnyOctetString),
137/// }
138///
139/// impl DecodeWithKey for Foo {
140///     type Key = u32;
141///
142///     fn decode(key: Self::Key, src: &mut BytesMut, length: usize) -> Result<(Self, usize), DecodeError> {
143///         match key {
144///             0x01020304 => {
145///                 let (a, size) = Decode::decode(src)?;
146///
147///                 Ok((Foo::A(a), size))
148///             }
149///             0x04030201 => {
150///                 let (b, size) = AnyOctetString::decode(src, length)?;
151///
152///                 Ok((Foo::B(b), size))
153///             }
154///             _ => Err(DecodeError::unsupported_key(key)),
155///         }
156///     }
157/// }
158///
159/// // Received over the wire
160/// let length = 8;
161///
162/// // Key is A
163/// let mut buf = BytesMut::from(&[
164///     0x01, 0x02, 0x03, 0x04, // Key
165///     0x05, 0x06, // Value
166///     0x07, 0x08, 0x09, 0x0A, 0x0B, // Rest
167/// ][..]);
168///
169/// let index = 0;
170///
171/// let (key, size) = Decode::decode(&mut buf).unwrap();
172/// let index = index + size;
173///
174/// let (foo, size) = Foo::decode(key, &mut buf, length - index).unwrap();
175/// let index = index + size;
176///
177/// let expected = Foo::A(0x0506);
178///
179/// assert_eq!(size, 2);
180/// assert_eq!(foo, expected);
181/// assert_eq!(&buf[..], &[0x07, 0x08, 0x09, 0x0A, 0x0B]);
182///
183/// // Received over the wire
184/// let length = 8;
185///
186/// // Key is B
187/// let mut buf = BytesMut::from(&[
188///     0x04, 0x03, 0x02, 0x01, // Key
189///     0x05, 0x06, 0x07, 0x08, // Value
190///     0x09, 0x0A, 0x0B, // Rest
191/// ][..]);
192///
193/// let index = 0;
194///
195/// let (key, size) = Decode::decode(&mut buf).unwrap();
196/// let index = index + size;
197///
198/// let (foo, size) = Foo::decode(key, &mut buf, length - index).unwrap();
199/// let index = index + size;
200///
201/// let expected = Foo::B(AnyOctetString::from_static_slice(&[0x05, 0x06, 0x07, 0x08]));
202///
203/// assert_eq!(size, 4);
204/// assert_eq!(foo, expected);
205/// assert_eq!(&buf[..], &[0x09, 0x0A, 0x0B]);
206/// ```
207pub trait DecodeWithKey: Sized {
208    type Key;
209
210    /// Decode a value from a buffer, using a key to determine the type.
211    fn decode(
212        key: Self::Key,
213        src: &mut BytesMut,
214        length: usize,
215    ) -> Result<(Self, usize), DecodeError>;
216}
217
218/// Trait for decoding optional `SMPP` values from a buffer with a specified key and length.
219///
220/// # Implementation
221///
222/// ```rust
223/// # use bytes::BytesMut;
224/// # use rusmpp_core::{
225/// #     decode::{owned::{Decode, DecodeWithKeyOptional, DecodeWithLength}, DecodeError},
226/// #     types::owned::AnyOctetString,
227/// # };
228///
229/// #[derive(Debug, PartialEq, Eq)]
230/// enum Foo {
231///     A,
232///     B(u16),
233///     C(AnyOctetString),
234/// }
235///
236/// impl DecodeWithKeyOptional for Foo {
237///     type Key = u32;
238///
239///     fn decode(
240///         key: Self::Key,
241///         src: &mut BytesMut,
242///         length: usize,
243///     ) -> Result<Option<(Self, usize)>, DecodeError> {
244///         if length == 0 {
245///             match key {
246///                 0x00000000 => return Ok(Some((Foo::A, 0))),
247///                 _ => return Ok(None),
248///             }
249///         }
250///
251///         match key {
252///             0x01020304 => {
253///                 let (a, size) = Decode::decode(src)?;
254///
255///                 Ok(Some((Foo::B(a), size)))
256///             }
257///             0x04030201 => {
258///                 let (b, size) = AnyOctetString::decode(src, length)?;
259///
260///                 Ok(Some((Foo::C(b), size)))
261///             }
262///             _ => Err(DecodeError::unsupported_key(key)),
263///         }
264///     }
265/// }
266///
267/// // Received over the wire
268/// let length = 4;
269///
270/// // Key is A
271/// let mut buf = BytesMut::from(&[
272///     0x00, 0x00, 0x00, 0x00, // Key
273///     0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, // Rest
274/// ][..]);
275///
276/// let index = 0;
277///
278/// let (key, size) = Decode::decode(&mut buf).unwrap();
279/// let index = index + size;
280///
281/// let (foo, size) = Foo::decode(key, &mut buf, length - index)
282///     .unwrap()
283///     .unwrap();
284/// let index = index + size;
285///
286/// let expected = Foo::A;
287///
288/// assert_eq!(size, 0);
289/// assert_eq!(foo, expected);
290/// assert_eq!(&buf[..], &[0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B]);
291///
292/// // Received over the wire
293/// let length = 4;
294///
295/// // Key is B, but the received length indicates no value
296/// let mut buf = BytesMut::from(&[
297///     0x01, 0x02, 0x03, 0x04, // Key
298///     0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, // Rest
299/// ][..]);
300///
301/// let index = 0;
302///
303/// let (key, size) = Decode::decode(&mut buf).unwrap();
304/// let index = index + size;
305///
306/// let value = Foo::decode(key, &mut buf, length - index).unwrap();
307///
308/// assert!(value.is_none());
309/// assert_eq!(&buf[..], &[0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B]);
310///
311/// // Received over the wire
312/// let length = 8;
313///
314/// // Key is B
315/// let mut buf = BytesMut::from(&[
316///     0x01, 0x02, 0x03, 0x04, // Key
317///     0x05, 0x06, // Value
318///     0x07, 0x08, 0x09, 0x0A, 0x0B, // Rest
319/// ][..]);
320///
321/// let index = 0;
322///
323/// let (key, size) = Decode::decode(&mut buf).unwrap();
324/// let index = index + size;
325///
326/// let (foo, size) = Foo::decode(key, &mut buf, length - index)
327///     .unwrap()
328///     .unwrap();
329/// let index = index + size;
330///
331/// let expected = Foo::B(0x0506);
332///
333/// assert_eq!(size, 2);
334/// assert_eq!(foo, expected);
335/// assert_eq!(&buf[..], &[0x07, 0x08, 0x09, 0x0A, 0x0B]);
336///
337/// // Received over the wire
338/// let length = 8;
339///
340/// // Key is C
341/// let mut buf = BytesMut::from(&[
342///     0x04, 0x03, 0x02, 0x01, // Key
343///     0x05, 0x06, 0x07, 0x08, // Value
344///     0x09, 0x0A, 0x0B, // Rest
345/// ][..]);
346///
347/// let index = 0;
348///
349/// let (key, size) = Decode::decode(&mut buf).unwrap();
350/// let index = index + size;
351///
352/// let (foo, size) = Foo::decode(key, &mut buf, length - index)
353///     .unwrap()
354///     .unwrap();
355/// let index = index + size;
356///
357/// let expected = Foo::C(AnyOctetString::from_static_slice(&[0x05, 0x06, 0x07, 0x08]));
358///
359/// assert_eq!(size, 4);
360/// assert_eq!(foo, expected);
361/// assert_eq!(&buf[..], &[0x09, 0x0A, 0x0B]);
362/// ```
363pub trait DecodeWithKeyOptional: Sized {
364    type Key;
365
366    /// Decode an optional value from a buffer, using a key to determine the type.
367    fn decode(
368        key: Self::Key,
369        src: &mut BytesMut,
370        length: usize,
371    ) -> Result<Option<(Self, usize)>, DecodeError>;
372}
373
374#[doc(hidden)]
375pub trait DecodeExt: Decode {
376    fn decode_move(src: &mut BytesMut, size: usize) -> Result<(Self, usize), DecodeError> {
377        Self::decode(src).map(|(this, size_)| (this, size + size_))
378    }
379
380    /// Decode a vector of values from a buffer with a specified count.
381    fn counted(
382        src: &mut BytesMut,
383        count: usize,
384    ) -> Result<(alloc::vec::Vec<Self>, usize), DecodeError> {
385        (0..count).try_fold(
386            (alloc::vec::Vec::with_capacity(count), 0),
387            |(mut vec, size), _| {
388                Self::decode(src).map(|(item, size_)| {
389                    vec.push(item);
390
391                    (vec, size + size_)
392                })
393            },
394        )
395    }
396
397    fn counted_move(
398        src: &mut BytesMut,
399        count: usize,
400        size: usize,
401    ) -> Result<(alloc::vec::Vec<Self>, usize), DecodeError> {
402        Self::counted(src, count).map(|(vec, size_)| (vec, size + size_))
403    }
404
405    /// Decode a value from a buffer.
406    ///
407    /// If the length is 0, return `None`.
408    fn length_checked_decode(
409        src: &mut BytesMut,
410        length: usize,
411    ) -> Result<Option<(Self, usize)>, DecodeError> {
412        (length > 0)
413            .then_some(())
414            .map(|_| Self::decode(src))
415            .transpose()
416    }
417
418    fn length_checked_decode_move(
419        src: &mut BytesMut,
420        length: usize,
421        size: usize,
422    ) -> Result<Option<(Self, usize)>, DecodeError> {
423        Self::length_checked_decode(src, length)
424            .map(|decoded| decoded.map(|(this, size_)| (this, size + size_)))
425    }
426}
427
428impl<T: Decode> DecodeExt for T {}
429
430#[doc(hidden)]
431pub trait DecodeWithLengthExt: DecodeWithLength {
432    fn decode_move(
433        src: &mut BytesMut,
434        length: usize,
435        size: usize,
436    ) -> Result<(Self, usize), DecodeError> {
437        Self::decode(src, length).map(|(this, size_)| (this, size + size_))
438    }
439}
440
441impl<T: DecodeWithLength> DecodeWithLengthExt for T {}
442
443#[doc(hidden)]
444pub trait DecodeWithKeyExt: DecodeWithKey {
445    /// Decode a value from a buffer, using a key to determine the type.
446    ///
447    /// If the length is 0, return `None`.
448    fn optional_length_checked_decode(
449        key: Self::Key,
450        src: &mut BytesMut,
451        length: usize,
452    ) -> Result<Option<(Self, usize)>, DecodeError> {
453        (length > 0)
454            .then_some(())
455            .map(|_| Self::decode(key, src, length))
456            .transpose()
457    }
458
459    fn optional_length_checked_decode_move(
460        key: Self::Key,
461        src: &mut BytesMut,
462        length: usize,
463        size: usize,
464    ) -> Result<Option<(Self, usize)>, DecodeError> {
465        Self::optional_length_checked_decode(key, src, length)
466            .map(|decoded| decoded.map(|(this, size_)| (this, size + size_)))
467    }
468}
469
470impl<T: DecodeWithKey> DecodeWithKeyExt for T {}
471
472#[doc(hidden)]
473pub trait DecodeWithKeyOptionalExt: DecodeWithKeyOptional {
474    fn decode_move(
475        key: Self::Key,
476        src: &mut BytesMut,
477        length: usize,
478        size: usize,
479    ) -> Result<Option<(Self, usize)>, DecodeError> {
480        Self::decode(key, src, length)
481            .map(|decoded| decoded.map(|(this, size_)| (this, size + size_)))
482    }
483}
484
485impl<T: DecodeWithKeyOptional> DecodeWithKeyOptionalExt for T {}
486
487impl<T: Decode> DecodeWithLength for alloc::vec::Vec<T> {
488    fn decode(src: &mut BytesMut, length: usize) -> Result<(Self, usize), DecodeError> {
489        if length == 0 {
490            return Ok((alloc::vec::Vec::new(), 0));
491        }
492
493        if length > src.len() {
494            return Err(DecodeError::unexpected_eof());
495        }
496
497        let mut src = src.split_to(length);
498
499        let mut size = 0;
500
501        let mut vec = alloc::vec::Vec::new();
502
503        while size < length {
504            let (item, size_) = T::decode(&mut src)?;
505
506            size += size_;
507
508            vec.push(item);
509        }
510
511        Ok((vec, size))
512    }
513}
514
515#[cfg(test)]
516mod tests {
517    use alloc::vec::Vec;
518
519    use crate::{
520        decode::{COctetStringDecodeError, DecodeErrorKind},
521        types::owned::{COctetString, EmptyOrFullCOctetString},
522    };
523
524    use super::*;
525
526    /// Testing [`counted_move`](DecodeExt::counted_move) will automatically test [`counted`](DecodeExt::counted).
527    #[test]
528    fn counted() {
529        // Count is 0
530        let mut buf = BytesMut::from(&[0, 1, 2][..]);
531
532        let (values, size) = u8::counted(&mut buf, 0).unwrap();
533
534        assert_eq!(size, 0);
535        assert_eq!(values.len(), 0);
536        assert_eq!(&buf[..], &[0, 1, 2]);
537        assert_eq!(values, Vec::<u8>::new());
538
539        // Count is more than the buffer
540        let mut buf = BytesMut::from(&[0, 1, 2][..]);
541
542        let error = u8::counted(&mut buf, 5).unwrap_err();
543        assert!(matches!(error.kind(), DecodeErrorKind::UnexpectedEof));
544
545        // Count is within the buffer
546        let mut buf = BytesMut::from(&[0, 1, 2, 3, 4, 5, 6, 7, 8, 9][..]);
547
548        let (values, size) = u8::counted(&mut buf, 10).unwrap();
549
550        assert_eq!(size, 10);
551        assert_eq!(values.len(), 10);
552        assert!(buf.is_empty());
553        assert_eq!(values, alloc::vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
554
555        let mut buf =
556            BytesMut::from(&[0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, 9][..]);
557
558        let (values, size) = u16::counted(&mut buf, 10).unwrap();
559
560        assert_eq!(size, 20);
561        assert_eq!(values.len(), 10);
562        assert!(buf.is_empty());
563        assert_eq!(values, alloc::vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
564
565        let mut buf = BytesMut::from(
566            &[
567                0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5, 0, 0, 0, 6,
568                0, 0, 0, 7, 0, 0, 0, 8, 0, 0, 0, 9,
569            ][..],
570        );
571
572        // Actually 10 values, 12 will break
573        let error = u32::counted(&mut buf, 12).unwrap_err();
574
575        assert!(matches!(error.kind(), DecodeErrorKind::UnexpectedEof));
576
577        let mut buf = BytesMut::from(&b"Hello\0World\0"[..]);
578
579        let (values, size) = COctetString::<1, 6>::counted_move(&mut buf, 2, 0).unwrap();
580
581        assert_eq!(size, 12);
582        assert!(&buf[..].is_empty());
583        assert_eq!(
584            values,
585            alloc::vec![
586                COctetString::<1, 6>::from_static_slice(b"Hello\0").unwrap(),
587                COctetString::<1, 6>::from_static_slice(b"World\0").unwrap(),
588            ]
589        );
590
591        let mut buf = BytesMut::from(&b"Hello\0World\0"[..]);
592
593        let (values, size) = EmptyOrFullCOctetString::<6>::counted_move(&mut buf, 2, 0).unwrap();
594
595        assert_eq!(size, 12);
596        assert!(&buf[..].is_empty());
597        assert_eq!(
598            values,
599            alloc::vec![
600                EmptyOrFullCOctetString::<6>::from_static_slice(b"Hello\0").unwrap(),
601                EmptyOrFullCOctetString::<6>::from_static_slice(b"World\0").unwrap(),
602            ]
603        );
604
605        let mut buf = BytesMut::from(&b"Hello\0World\0Hi"[..]);
606
607        let error = COctetString::<1, 6>::counted_move(&mut buf, 3, 0).unwrap_err();
608
609        assert!(matches!(
610            error.kind(),
611            DecodeErrorKind::COctetStringDecodeError(COctetStringDecodeError::NotNullTerminated)
612        ));
613
614        // Remaining bytes
615        let mut buf = BytesMut::from(&[0, 1, 2, 3, 4, 5, 6, 7, 8, 9][..]);
616
617        let (values, size) = u8::counted_move(&mut buf, 5, 0).unwrap();
618
619        assert_eq!(size, 5);
620        assert_eq!(&buf[..], &[5, 6, 7, 8, 9]);
621        assert_eq!(values, alloc::vec![0, 1, 2, 3, 4]);
622
623        let mut buf =
624            BytesMut::from(&[0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, 9][..]);
625
626        let (values, size) = u16::counted_move(&mut buf, 5, 0).unwrap();
627
628        assert_eq!(size, 10);
629        assert_eq!(&buf[..], &[0, 5, 0, 6, 0, 7, 0, 8, 0, 9]);
630        assert_eq!(values, alloc::vec![0, 1, 2, 3, 4]);
631    }
632
633    #[test]
634    fn decode_with_length_vec() {
635        // Length is 0
636        let mut buf = BytesMut::from(&[0, 1, 2][..]);
637
638        let (values, size) = Vec::<u8>::decode(&mut buf, 0).unwrap();
639
640        assert_eq!(size, 0);
641        assert_eq!(&buf[..], &[0, 1, 2]);
642        assert_eq!(values, Vec::<u8>::new());
643
644        // Length is bigger than the buffer
645        let mut buf = BytesMut::from(&[0, 1, 2][..]);
646
647        let error = Vec::<u8>::decode(&mut buf, 5).unwrap_err();
648
649        assert!(matches!(error.kind(), DecodeErrorKind::UnexpectedEof));
650
651        // Length is within the buffer
652        let mut buf = BytesMut::from(&[0, 1, 2, 3, 4, 5, 6, 7, 8, 9][..]);
653
654        let (values, size) = Vec::<u8>::decode(&mut buf, 10).unwrap();
655
656        assert_eq!(size, 10);
657        assert!(buf.is_empty());
658        assert_eq!(values, alloc::vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
659
660        let mut buf =
661            BytesMut::from(&[0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, 9][..]);
662
663        let (values, size) = Vec::<u16>::decode(&mut buf, 20).unwrap();
664
665        assert_eq!(size, 20);
666        assert!(buf.is_empty());
667        assert_eq!(values, alloc::vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
668
669        let mut buf = BytesMut::from(
670            &[
671                0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4, 0, 0, 0, 5, 0, 0, 0, 6,
672                0, 0, 0, 7, 0, 0, 0, 8, 0, 0, 0, 9,
673            ][..],
674        );
675
676        // Actually 40 bytes, 50 will break
677        let error = Vec::<u32>::decode(&mut buf, 50).unwrap_err();
678
679        assert!(matches!(error.kind(), DecodeErrorKind::UnexpectedEof));
680
681        let mut buf = BytesMut::from(&b"Hello\0World\0"[..]);
682
683        let (values, size) = Vec::<COctetString<1, 6>>::decode(&mut buf, 12).unwrap();
684
685        assert_eq!(size, 12);
686        assert!(buf.is_empty());
687        assert_eq!(
688            values,
689            alloc::vec![
690                COctetString::<1, 6>::from_static_slice(b"Hello\0").unwrap(),
691                COctetString::<1, 6>::from_static_slice(b"World\0").unwrap(),
692            ]
693        );
694
695        let mut buf = BytesMut::from(&b"Hello\0World\0"[..]);
696
697        let (values, size) = Vec::<EmptyOrFullCOctetString<6>>::decode(&mut buf, 12).unwrap();
698
699        assert_eq!(size, 12);
700        assert!(buf.is_empty());
701        assert_eq!(
702            values,
703            alloc::vec![
704                EmptyOrFullCOctetString::<6>::from_static_slice(b"Hello\0").unwrap(),
705                EmptyOrFullCOctetString::<6>::from_static_slice(b"World\0").unwrap(),
706            ]
707        );
708
709        let mut buf = BytesMut::from(&b"Hello\0World\0Hi"[..]);
710
711        // This will try to decode 11 bytes b"Hello\0World"
712        let error = Vec::<COctetString<1, 6>>::decode(&mut buf, 11).unwrap_err();
713
714        assert!(matches!(
715            error.kind(),
716            DecodeErrorKind::COctetStringDecodeError(COctetStringDecodeError::NotNullTerminated)
717        ));
718
719        // Remaining bytes
720        let mut buf = BytesMut::from(&[0, 1, 2, 3, 4, 5, 6, 7, 8, 9][..]);
721
722        let (values, size) = Vec::<u8>::decode(&mut buf, 5).unwrap();
723
724        assert_eq!(size, 5);
725        assert_eq!(&buf[..], &[5, 6, 7, 8, 9]);
726        assert_eq!(values, alloc::vec![0, 1, 2, 3, 4]);
727
728        let mut buf =
729            BytesMut::from(&[0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8, 0, 9][..]);
730
731        let (values, size) = Vec::<u16>::decode(&mut buf, 10).unwrap();
732
733        assert_eq!(size, 10);
734        assert_eq!(&buf[..], &[0, 5, 0, 6, 0, 7, 0, 8, 0, 9]);
735        assert_eq!(values, alloc::vec![0, 1, 2, 3, 4]);
736    }
737}