use serde::{de::DeserializeOwned, Serialize};
pub(crate) trait PostcardPersistence: Serialize + DeserializeOwned + Sized {
fn to_bytes(&self) -> Result<Vec<u8>, postcard::Error> {
postcard::to_allocvec(self)
}
fn from_bytes(bytes: &[u8]) -> Result<Self, postcard::Error> {
postcard::from_bytes(bytes)
}
fn save_to_file(&self, path: &std::path::Path) -> std::io::Result<()> {
let bytes = self
.to_bytes()
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string()))?;
crate::storage::atomic_write::atomic_write(path, &bytes)
}
fn load_from_file(path: &std::path::Path) -> std::io::Result<Self> {
let bytes = std::fs::read(path)?;
Self::from_bytes(&bytes)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string()))
}
}
#[inline]
pub(crate) fn safe_bitmap_id(id: u64) -> Option<u32> {
u32::try_from(id).ok()
}
#[inline]
pub(crate) fn make_label_prop_key(label: &str, property: &str) -> (String, String) {
(label.to_string(), property.to_string())
}
#[cfg(test)]
#[path = "helpers_tests.rs"]
mod tests;