use crate::Error;
pub trait ErrorQueue: Default {
fn error_count(&self) -> usize;
fn push_error(&mut self, error: Error);
fn pop_error(&mut self) -> Option<Error>;
fn clear(&mut self);
}
#[derive(Default)]
pub struct StaticErrorQueue<const N: usize>(heapless::Deque<Error, N>);
impl<const N: usize> StaticErrorQueue<N> {
pub fn new() -> StaticErrorQueue<N> {
StaticErrorQueue::default()
}
}
impl<const N: usize> ErrorQueue for StaticErrorQueue<N> {
fn push_error(&mut self, error: Error) {
#[cfg(feature = "defmt")]
defmt::trace!("Push Error: {}", error);
if self.0.push_back(error).is_err() {
if let Some(value) = self.0.back_mut() {
*value = Error::QueueOverflow;
}
}
}
fn pop_error(&mut self) -> Option<Error> {
self.0.pop_front()
}
fn error_count(&self) -> usize {
self.0.len()
}
fn clear(&mut self) {
self.0.clear();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_push_and_pop_error() {
let mut queue: StaticErrorQueue<3> = StaticErrorQueue::new();
assert_eq!(queue.error_count(), 0);
queue.push_error(Error::ExpressionError);
assert_eq!(queue.error_count(), 1);
let error = queue.pop_error();
assert_eq!(error, Some(Error::ExpressionError));
assert_eq!(queue.error_count(), 0);
}
#[test]
fn test_queue_overflow() {
let mut queue: StaticErrorQueue<2> = StaticErrorQueue::new();
queue.push_error(Error::CalibrationFailed);
queue.push_error(Error::HardwareError);
assert_eq!(queue.error_count(), 2);
queue.push_error(Error::DataTypeError);
assert_eq!(queue.error_count(), 2);
let error = queue.pop_error();
assert_eq!(error, Some(Error::CalibrationFailed));
let error = queue.pop_error();
assert_eq!(error, Some(Error::QueueOverflow));
}
#[test]
fn test_pop_empty_queue() {
let mut queue: StaticErrorQueue<2> = StaticErrorQueue::new();
let error = queue.pop_error();
assert_eq!(error, None);
}
#[test]
fn test_error_count() {
let mut queue: StaticErrorQueue<3> = StaticErrorQueue::new();
assert_eq!(queue.error_count(), 0);
queue.push_error(Error::BlockDataNotAllowed);
assert_eq!(queue.error_count(), 1);
queue.push_error(Error::OutOfMemory);
assert_eq!(queue.error_count(), 2);
queue.pop_error();
assert_eq!(queue.error_count(), 1);
queue.pop_error();
assert_eq!(queue.error_count(), 0);
}
}