2021-04-05 17:55:53 +00:00
|
|
|
use std::io::{Read, Write};
|
2021-04-09 00:32:42 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2023-01-04 12:07:13 +00:00
|
|
|
use std::sync::{Arc, RwLock};
|
2021-04-05 17:55:53 +00:00
|
|
|
|
2021-04-05 18:33:24 +00:00
|
|
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
2021-04-05 17:55:53 +00:00
|
|
|
|
|
|
|
use crate::error::Error;
|
2023-01-03 13:44:47 +00:00
|
|
|
use crate::migrate::Migrate;
|
2021-04-05 17:55:53 +00:00
|
|
|
|
2023-01-03 13:44:47 +00:00
|
|
|
pub struct Persister<T: Migrate> {
|
2021-04-05 17:55:53 +00:00
|
|
|
path: PathBuf,
|
|
|
|
|
|
|
|
_marker: std::marker::PhantomData<T>,
|
|
|
|
}
|
|
|
|
|
2023-01-03 13:44:47 +00:00
|
|
|
impl<T: Migrate> Persister<T> {
|
2021-04-09 00:32:42 +00:00
|
|
|
pub fn new(base_dir: &Path, file_name: &str) -> Self {
|
|
|
|
let mut path = base_dir.to_path_buf();
|
2021-04-05 17:55:53 +00:00
|
|
|
path.push(file_name);
|
|
|
|
Self {
|
|
|
|
path,
|
|
|
|
_marker: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-03 13:44:47 +00:00
|
|
|
fn decode(&self, bytes: &[u8]) -> Result<T, Error> {
|
|
|
|
match T::decode(bytes) {
|
|
|
|
Some(v) => Ok(v),
|
|
|
|
None => {
|
|
|
|
error!(
|
|
|
|
"Unable to decode persisted data file {}",
|
|
|
|
self.path.display()
|
|
|
|
);
|
|
|
|
for line in hexdump::hexdump_iter(bytes) {
|
|
|
|
debug!("{}", line);
|
|
|
|
}
|
|
|
|
Err(Error::Message(format!(
|
|
|
|
"Unable to decode persisted data file {}",
|
|
|
|
self.path.display()
|
|
|
|
)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 17:55:53 +00:00
|
|
|
pub fn load(&self) -> Result<T, Error> {
|
|
|
|
let mut file = std::fs::OpenOptions::new().read(true).open(&self.path)?;
|
|
|
|
|
|
|
|
let mut bytes = vec![];
|
|
|
|
file.read_to_end(&mut bytes)?;
|
|
|
|
|
2023-01-03 13:44:47 +00:00
|
|
|
let value = self.decode(&bytes[..])?;
|
2021-04-05 17:55:53 +00:00
|
|
|
Ok(value)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn save(&self, t: &T) -> Result<(), Error> {
|
2023-01-03 13:44:47 +00:00
|
|
|
let bytes = t.encode()?;
|
2021-04-05 17:55:53 +00:00
|
|
|
|
|
|
|
let mut file = std::fs::OpenOptions::new()
|
|
|
|
.write(true)
|
|
|
|
.create(true)
|
|
|
|
.truncate(true)
|
|
|
|
.open(&self.path)?;
|
|
|
|
|
|
|
|
file.write_all(&bytes[..])?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-04-05 18:33:24 +00:00
|
|
|
pub async fn load_async(&self) -> Result<T, Error> {
|
|
|
|
let mut file = tokio::fs::File::open(&self.path).await?;
|
|
|
|
|
|
|
|
let mut bytes = vec![];
|
|
|
|
file.read_to_end(&mut bytes).await?;
|
|
|
|
|
2023-01-03 13:44:47 +00:00
|
|
|
let value = self.decode(&bytes[..])?;
|
2021-04-05 18:33:24 +00:00
|
|
|
Ok(value)
|
|
|
|
}
|
|
|
|
|
2021-04-05 17:55:53 +00:00
|
|
|
pub async fn save_async(&self, t: &T) -> Result<(), Error> {
|
2023-01-03 13:44:47 +00:00
|
|
|
let bytes = t.encode()?;
|
2021-04-05 17:55:53 +00:00
|
|
|
|
|
|
|
let mut file = tokio::fs::File::create(&self.path).await?;
|
|
|
|
file.write_all(&bytes[..]).await?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2023-01-04 12:07:13 +00:00
|
|
|
|
|
|
|
pub struct PersisterShared<V: Migrate + Default>(Arc<(Persister<V>, RwLock<V>)>);
|
|
|
|
|
|
|
|
impl<V: Migrate + Default> Clone for PersisterShared<V> {
|
|
|
|
fn clone(&self) -> PersisterShared<V> {
|
|
|
|
PersisterShared(self.0.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<V: Migrate + Default> PersisterShared<V> {
|
|
|
|
pub fn new(base_dir: &Path, file_name: &str) -> Self {
|
|
|
|
let persister = Persister::new(base_dir, file_name);
|
|
|
|
let value = persister.load().unwrap_or_default();
|
|
|
|
Self(Arc::new((persister, RwLock::new(value))))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_with<F, R>(&self, f: F) -> R
|
|
|
|
where
|
|
|
|
F: FnOnce(&V) -> R,
|
|
|
|
{
|
|
|
|
let value = self.0 .1.read().unwrap();
|
|
|
|
f(&value)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_with<F>(&self, f: F) -> Result<(), Error>
|
|
|
|
where
|
|
|
|
F: FnOnce(&mut V),
|
|
|
|
{
|
|
|
|
let mut value = self.0 .1.write().unwrap();
|
|
|
|
f(&mut value);
|
|
|
|
self.0 .0.save(&value)
|
|
|
|
}
|
|
|
|
}
|