use embedded_io::Write;
use crate::{
autocomplete::{Autocompletion, Request},
cli::CliHandle,
command::RawCommand,
help::HelpRequest,
writer::Writer,
};
#[derive(Debug)]
pub enum ProcessError<'a, E: embedded_io::Error> {
ParseError(ParseError<'a>),
WriteError(E),
}
#[derive(Debug)]
pub enum ParseError<'a> {
NotEnoughArguments,
Other(&'a str),
ParseArgumentError { value: &'a str },
TooManyArguments { expected: usize },
UnknownCommand,
}
impl<'a, E: embedded_io::Error> From<E> for ProcessError<'a, E> {
fn from(value: E) -> Self {
Self::WriteError(value)
}
}
impl<'a, E: embedded_io::Error> From<ParseError<'a>> for ProcessError<'a, E> {
fn from(value: ParseError<'a>) -> Self {
Self::ParseError(value)
}
}
#[derive(Debug)]
pub enum HelpError<E: embedded_io::Error> {
WriteError(E),
UnknownCommand,
}
impl<E: embedded_io::Error> From<E> for HelpError<E> {
fn from(value: E) -> Self {
Self::WriteError(value)
}
}
pub trait Autocomplete {
fn autocomplete(request: Request<'_>, autocompletion: &mut Autocompletion<'_>);
}
pub trait Help {
fn longest_command() -> usize {
0
}
fn help<W: Write<Error = E>, E: embedded_io::Error>(
request: HelpRequest<'_>,
writer: &mut Writer<'_, W, E>,
) -> Result<(), HelpError<E>>;
}
pub trait FromRaw<'a>: Sized {
fn parse(raw: RawCommand<'a>) -> Result<Self, ParseError<'a>>;
}
pub trait CommandProcessor<W: Write<Error = E>, E: embedded_io::Error> {
fn process<'a>(
&mut self,
cli: &mut CliHandle<'_, W, E>,
raw: RawCommand<'a>,
) -> Result<(), ProcessError<'a, E>>;
}
impl<W, E, F> CommandProcessor<W, E> for F
where
W: Write<Error = E>,
E: embedded_io::Error,
F: for<'a> FnMut(&mut CliHandle<'_, W, E>, RawCommand<'a>) -> Result<(), ProcessError<'a, E>>,
{
fn process<'a>(
&mut self,
cli: &mut CliHandle<'_, W, E>,
command: RawCommand<'a>,
) -> Result<(), ProcessError<'a, E>> {
self(cli, command)
}
}