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