Created
February 14, 2022 13:34
-
-
Save gsora/bb96e3b8b84e165363c089b23317b00d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use pddb::Pddb; | |
use std::io::Read; | |
use std::io::Write; | |
use xous::Error; | |
pub struct PddbMap { | |
basis_name: String, | |
dict_name: String, | |
pddb_inst: pddb::Pddb, | |
} | |
#[derive(Debug)] | |
pub enum MapError { | |
KeyNotFound(String), | |
} | |
impl PddbMap { | |
pub fn new(basis_name: &str, dict_name: &str) -> Self { | |
PddbMap { | |
basis_name: String::from(basis_name), | |
dict_name: String::from(dict_name), | |
pddb_inst: Pddb::new(), | |
} | |
} | |
pub fn set_dict(&mut self, dict_name: &str) { | |
self.dict_name = String::from(dict_name); | |
} | |
pub fn set(&mut self, key: &str, value: &[u8]) { | |
let mut key = self | |
.pddb_inst | |
.get( | |
&self.dict_name, | |
key, | |
Some(&self.basis_name), | |
true, | |
true, | |
None, | |
None::<fn()>, | |
) | |
.expect("cannot create key"); | |
key.write_all(value) | |
.expect("cannot write entirety of value to key") | |
} | |
pub fn get(&mut self, key: &str) -> Result<Vec<u8>, MapError> { | |
let mut pkey = match self.pddb_inst.get( | |
&self.dict_name, | |
key, | |
Some(&self.basis_name), | |
false, | |
false, | |
None, | |
None::<fn()>, | |
) { | |
Ok(key) => key, | |
Err(..) => { | |
return Err(MapError::KeyNotFound(String::from(key))); | |
} | |
}; | |
let attrs = pkey.attributes().expect("cannot fetch key attributes"); | |
let mut data = vec![]; | |
pkey.read_to_end(&mut data); | |
Ok(data) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment