Skip to main content

decode_application_value_ref

Function decode_application_value_ref 

Source
pub fn decode_application_value_ref(
    data: &[u8],
    offset: usize,
) -> Result<(ApplicationValueRef<'_>, usize), Error>
Expand description

Decode one application-tagged primitive value from data at offset, without copying it; returns the value and the offset past it.

It refuses what the owned decoder, decode_application_value, refuses in an application-tagged value: a fixed-size value whose header gives another length is Error::Decoding, contents that run past the end of data are Error::BufferTooShort. Every offset it reports indexes data, a fault inside the contents included, where the owned decoder reports one of those from the contents’ start. It also refuses a context-tagged element, which only the owned decoder keeps (as its raw octets), with DecodingKind::InvalidTag. A CharacterString is checked as CharacterStringRef::decode checks it, so one in a character set the owned decoder can’t convert still reads here.

use bacnet_encoding::primitives::{decode_application_value_ref, ApplicationValueRef};

// A REAL 72.5, then a CharacterString "AV".
let data = [0x44, 0x42, 0x91, 0x00, 0x00, 0x73, 0x00, b'A', b'V'];
let (real, next) = decode_application_value_ref(&data, 0)?;
assert_eq!(real, ApplicationValueRef::Real(72.5));
let (text, end) = decode_application_value_ref(&data, next)?;
let ApplicationValueRef::CharacterString(text) = text else { unreachable!() };
assert_eq!((text.as_str(), end), (Some("AV"), data.len()));