Skip to content

Commit

Permalink
Switch persister to a fixed set of keys
Browse files Browse the repository at this point in the history
  • Loading branch information
ivmarkov committed Oct 6, 2024
1 parent e57994c commit 6bd0005
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/persist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use log::info;

use rs_matter::error::{Error, ErrorCode};

use rs_matter_stack::persist::{KvBlobStore, KvPersist};
use rs_matter_stack::persist::{Key, KvBlobStore, KvPersist};

pub type EspMatterPersist<'a, T, C> = KvPersist<'a, EspKvBlobStore<T>, C>;

Expand All @@ -31,10 +31,10 @@ where
Ok(Self(EspNvs::new(nvs, namespace, true)?))
}

fn load_blob<'b>(&self, key: &str, buf: &'b mut [u8]) -> Result<Option<&'b [u8]>, EspError> {
fn load_blob<'b>(&self, key: Key, buf: &'b mut [u8]) -> Result<Option<&'b [u8]>, EspError> {
// TODO: Not really async

let data = self.0.get_blob(key, buf)?;
let data = self.0.get_blob(key.as_ref(), buf)?;
info!(
"Blob {key}: loaded {:?} bytes {data:?}",
data.map(|data| data.len())
Expand All @@ -43,20 +43,20 @@ where
Ok(data)
}

fn store_blob(&mut self, key: &str, data: &[u8]) -> Result<(), EspError> {
fn store_blob(&mut self, key: Key, data: &[u8]) -> Result<(), EspError> {
// TODO: Not really async

self.0.set_blob(key, data)?;
self.0.set_blob(key.as_ref(), data)?;

info!("Blob {key}: stored {} bytes {data:?}", data.len());

Ok(())
}

fn remove_blob(&mut self, key: &str) -> Result<(), EspError> {
fn remove_blob(&mut self, key: Key) -> Result<(), EspError> {
// TODO: Not really async

self.0.remove(key)?;
self.0.remove(key.as_ref())?;

info!("Blob {key}: removed");

Expand All @@ -68,20 +68,20 @@ impl<T> KvBlobStore for EspKvBlobStore<T>
where
T: NvsPartitionId,
{
async fn load<'a>(&mut self, key: &str, buf: &'a mut [u8]) -> Result<Option<&'a [u8]>, Error> {
async fn load<'a>(&mut self, key: Key, buf: &'a mut [u8]) -> Result<Option<&'a [u8]>, Error> {
Ok(self
.load_blob(key, buf)
.map_err(|_| ErrorCode::StdIoError)?) // TODO: We need a dedicated PersistError code here
}

async fn store(&mut self, key: &str, value: &[u8]) -> Result<(), Error> {
async fn store(&mut self, key: Key, value: &[u8]) -> Result<(), Error> {
self.store_blob(key, value)
.map_err(|_| ErrorCode::StdIoError)?;

Ok(())
}

async fn remove(&mut self, key: &str) -> Result<(), Error> {
async fn remove(&mut self, key: Key) -> Result<(), Error> {
self.remove_blob(key).map_err(|_| ErrorCode::StdIoError)?;

Ok(())
Expand Down

0 comments on commit 6bd0005

Please sign in to comment.