1 use crate::protocol::common::hex::decode_hex_buf; 2 use crate::protocol::common::hex::is_hex; 3 4 /// A wrapper type around a list of hex encoded arguments separated by `;`. 5 #[derive(Debug)] 6 pub struct ArgListHex<'a>(&'a mut [u8]); 7 8 impl<'a> ArgListHex<'a> { from_packet(args: &'a mut [u8]) -> Option<Self>9 pub fn from_packet(args: &'a mut [u8]) -> Option<Self> { 10 // validate that args have valid hex encoding (with ';' delimiters). 11 // this removes all the error handling from the lazy `Args` iterator. 12 if args.iter().any(|b| !(is_hex(*b) || *b == b';')) { 13 return None; 14 } 15 Some(Self(args)) 16 } 17 into_iter(self) -> impl Iterator<Item = &'a [u8]> + 'a18 pub fn into_iter(self) -> impl Iterator<Item = &'a [u8]> + 'a { 19 self.0 20 .split_mut(|b| *b == b';') 21 // the `from_packet` method guarantees that the args are valid hex ascii, so this should 22 // method should never fail. 23 .map(|raw| decode_hex_buf(raw).unwrap_or(&mut [])) 24 .map(|s| s as &[u8]) 25 .filter(|s| !s.is_empty()) 26 } 27 } 28