Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions embedded-cli-macros/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ pub fn impl_processor(vis: &Visibility, target: &TargetType) -> Result<TokenStre
#vis fn processor<
W: _io::Write<Error = E>,
E: _io::Error,
F: FnMut(&mut _cli::cli::CliHandle<'_, W, E>, #ident #unnamed_lifetime) -> Result<(), E>,
F: AsyncFnMut(&mut _cli::cli::CliHandle<'_, W, E>, #ident #unnamed_lifetime) -> Result<(), E>,
>(
f: F,
) -> impl _cli::service::CommandProcessor<W, E> {
struct Processor<
W: _io::Write<Error = E>,
E: _io::Error,
F: FnMut(&mut _cli::cli::CliHandle<'_, W, E>, #ident #unnamed_lifetime) -> Result<(), E>,
F: AsyncFnMut(&mut _cli::cli::CliHandle<'_, W, E>, #ident #unnamed_lifetime) -> Result<(), E>,
> {
f: F,
_ph: core::marker::PhantomData<(W, E)>,
Expand All @@ -32,16 +32,16 @@ pub fn impl_processor(vis: &Visibility, target: &TargetType) -> Result<TokenStre
impl<
W: _io::Write<Error = E>,
E: _io::Error,
F: FnMut(&mut _cli::cli::CliHandle<'_, W, E>, #ident #unnamed_lifetime) -> Result<(), E>,
F: AsyncFnMut(&mut _cli::cli::CliHandle<'_, W, E>, #ident #unnamed_lifetime) -> Result<(), E>,
> _cli::service::CommandProcessor<W, E> for Processor<W, E, F>
{
fn process<'a>(
async fn process<'a>(
&mut self,
cli: &mut _cli::cli::CliHandle<'_, W, E>,
raw: _cli::command::RawCommand<'a>,
) -> Result<(), _cli::service::ProcessError<'a, E>> {
let cmd = <#ident #unnamed_lifetime as _cli::service::FromRaw>::parse(raw)?;
(self.f)(cli, cmd)?;
(self.f)(cli, cmd).await?;
Ok(())
}
}
Expand Down
25 changes: 14 additions & 11 deletions embedded-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,23 +157,26 @@ where
/// command set and/or command processor.
/// In process callback you can change some outside state
/// so next calls will use different processor
pub fn process_byte<C: Autocomplete + Help, P: CommandProcessor<W, E>>(
pub async fn process_byte<C: Autocomplete + Help, P: CommandProcessor<W, E>>(
&mut self,
b: u8,
processor: &mut P,
) -> Result<(), E> {
if let (Some(mut editor), Some(mut input_generator)) =
(self.editor.take(), self.input_generator.take())
{
let result = input_generator
.accept(b)
.map(|input| match input {
let result = if let Some(input) = input_generator.accept(b) {
Some(match input {
Input::Control(control) => {
self.on_control_input::<C, _>(&mut editor, control, processor)
.await
}
Input::Char(text) => self.on_text_input(&mut editor, text),
})
.unwrap_or(Ok(()));
} else {
None
}
.unwrap_or(Ok(()));

self.editor = Some(editor);
self.input_generator = Some(input_generator);
Expand Down Expand Up @@ -244,7 +247,7 @@ where
Ok(())
}

fn on_control_input<C: Autocomplete + Help, P: CommandProcessor<W, E>>(
async fn on_control_input<C: Autocomplete + Help, P: CommandProcessor<W, E>>(
&mut self,
editor: &mut Editor<CommandBuffer>,
control: ControlInput,
Expand All @@ -259,7 +262,7 @@ where
let text = editor.text_mut();

let tokens = Tokens::new(text);
self.process_input::<C, _>(tokens, processor)?;
self.process_input::<C, _>(tokens, processor).await?;

editor.clear();

Expand Down Expand Up @@ -354,15 +357,15 @@ where
Ok(())
}

fn process_command<P: CommandProcessor<W, E>>(
async fn process_command<P: CommandProcessor<W, E>>(
&mut self,
command: RawCommand<'_>,
handler: &mut P,
) -> Result<(), E> {
let cli_writer = Writer::new(&mut self.writer);
let mut handle = CliHandle::new(cli_writer);

let res = handler.process(&mut handle, command);
let res = handler.process(&mut handle, command).await;

if let Some(prompt) = handle.new_prompt {
self.prompt = prompt;
Expand All @@ -380,7 +383,7 @@ where
}

#[allow(clippy::extra_unused_type_parameters)]
fn process_input<C: Help, P: CommandProcessor<W, E>>(
async fn process_input<C: Help, P: CommandProcessor<W, E>>(
&mut self,
tokens: Tokens<'_>,
handler: &mut P,
Expand All @@ -391,7 +394,7 @@ where
return self.process_help::<C>(request);
}

self.process_command(command, handler)?;
self.process_command(command, handler).await?;
};

Ok(())
Expand Down
10 changes: 5 additions & 5 deletions embedded-cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ impl<'a> RawCommand<'a> {
pub fn processor<
W: Write<Error = E>,
E: embedded_io::Error,
F: FnMut(&mut CliHandle<'_, W, E>, RawCommand<'_>) -> Result<(), E>,
F: AsyncFnMut(&mut CliHandle<'_, W, E>, RawCommand<'_>) -> Result<(), E>,
>(
f: F,
) -> impl CommandProcessor<W, E> {
struct Processor<
W: Write<Error = E>,
E: embedded_io::Error,
F: FnMut(&mut CliHandle<'_, W, E>, RawCommand<'_>) -> Result<(), E>,
F: AsyncFnMut(&mut CliHandle<'_, W, E>, RawCommand<'_>) -> Result<(), E>,
> {
f: F,
_ph: PhantomData<(W, E)>,
Expand All @@ -72,15 +72,15 @@ impl<'a> RawCommand<'a> {
impl<
W: Write<Error = E>,
E: embedded_io::Error,
F: FnMut(&mut CliHandle<'_, W, E>, RawCommand<'_>) -> Result<(), E>,
F: AsyncFnMut(&mut CliHandle<'_, W, E>, RawCommand<'_>) -> Result<(), E>,
> CommandProcessor<W, E> for Processor<W, E, F>
{
fn process<'a>(
async fn process<'a>(
&mut self,
cli: &mut CliHandle<'_, W, E>,
raw: RawCommand<'a>,
) -> Result<(), ProcessError<'a, E>> {
(self.f)(cli, raw)?;
(self.f)(cli, raw).await?;
Ok(())
}
}
Expand Down
4 changes: 2 additions & 2 deletions embedded-cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ pub trait FromRaw<'a>: Sized {
}

pub trait CommandProcessor<W: Write<Error = E>, E: embedded_io::Error> {
fn process<'a>(
async fn process<'a>(
&mut self,
cli: &mut CliHandle<'_, W, E>,
raw: RawCommand<'a>,
Expand All @@ -131,7 +131,7 @@ where
E: embedded_io::Error,
F: for<'a> FnMut(&mut CliHandle<'_, W, E>, RawCommand<'a>) -> Result<(), ProcessError<'a, E>>,
{
fn process<'a>(
async fn process<'a>(
&mut self,
cli: &mut CliHandle<'_, W, E>,
command: RawCommand<'a>,
Expand Down
1 change: 1 addition & 0 deletions examples/desktop/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ embedded-cli = { path = "../../embedded-cli" }
embedded-io = "0.7.1"
rand = "0.8.5"
termion = "3.0.0"
tokio = { version = "1.52.3", features = ["full"] }
ufmt = "0.2.0"
6 changes: 4 additions & 2 deletions examples/desktop/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ fn on_status(
Ok(())
}

fn main() {
#[tokio::main]
async fn main() {
let stdout = stdout().into_raw_mode().unwrap();

let writer = Writer { stdout };
Expand Down Expand Up @@ -238,7 +239,7 @@ Use left and right to move inside input."
for byte in bytes {
cli.process_byte::<BaseCommand<'_>, _>(
byte,
&mut BaseCommand::processor(|cli, command| match command {
&mut BaseCommand::processor(async |cli, command| match command {
BaseCommand::Led { id, command } => on_led(cli, &mut state, id, command),
BaseCommand::Adc { id, command } => on_adc(cli, &mut state, id, command),
BaseCommand::Status => on_status(cli, &mut state),
Expand All @@ -248,6 +249,7 @@ Use left and right to move inside input."
}
}),
)
.await
.unwrap();
}

Expand Down