use core::future::Future;
use zerocopy::{Immutable, IntoBytes};
use crate::entry::EntryHeader;
use crate::error::Error;
use crate::run::Run;
use crate::schema::{Tagged, read_tagged, tag, untag, untag_payload};
use crate::typed::SensorData;
pub trait LogSink {
fn append(&mut self, sensor_id: u16, timestamp: u64, payload: &[u8]) -> Result<(), Error>;
fn append_run(&mut self, run: &Run<'_>) -> Result<(), Error>;
fn sync(&mut self) -> Result<(), Error>;
}
pub trait LogSinkExt: LogSink {
fn append_value<T: IntoBytes + Immutable>(
&mut self,
sensor_id: u16,
timestamp: u64,
data: &T,
) -> Result<(), Error> {
self.append(sensor_id, timestamp, data.as_bytes())
}
fn append_sensor<T: SensorData>(&mut self, timestamp: u64, data: &T) -> Result<(), Error> {
let framed = tag(data);
self.append(T::SENSOR_ID, timestamp, framed.bytes())
}
}
impl<L: LogSink + ?Sized> LogSinkExt for L {}
pub trait LogSource {
fn latest(
&mut self,
sensor_id: u16,
buf: &mut [u8],
) -> Result<Option<(EntryHeader, usize)>, Error>;
fn oldest(
&mut self,
sensor_id: u16,
buf: &mut [u8],
) -> Result<Option<(EntryHeader, usize)>, Error>;
fn count(&mut self, sensor_id: Option<u16>) -> Result<u32, Error>;
fn query_range(
&mut self,
sensor_id: Option<u16>,
t_start: u64,
t_end: u64,
callback: &mut dyn FnMut(&EntryHeader, &[u8]) -> bool,
) -> Result<u32, Error>;
}
pub trait LogSourceExt: LogSource {
fn latest_sensor<T: SensorData>(&mut self) -> Result<Option<(EntryHeader, T)>, Error> {
read_tagged(|buf| self.latest(T::SENSOR_ID, buf))
}
fn oldest_sensor<T: SensorData>(&mut self) -> Result<Option<(EntryHeader, T)>, Error> {
read_tagged(|buf| self.oldest(T::SENSOR_ID, buf))
}
fn count_sensor<T: SensorData>(&mut self) -> Result<u32, Error> {
self.count(Some(T::SENSOR_ID))
}
fn query_sensor<T: SensorData, C>(
&mut self,
t_start: u64,
t_end: u64,
mut callback: C,
) -> Result<u32, Error>
where
C: FnMut(&EntryHeader, &T) -> bool,
{
let mut delivered = 0u32;
self.query_range(
Some(T::SENSOR_ID),
t_start,
t_end,
&mut |header: &EntryHeader, payload: &[u8]| {
let Some(value) = untag_payload::<T>(payload) else {
return true;
};
delivered += 1;
callback(header, &value)
},
)?;
Ok(delivered)
}
fn query_all_sensor<T: SensorData, C>(&mut self, callback: C) -> Result<u32, Error>
where
C: FnMut(&EntryHeader, &T) -> bool,
{
self.query_sensor(0, u64::MAX, callback)
}
}
impl<L: LogSource + ?Sized> LogSourceExt for L {}
pub trait LogSinkAsync {
fn append(
&mut self,
sensor_id: u16,
timestamp: u64,
payload: &[u8],
) -> impl Future<Output = Result<(), Error>>;
fn append_run(&mut self, run: &Run<'_>) -> impl Future<Output = Result<(), Error>>;
fn sync(&mut self) -> impl Future<Output = Result<(), Error>>;
}
pub trait LogSinkAsyncExt: LogSinkAsync {
fn append_value<T: IntoBytes + Immutable>(
&mut self,
sensor_id: u16,
timestamp: u64,
data: &T,
) -> impl Future<Output = Result<(), Error>> {
self.append(sensor_id, timestamp, data.as_bytes())
}
#[allow(clippy::manual_async_fn)]
fn append_sensor<T: SensorData>(
&mut self,
timestamp: u64,
data: &T,
) -> impl Future<Output = Result<(), Error>> {
async move {
let framed = tag(data);
self.append(T::SENSOR_ID, timestamp, framed.bytes()).await
}
}
}
impl<L: LogSinkAsync + ?Sized> LogSinkAsyncExt for L {}
pub trait LogSourceAsync {
fn latest(
&mut self,
sensor_id: u16,
buf: &mut [u8],
) -> impl Future<Output = Result<Option<(EntryHeader, usize)>, Error>>;
fn oldest(
&mut self,
sensor_id: u16,
buf: &mut [u8],
) -> impl Future<Output = Result<Option<(EntryHeader, usize)>, Error>>;
fn count(&mut self, sensor_id: Option<u16>) -> impl Future<Output = Result<u32, Error>>;
fn query_range(
&mut self,
sensor_id: Option<u16>,
t_start: u64,
t_end: u64,
callback: &mut dyn FnMut(&EntryHeader, &[u8]) -> bool,
) -> impl Future<Output = Result<u32, Error>>;
}
pub trait LogSourceAsyncExt: LogSourceAsync {
#[allow(clippy::manual_async_fn)]
fn latest_sensor<T: SensorData>(
&mut self,
) -> impl Future<Output = Result<Option<(EntryHeader, T)>, Error>> {
async {
let mut framed = Tagged::<T>::empty();
let found = self.latest(T::SENSOR_ID, framed.bytes_mut()).await?;
untag(found, framed)
}
}
#[allow(clippy::manual_async_fn)]
fn oldest_sensor<T: SensorData>(
&mut self,
) -> impl Future<Output = Result<Option<(EntryHeader, T)>, Error>> {
async {
let mut framed = Tagged::<T>::empty();
let found = self.oldest(T::SENSOR_ID, framed.bytes_mut()).await?;
untag(found, framed)
}
}
fn count_sensor<T: SensorData>(&mut self) -> impl Future<Output = Result<u32, Error>> {
self.count(Some(T::SENSOR_ID))
}
#[allow(clippy::manual_async_fn)]
fn query_sensor<T: SensorData, C>(
&mut self,
t_start: u64,
t_end: u64,
mut callback: C,
) -> impl Future<Output = Result<u32, Error>>
where
C: FnMut(&EntryHeader, &T) -> bool,
{
async move {
let mut delivered = 0u32;
self.query_range(
Some(T::SENSOR_ID),
t_start,
t_end,
&mut |header: &EntryHeader, payload: &[u8]| {
let Some(value) = untag_payload::<T>(payload) else {
return true;
};
delivered += 1;
callback(header, &value)
},
)
.await?;
Ok(delivered)
}
}
fn query_all_sensor<T: SensorData, C>(
&mut self,
callback: C,
) -> impl Future<Output = Result<u32, Error>>
where
C: FnMut(&EntryHeader, &T) -> bool,
{
self.query_sensor(0, u64::MAX, callback)
}
}
impl<L: LogSourceAsync + ?Sized> LogSourceAsyncExt for L {}