use embassy_sync::blocking_mutex::raw::RawMutex;
use embassy_sync::channel::Channel;
use heapless::{String, Vec};
use mqttrs::QoS;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MqttIntent {
pub topic: String<128>,
pub payload: Vec<u8, 512>,
pub qos: QoS,
}
pub struct CommandOverloaded;
pub trait CommandHandler {
fn handle(&self, intent: MqttIntent) -> Result<(), CommandOverloaded>;
}
impl<M: RawMutex, const N: usize> CommandHandler for Channel<M, MqttIntent, N> {
fn handle(&self, intent: MqttIntent) -> Result<(), CommandOverloaded> {
self.try_send(intent).map_err(|_| CommandOverloaded)
}
}
#[cfg(test)]
pub mod mock {
use super::{CommandHandler, CommandOverloaded, MqttIntent};
use core::cell::RefCell;
pub struct MockCommandHandler {
pub received: RefCell<std::vec::Vec<MqttIntent>>,
pub overloaded: bool,
}
impl MockCommandHandler {
pub fn new() -> Self {
Self {
received: RefCell::new(std::vec::Vec::new()),
overloaded: false,
}
}
pub fn new_overloaded() -> Self {
Self {
received: RefCell::new(std::vec::Vec::new()),
overloaded: true,
}
}
}
impl CommandHandler for MockCommandHandler {
fn handle(&self, intent: MqttIntent) -> Result<(), CommandOverloaded> {
if self.overloaded {
return Err(CommandOverloaded);
}
self.received.borrow_mut().push(intent);
Ok(())
}
}
}