mod cmac;
mod crc;
mod encrypted;
mod mock;
mod plain;
mod tap;
mod transport_backend;
pub use cmac::{cmac_out_cmac_in, plain_out_cmac_in};
pub(crate) use crc::check_crc32;
pub use encrypted::{encrypted_out_cmac_in, encrypted_out_plain_in, plain_out_encrypted_in};
pub use mock::MockBackend;
#[allow(unused_imports)]
pub(crate) use mock::mock_backend;
pub use plain::{plain, plain_multi};
pub use tap::TapBackend;
pub use transport_backend::TransportBackend;
use crate::{
StatusCode,
std::{fmt::Debug, future::Future},
};
pub trait Backend
where
Self::Error: Debug,
{
type Error;
fn exchange_raw(
&mut self,
output: &mut [u8],
input: &[u8],
) -> impl Future<Output = Result<usize, Self::Error>>;
#[allow(async_fn_in_trait)]
async fn exchange<'a>(
&mut self,
output: &'a mut [u8],
input: &[u8],
) -> Result<(StatusCode, &'a [u8]), Self::Error> {
#[cfg(feature = "tracing")]
tracing::trace!(direction = "request", data = hex::encode(input));
let n = self.exchange_raw(output, input).await?;
assert!(n > 0);
let status_code: StatusCode = output[0].into();
let data = &output[1..n];
#[cfg(feature = "tracing")]
tracing::trace!(
direction = "response",
status_code = format!("{:?}", status_code),
data = hex::encode(data),
);
Ok((status_code, data))
}
}
impl<T> Backend for &mut T
where
T: Backend,
{
type Error = T::Error;
async fn exchange_raw(
&mut self,
output: &mut [u8],
input: &[u8],
) -> Result<usize, Self::Error> {
<T as Backend>::exchange_raw(self, output, input).await
}
}