use embedded_io::Write;
use crate::{arguments::FromArgumentError, cli::CliHandle, command::RawCommand};
#[cfg(feature = "autocomplete")]
use crate::autocomplete::{Autocompletion, Request};
#[cfg(feature = "help")]
use crate::writer::Writer;
#[derive(Debug)]
pub enum ProcessError<'a, E: embedded_io::Error> {
ParseError(ParseError<'a>),
WriteError(E),
}
#[derive(Debug)]
#[non_exhaustive]
pub enum ParseError<'a> {
MissingRequiredArgument {
name: &'a str,
},
NonAsciiShortOption,
ParseValueError {
value: &'a str,
expected: &'static str,
},
UnexpectedArgument {
value: &'a str,
},
UnexpectedLongOption {
name: &'a str,
},
UnexpectedShortOption {
name: char,
},
UnknownCommand,
}
impl<'a> From<FromArgumentError<'a>> for ParseError<'a> {
fn from(error: FromArgumentError<'a>) -> Self {
Self::ParseValueError {
value: error.value,
expected: error.expected,
}
}
}
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 {
#[cfg(feature = "autocomplete")]
fn autocomplete(request: Request<'_>, autocompletion: &mut Autocompletion<'_>);
}
pub trait Help {
#[cfg(feature = "help")]
fn command_count() -> usize;
#[cfg(feature = "help")]
fn list_commands<W: Write<Error = E>, E: embedded_io::Error>(
writer: &mut Writer<'_, W, E>,
) -> Result<(), E>;
#[cfg(feature = "help")]
fn command_help<
W: Write<Error = E>,
E: embedded_io::Error,
F: FnMut(&mut Writer<'_, W, E>) -> Result<(), E>,
>(
parent: &mut F,
command: RawCommand<'_>,
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)
}
}