use std::error::Error;
#[cfg(feature = "async")]
use std::{future::Future, pin::Pin};
#[macro_export]
#[cfg_attr(nightly, doc(cfg(feature = "async")))]
macro_rules! async_fn {
($state:ty, $inc:expr) => {{
fn rustc_complains_if_this_name_conflicts_with_the_environment_even_though_its_probably_fine(
state: &mut $state,
args: Vec<String>
) -> ::std::pin::Pin<Box<dyn ::std::future::Future<Output = Result<(), Box<dyn ::std::error::Error>>> + Send + '_ >> {
Box::pin($inc(state, args))
}
rustc_complains_if_this_name_conflicts_with_the_environment_even_though_its_probably_fine
}}
}
#[derive(Clone)]
pub struct Command<T> {
pub command: CommandType<T>,
pub help: String,
}
impl<T> Command<T> {
pub fn new(help: String, command: CommandFn<T>) -> Self {
Self {
command: CommandType::Sync(command),
help,
}
}
#[cfg(feature = "async")]
pub fn new_async(help: String, command: AsyncCommandFn<T>) -> Self {
Self {
command: CommandType::Async(command),
help,
}
}
}
pub type CommandFn<T> = fn(&mut T, Vec<String>) -> Result<(), Box<dyn Error>>;
#[cfg(feature = "async")]
pub type AsyncCommandFn<T> = fn(
&mut T,
Vec<String>,
) -> Pin<
Box<dyn Future<Output = Result<(), Box<dyn Error>>> + Send + '_>,
>;
#[derive(Clone)]
pub enum CommandType<T> {
Sync(CommandFn<T>),
#[cfg(feature = "async")]
Async(AsyncCommandFn<T>),
}