#![deny(missing_debug_implementations)]
#![cfg_attr(not(feature = "std"), no_std)]
mod command;
mod error;
mod response;
pub use command::*;
pub use error::*;
pub use response::*;
use core::fmt::{Debug, Display, Formatter};
pub enum HandleError {
NotEnoughBuffer(usize),
Nfc(Box<dyn Display>),
}
impl HandleError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
use HandleError::*;
match self {
NotEnoughBuffer(size) => write!(
f,
"The buffer is too small to write the response. (needs {size} bytes)",
),
Nfc(e) => e.fmt(f),
}
}
}
impl Debug for HandleError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Self::fmt(self, f)
}
}
impl Display for HandleError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Self::fmt(self, f)
}
}
#[cfg(feature = "std")]
impl std::error::Error for HandleError {}
pub type Result = std::result::Result<usize, HandleError>;
pub trait HandlerInCtx<Ctx = ()> {
fn handle_in_ctx(&self, ctx: Ctx, command: &[u8], response: &mut [u8]) -> Result;
}
pub trait Handler: HandlerInCtx<()> {
fn handle(&self, command: &[u8], response: &mut [u8]) -> Result {
self.handle_in_ctx((), command, response)
}
}