pub struct Tlv {
pub type_code: u8,
pub opt: Opt,
pub payload: Vec<u8>,
pub children: Vec<Tlv>,
pub trailer: Option<Trailer>,
}Expand description
@brief A decoded TLV. For opaque TLVs (opt.PL=0) payload holds the bytes and
children is empty; for structured TLVs (opt.PL=1) children holds the parsed
sub-TLVs and payload is empty.
Fields§
§type_code: u8@brief A type_code value (or a user/unknown nonzero code).
opt: Opt@brief The unpacked option bits.
payload: Vec<u8>@brief Opaque payload bytes (empty when opt.pl).
children: Vec<Tlv>@brief Parsed sub-TLVs (empty unless opt.pl).
trailer: Option<Trailer>@brief Optional trailer (present iff opt.ts || opt.cr).
Implementations§
Source§impl Tlv
impl Tlv
Sourcepub fn first_child(&self, type_code: u8) -> Option<&Tlv>
pub fn first_child(&self, type_code: u8) -> Option<&Tlv>
@brief The first child with the given type_code, or None.
Sourcepub fn child_at(&self, index: usize) -> Option<&Tlv>
pub fn child_at(&self, index: usize) -> Option<&Tlv>
@brief The child at index, or None if out of range.
Sourcepub fn children_of_type(&self, type_code: u8) -> impl Iterator<Item = &Tlv>
pub fn children_of_type(&self, type_code: u8) -> impl Iterator<Item = &Tlv>
@brief Iterate the children with the given type_code.
Sourcepub fn payload_str(&self) -> Result<&str, BuildError>
pub fn payload_str(&self) -> Result<&str, BuildError>
@brief The opaque payload interpreted as UTF-8 (for NAME / DESCRIPTION).
§Errors
BuildError::InvalidUtf8 if the payload is not valid UTF-8.
Sourcepub fn payload_uint(&self) -> u64
pub fn payload_uint(&self) -> u64
@brief The opaque payload interpreted as a little-endian unsigned integer (up to 8 bytes; extra leading zero-extension). Returns 0 for an empty payload.
Examples found in repository?
14fn main() {
15 // Write 21500 (a temperature in milli-degrees) to /sensor/temp on a remote node.
16 let mut req = FwdRequest::new(fwd_op::WRITE, &["sensor", "temp"], &["client"]);
17 req.payload = Some(value_u32(21_500));
18 let frame = encode_fwd_bytes(&req).expect("a valid request");
19 println!("FWD{{WRITE}} ({} bytes): {:02x?}", frame.len(), frame);
20
21 // The receiving node reads the same four children back, positionally.
22 let parsed = decode_fwd(&frame).expect("a well-formed FWD");
23 assert_eq!(parsed.op, fwd_op::WRITE);
24 let dst = fwd_dst_path(&parsed).unwrap();
25 assert_eq!(dst, "/sensor/temp");
26 let value = parsed.payload.as_ref().expect("a WRITE carries its value");
27 assert_eq!(value.payload_uint(), 21_500);
28 println!("dst {dst} <- {}", value.payload_uint());
29
30 // The frame carries no correlation id: the reply finds its way home along `src`.
31 assert_eq!(parsed.src.payload, [6, b'c', b'l', b'i', b'e', b'n', b't']);
32}More examples
16fn main() {
17 // Consumer -> producer: WRITE /sensor/temp:subscribers[] <- SUBSCRIBER{target=/client}.
18 let mut req = FwdRequest::new(fwd_op::WRITE, &["sensor", "temp"], &["client"]);
19 req.field = Some(FieldSel::Str(":subscribers[]"));
20 req.payload = Some(subscriber(&["client"]).unwrap());
21 let frame = encode_fwd_bytes(&req).unwrap();
22 println!("subscribe ({} bytes): {:02x?}", frame.len(), frame);
23
24 // The producer sees a field-write whose payload names where to deliver.
25 let seen = decode_fwd(&frame).unwrap();
26 assert!(seen.field.is_some(), "a subscribe is addressed to a field");
27 let target = subscriber_target_path(seen.payload.as_ref().unwrap()).unwrap();
28 assert_eq!(target.as_deref(), Some("/client"));
29 println!("producer will deliver to {}", target.unwrap());
30
31 // Producer -> consumer: a delivery is a WRITE of the new VALUE to that target.
32 let mut push = FwdRequest::new(fwd_op::WRITE, &["client"], &["sensor", "temp"]);
33 push.payload = Some(value_u32(21_750));
34 let delivery = decode_fwd(&encode_fwd_bytes(&push).unwrap()).unwrap();
35 assert_eq!(delivery.op, fwd_op::WRITE);
36 assert_eq!(fwd_dst_path(&delivery).unwrap(), "/client");
37 assert_eq!(delivery.payload.unwrap().payload_uint(), 21_750);
38 println!("delivery: WRITE /client <- 21750");
39}