use crate::command::Command;
use crate::error::{ErrorCode, Result};
use crate::response::Formatter;
use crate::tokenizer::Tokenizer;
use crate::Context;
#[macro_export]
macro_rules! scpi_tree {
($($node:expr),*) => {
&Node{name: b"ROOT", optional: false, handler: None, sub: &[
$(
$node
),*
]}
};
}
pub struct Node<'a> {
pub name: &'static [u8],
pub handler: Option<&'a dyn Command>,
pub sub: &'a [Node<'a>],
pub optional: bool,
}
impl<'a> Node<'a> {
pub(crate) fn exec<FMT>(
&self,
context: &mut Context,
args: &mut Tokenizer,
response: &mut FMT,
query: bool,
) -> Result<()>
where
FMT: Formatter,
{
if let Some(handler) = self.handler {
if query {
handler.query(context, args, &mut response.response_unit()?)
} else {
handler.event(context, args)
}
} else if !self.sub.is_empty() {
for child in self.sub {
if child.optional {
return child.exec(context, args, response, query);
}
}
Err(ErrorCode::CommandHeaderError.into())
} else {
Err(ErrorCode::CommandHeaderError.into())
}
}
}