aboutsummaryrefslogtreecommitdiff
path: root/src/util/persister.rs
blob: 5c66bbed75a2428578518389487ffbee69d7d550 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};

use tokio::io::{AsyncReadExt, AsyncWriteExt};

use crate::error::Error;
use crate::migrate::Migrate;

pub struct Persister<T: Migrate> {
	path: PathBuf,

	_marker: std::marker::PhantomData<T>,
}

impl<T: Migrate> Persister<T> {
	pub fn new(base_dir: &Path, file_name: &str) -> Self {
		let mut path = base_dir.to_path_buf();
		path.push(file_name);
		Self {
			path,
			_marker: Default::default(),
		}
	}

	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()
				)))
			}
		}
	}

	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)?;

		let value = self.decode(&bytes[..])?;
		Ok(value)
	}

	pub fn save(&self, t: &T) -> Result<(), Error> {
		let bytes = t.encode()?;

		let mut file = std::fs::OpenOptions::new()
			.write(true)
			.create(true)
			.truncate(true)
			.open(&self.path)?;

		file.write_all(&bytes[..])?;

		Ok(())
	}

	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?;

		let value = self.decode(&bytes[..])?;
		Ok(value)
	}

	pub async fn save_async(&self, t: &T) -> Result<(), Error> {
		let bytes = t.encode()?;

		let mut file = tokio::fs::File::create(&self.path).await?;
		file.write_all(&bytes[..]).await?;

		Ok(())
	}
}

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)
	}
}