use leveldb::database::Database; use leveldb::options::Options; use leveldb_orm::{EncodedKey, KVOrm, KeyOrm}; use serde::{Deserialize, Serialize}; use tempdir::TempDir; #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] struct Command { pub executable: u8, pub args: Vec, pub current_dir: Option, } impl<'a> leveldb_orm::KeyOrm<'a> for Command { type KeyType = (u8, Vec); type KeyTypeRef = (&'a u8, &'a Vec); #[inline] fn key(&self) -> leveldb_orm::Result> { Self::encode_key((&self.executable, &self.args)) } fn decode_key(data: &EncodedKey) -> leveldb_orm::Result { bincode::deserialize(&data.inner).map_err(|e| e.into()) } fn encode_key(key: Self::KeyTypeRef) -> leveldb_orm::Result> { bincode::serialize(&key) .map(EncodedKey::from) .map_err(|e| e.into()) } } #[test] fn test_user_impl() { let cmd = Command { executable: 1, args: vec!["arg1".into(), "arg2".into(), "arg3".into()], current_dir: Some("\\dir".into()), }; let key = cmd.key().unwrap(); let tempdir = TempDir::new("demo").unwrap(); let path = tempdir.path(); let mut options = Options::new(); options.create_if_missing = true; let database = Database::open(path, options).unwrap(); cmd.put(&database).unwrap(); let res = Command::get(&database, &key).unwrap(); assert_eq!(res, Some(cmd.clone())); Command::delete(&database, false, &key).unwrap(); assert!(Command::get(&database, &key).unwrap().is_none()); }