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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
use ron::ser::PrettyConfig;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use std::fs::{self, File, OpenOptions};
use std::io::{Read, Write};
use std::path::PathBuf;
use thiserror::Error;
use log::debug;
use log::info;
use log::log;
use log::warn;
#[derive(Error, Debug)]
pub enum StoreError {
#[error("RON parsing error: {0}")]
RonParse(#[source] ron::error::SpannedError),
#[error("RON error: {0}")]
Ron(#[source] ron::error::Error),
#[error("Failed to open file: {0}")]
FileOpen(#[source] std::io::Error),
#[error("Failed to read from file: {0}")]
Read(#[source] std::io::Error),
#[error("Failed to create directory: {0}")]
CreateDir(#[source] std::io::Error),
#[error("Failed to write to file: {0}")]
Write(#[source] std::io::Error),
}
#[derive(Debug, Default)]
pub enum StoringType {
Cache,
#[default]
Data,
Config,
}
pub trait Storing: Clone + Serialize + for<'de> Deserialize<'de> + Debug + Default {
fn store_type() -> StoringType;
}
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]
pub struct StoreHandle<T> {
store_id: String,
store: T,
}
impl<T: Storing> StoreHandle<T> {
pub fn new(store_id: &str) -> Self {
Self {
store: T::default(),
store_id: store_id.to_owned(),
}
}
fn set_store(&mut self, store: T) {
debug!("Setting store with id: {}", self.store_id);
self.store = store;
}
/// Returns a mutable reference to the stored data.
pub fn get_store_mut(&mut self) -> &mut T {
&mut self.store
}
/// Returns a reference to the stored data.
pub fn get_store(&self) -> &T {
&self.store
}
pub fn store_id(&self) -> &str {
&self.store_id
}
}
/// Handles file system paths for reading from and writing to data storage.
///
/// The `Storage` struct provides a way to manage file paths used for storing data in different locations.
/// It simplifies the process of accessing and modifying data by providing methods for these operations.
///
/// # Example
///
/// ```
/// use serde::{Deserialize, Serialize};
/// use storage::{Storage, StoreHandle, Storing, StoringType};
///
/// #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
/// pub struct MyStore {
/// pub count: u32,
/// }
///
/// impl MyStore {
/// fn increment_count(&mut self) {
/// self.count += 1;
/// }
/// }
///
/// impl Storing for MyStore {
/// fn store_type() -> StoringType {
/// StoringType::Data
/// }
/// }
///
/// // Initialize the Storage with the defaults
/// let storage = Storage::new("com.github.mazynoah.storage".to_owned());
///
/// // Create a handle for managing the store data.
/// let mut handle = StoreHandle::<MyStore>::new("handle");
///
/// // Read existing store from storage
/// storage
/// .read(&mut handle)
/// .expect("Failed to read from storage");
///
/// // Modify the store data
/// let counter = handle.get_store_mut();
///
/// counter.increment_count();
/// counter.increment_count();
/// counter.increment_count();
///
/// // Write changes to disk
/// storage
/// .write(&mut handle)
/// .expect("Failed to write to storage");
///
/// let counter = handle.get_store();
///
/// println!("Count: {}", counter.count);
///
///
/// ```
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]
pub struct Storage {
cache_dir: PathBuf,
data_dir: PathBuf,
config_dir: PathBuf,
}
impl Storage {
/// Creates a new `Storage` instance by obtaining the paths for cache, data, and configuration directories.
///
/// # Panics
///
/// - Panics if the cache directory, data directory, or configuration directory path cannot be determined.
pub fn new(app_id: String) -> Self {
Self {
data_dir: dirs::data_dir()
.expect("Failed to determine cache directory path")
.join(&app_id),
config_dir: dirs::config_dir()
.expect("Failed to determine data directory path")
.join(&app_id),
cache_dir: dirs::cache_dir()
.expect("Failed to determine configuration directory path")
.join(&app_id),
}
}
/// Creates a new `Storage` instance by obtaining the paths for cache, data, and configuration directories.
pub fn from(cache_dir: PathBuf, data_dir: PathBuf, config_dir: PathBuf) -> Self {
Self {
cache_dir,
data_dir,
config_dir,
}
}
/// Reads the store from a file and updates the provided `StoreHandle`.
/// If the file does not exist, it creates a default store if a default is available.
///
/// # Example
///
/// ```
/// let storage = Storage::new(app);
/// let handle: StoreHandle<MyStore> = StoreHandle::new("my_store_id");
///
/// storage.read(&mut handle).expect("Failed to read store");
///
/// ```
pub fn read<T: Storing>(&self, handle: &mut StoreHandle<T>) -> Result<(), StoreError> {
debug!("Reading store with id: {}", handle.store_id());
self.open_file::<T, _>(
|file, handle| {
let store = Self::read_string(file).map_err(StoreError::Read)?;
let store_data: T = ron::from_str(&store).map_err(StoreError::RonParse)?;
handle.set_store(store_data);
info!("Successfully read store with id: {}", handle.store_id());
Ok(())
},
handle,
)
}
/// Writes the current store `T` from the provided `StoreHandle` to a file.
/// If the file does not exist, it creates a default store if a default is available.
///
/// # Example
///
/// ```
/// let storage = Storage::new(app);
/// let handle: StoreHandle<MyStore> = StoreHandle::new("my_store_id");
///
/// storage.write(&mut handle).expect("Failed to read store");
///
/// ```
pub fn write<T: Storing>(&self, handle: &mut StoreHandle<T>) -> Result<(), StoreError> {
debug!("Writing store with id: {}", handle.store_id());
self.open_file::<T, _>(
|file: &mut File, handle| {
let store = handle.get_store_mut();
let str =
ron::ser::to_string_pretty(&store, PrettyConfig::new().compact_arrays(true))
.map_err(StoreError::Ron)?;
file.write(str.as_bytes()).map_err(StoreError::Write)?;
info!("Successfully wrote store with id: {}", handle.store_id());
Ok(())
},
handle,
)
}
/// Opens the file for reading or writing. If the file does not exist, it attempts
/// to create a default store if a default is provided.
///
/// # Example
///
/// ```rust
/// storage.open_file::<MyStore, _>(|file, handle| {
/// // Perform file operations
/// Ok(())
/// }, &mut handle);
/// ```
fn open_file<T, F>(
&self,
mut operation: F,
handle: &mut StoreHandle<T>,
) -> Result<(), StoreError>
where
T: Storing,
F: FnMut(&mut File, &mut StoreHandle<T>) -> Result<(), StoreError>,
{
let mut dir_path = self.dir_path::<T>();
dir_path.push(handle.store_id()); // i don't like this
debug!("Opening file at path: {:?}", dir_path);
match OpenOptions::new()
.read(true)
.write(true)
.create(false)
.truncate(false)
.open(&dir_path)
{
Ok(mut config) => {
debug!("File opened successfully at path: {:?}", dir_path);
operation(&mut config, handle)
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
warn!(
"File not found at path: {:?}, creating default store",
dir_path
);
self.store_default::<T>(dir_path)?;
self.open_file(operation, handle)
}
Err(err) => {
warn!(
"Failed to open file at path: {:?}, error: {:?}",
dir_path, err
);
Err(StoreError::FileOpen(err))
}
}
}
fn store_default<T: Storing>(&self, path: PathBuf) -> Result<(), StoreError> {
debug!("Storing default configuration at path: {:?}", path);
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).map_err(StoreError::CreateDir)?;
info!("Created directory for path: {:?}", parent);
}
let default_store = T::default();
let str = ron::ser::to_string_pretty(&default_store, PrettyConfig::new())
.map_err(StoreError::Ron)?;
fs::write(&path, str).map_err(StoreError::Write)?;
info!("Default store written at path: {:?}", &path);
Ok(())
}
fn dir_path<T: Storing>(&self) -> PathBuf {
let path = match T::store_type() {
StoringType::Cache => self.cache_dir.clone(),
StoringType::Data => self.data_dir.clone(),
StoringType::Config => self.config_dir.clone(),
};
debug!(
"Resolved directory path for store type: {:?} to path: {:?}",
T::store_type(),
path
);
path
}
fn read_string(mut file: &File) -> Result<String, std::io::Error> {
let mut buf = String::new();
file.read_to_string(&mut buf)?;
debug!("Read string from file, length: {}", buf.len());
Ok(buf)
}
}