use borrowme::borrowme;
use crate::cursor::{self, Cursor};
#[derive(thiserror::Error, Debug)]
#[error(transparent)]
pub struct Error(#[from] cursor::Error);
pub(crate) const REQUEST_HEADER: &[u8] = b"V";
pub(crate) const RESPONSE_HEADER: u8 = b'E';
#[borrowme]
#[derive(Debug)]
#[owned_attr(derive(Clone))]
#[owned_attr(doc(hidden))]
pub struct Rule<'a> {
pub name: &'a str,
pub value: &'a str,
}
#[borrowme]
#[derive(Debug)]
#[owned_attr(derive(Clone))]
#[owned_attr(doc(hidden))]
pub struct Rules<'a> {
pub num: u16,
pub rules: Vec<Rule<'a>>,
}
impl<'a> Rule<'a> {
pub fn to_owned(self) -> OwnedRule {
borrowme::to_owned(self)
}
}
impl<'a> Rules<'a> {
pub(crate) fn parse(raw: &'a [u8]) -> Result<Self, Error> {
let mut cur = Cursor::new(raw);
let num = cur.read_short()?;
let mut rules = Vec::with_capacity(num as usize);
for _ in 0..num {
rules.push(Rule {
name: cur.read_str()?,
value: cur.read_str()?,
});
}
Ok(Self { num, rules })
}
pub fn to_owned(self) -> OwnedRules {
borrowme::to_owned(self)
}
}