Add get_as methods for Pcd
This commit is contained in:
parent
e0bc9e9036
commit
b5ac0584ed
30
src/pcd.rs
30
src/pcd.rs
@ -282,6 +282,36 @@ impl Pcd {
|
||||
pub fn insert(&mut self, key: String, value: Vec<u8>) -> Option<Vec<u8>> {
|
||||
self.0.insert(key, value)
|
||||
}
|
||||
|
||||
// Helper methods for deserialization using the trait
|
||||
pub fn get_as_json(&self, key: &str) -> Result<serde_json::Value> {
|
||||
if let Some(data) = self.get(key) {
|
||||
serde_json::Value::deserialize_from_pcd(data)
|
||||
} else {
|
||||
Err(Error::msg("Key not found"))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_as_file_blob(&self, key: &str) -> Result<FileBlob> {
|
||||
if let Some(data) = self.get(key) {
|
||||
FileBlob::deserialize_from_pcd(data)
|
||||
} else {
|
||||
Err(Error::msg("Key not found"))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_as<T: PcdSerializable>(&self, key: &str) -> Result<T> {
|
||||
if let Some(data) = self.get(key) {
|
||||
T::deserialize_from_pcd(data)
|
||||
} else {
|
||||
Err(Error::msg("Key not found"))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert_serializable<T: PcdSerializable>(&mut self, key: String, value: &T) -> Result<Option<Vec<u8>>> {
|
||||
let compressed = value.serialize_to_pcd()?;
|
||||
Ok(self.insert(key, compressed))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize, Tsify)]
|
||||
|
Loading…
x
Reference in New Issue
Block a user