DecodeWithKeyOptional

Trait DecodeWithKeyOptional 

Source
pub trait DecodeWithKeyOptional: Sized {
    type Key;

    // Required method
    fn decode(
        key: Self::Key,
        src: &mut BytesMut,
        length: usize,
    ) -> Result<Option<(Self, usize)>, DecodeError>;
}
Available on crate feature alloc only.
Expand description

Trait for decoding optional SMPP values from a buffer with a specified key and length.

§Implementation


#[derive(Debug, PartialEq, Eq)]
enum Foo {
    A,
    B(u16),
    C(AnyOctetString),
}

impl DecodeWithKeyOptional for Foo {
    type Key = u32;

    fn decode(
        key: Self::Key,
        src: &mut BytesMut,
        length: usize,
    ) -> Result<Option<(Self, usize)>, DecodeError> {
        if length == 0 {
            match key {
                0x00000000 => return Ok(Some((Foo::A, 0))),
                _ => return Ok(None),
            }
        }

        match key {
            0x01020304 => {
                let (a, size) = Decode::decode(src)?;

                Ok(Some((Foo::B(a), size)))
            }
            0x04030201 => {
                let (b, size) = AnyOctetString::decode(src, length)?;

                Ok(Some((Foo::C(b), size)))
            }
            _ => Err(DecodeError::unsupported_key(key)),
        }
    }
}

// Received over the wire
let length = 4;

// Key is A
let mut buf = BytesMut::from(&[
    0x00, 0x00, 0x00, 0x00, // Key
    0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, // Rest
][..]);

let index = 0;

let (key, size) = Decode::decode(&mut buf).unwrap();
let index = index + size;

let (foo, size) = Foo::decode(key, &mut buf, length - index)
    .unwrap()
    .unwrap();
let index = index + size;

let expected = Foo::A;

assert_eq!(size, 0);
assert_eq!(foo, expected);
assert_eq!(&buf[..], &[0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B]);

// Received over the wire
let length = 4;

// Key is B, but the received length indicates no value
let mut buf = BytesMut::from(&[
    0x01, 0x02, 0x03, 0x04, // Key
    0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, // Rest
][..]);

let index = 0;

let (key, size) = Decode::decode(&mut buf).unwrap();
let index = index + size;

let value = Foo::decode(key, &mut buf, length - index).unwrap();

assert!(value.is_none());
assert_eq!(&buf[..], &[0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B]);

// Received over the wire
let length = 8;

// Key is B
let mut buf = BytesMut::from(&[
    0x01, 0x02, 0x03, 0x04, // Key
    0x05, 0x06, // Value
    0x07, 0x08, 0x09, 0x0A, 0x0B, // Rest
][..]);

let index = 0;

let (key, size) = Decode::decode(&mut buf).unwrap();
let index = index + size;

let (foo, size) = Foo::decode(key, &mut buf, length - index)
    .unwrap()
    .unwrap();
let index = index + size;

let expected = Foo::B(0x0506);

assert_eq!(size, 2);
assert_eq!(foo, expected);
assert_eq!(&buf[..], &[0x07, 0x08, 0x09, 0x0A, 0x0B]);

// Received over the wire
let length = 8;

// Key is C
let mut buf = BytesMut::from(&[
    0x04, 0x03, 0x02, 0x01, // Key
    0x05, 0x06, 0x07, 0x08, // Value
    0x09, 0x0A, 0x0B, // Rest
][..]);

let index = 0;

let (key, size) = Decode::decode(&mut buf).unwrap();
let index = index + size;

let (foo, size) = Foo::decode(key, &mut buf, length - index)
    .unwrap()
    .unwrap();
let index = index + size;

let expected = Foo::C(AnyOctetString::from_static_slice(&[0x05, 0x06, 0x07, 0x08]));

assert_eq!(size, 4);
assert_eq!(foo, expected);
assert_eq!(&buf[..], &[0x09, 0x0A, 0x0B]);

Required Associated Types§

Required Methods§

Source

fn decode( key: Self::Key, src: &mut BytesMut, length: usize, ) -> Result<Option<(Self, usize)>, DecodeError>

Decode an optional value from a buffer, using a key to determine the type.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§