diff --git a/README.md b/README.md index db270bf..c394f33 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,6 @@ For details see `trait StoreBackend`. ## TODO -* NoFlash backend * read_int() * write_str() * automatic value coercion diff --git a/src/lib.rs b/src/lib.rs index 0f2a6f1..ce81174 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,6 +9,7 @@ mod iter; use iter::Iter; mod fmt; use fmt::FmtWrapper; +pub mod no_flash; mod test; pub trait StoreBackend { diff --git a/src/no_flash.rs b/src/no_flash.rs new file mode 100644 index 0000000..8324461 --- /dev/null +++ b/src/no_flash.rs @@ -0,0 +1,33 @@ +use crate::StoreBackend; +use core::fmt; + +/// expect this for any write operation to `NoFlash` +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct NoFlashError; + +impl fmt::Display for NoFlashError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "no flash") + } +} + +/// a valid `StoreBackend` that can be used for configurations lacking a flash +#[derive(Debug, Clone, Copy)] +pub struct NoFlash; + +impl StoreBackend for NoFlash { + type Data = [u8; 0]; + + fn data(&self) -> &Self::Data { + &[] + } + + type Error = NoFlashError; + fn erase(&mut self) -> Result<(), Self::Error> { + Err(NoFlashError) + } + + fn program(&mut self, _: usize, _: &[u8]) -> Result<(), Self::Error> { + Err(NoFlashError) + } +} diff --git a/src/test.rs b/src/test.rs index 0d3272a..0d382fe 100644 --- a/src/test.rs +++ b/src/test.rs @@ -1,6 +1,6 @@ #![cfg(test)] -use crate::{Store, StoreBackend}; +use crate::{Error, no_flash, Store, StoreBackend, WriteError}; const SIZE: usize = 1024; @@ -77,3 +77,13 @@ fn write_many() { assert_eq!(store.read("foo").unwrap().unwrap(), b"do not compact away"); assert_eq!(store.read("bar").unwrap().unwrap(), b"SPAM"); } + +#[test] +fn no_flash() { + let mut store = Store::new(no_flash::NoFlash); + assert_eq!(store.read("foo").unwrap(), None); + assert_eq!( + store.write("foo", b"bar"), + Err(Error::Write(WriteError::Backend(no_flash::NoFlashError))) + ); +}