use bacnet_encoding::tags;
use bacnet_types::error::Error;
use bacnet_types::primitives::PropertyValue;
pub(crate) fn decode_command_write<T>(
value: PropertyValue,
decode: fn(&[u8]) -> Result<T, Error>,
) -> Result<T, Error> {
let PropertyValue::ApplicationData(bytes) = value else {
return Err(super::invalid_data_type_error());
};
match tags::decode_tag(&bytes, 0) {
Ok((tag, _)) if tag.is_context(0) => {}
Ok(_) => return Err(super::invalid_data_type_error()),
Err(_) => return Err(super::invalid_data_encoding_error()),
}
decode(&bytes).map_err(|error| match error {
Error::OutOfRange(_) => super::value_out_of_range_error(),
_ => super::invalid_data_encoding_error(),
})
}