paths: remove hardcoded basepath

Before we were hardcoding the basepath with dirs, which isn't that
useful for testing, previews, or for android. Let's fix that.

This also moves the db and cache directories into our root DataPaths.
This is a breaking change, we don't have a migration step. sorry.

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2024-11-12 14:00:57 -08:00
parent 6c9693dbf0
commit fab1257f6e
11 changed files with 167 additions and 144 deletions

View File

@@ -8,33 +8,45 @@ use std::{
use crate::Error;
pub enum DataPaths {
#[derive(Debug, Clone)]
pub struct DataPath {
base: PathBuf,
}
impl DataPath {
pub fn new(base: impl AsRef<Path>) -> Self {
let base = base.as_ref().to_path_buf();
Self { base }
}
pub fn default_base() -> Option<PathBuf> {
dirs::data_local_dir().map(|pb| pb.join("notedeck"))
}
}
pub enum DataPathType {
Log,
Setting,
Keys,
SelectedKey,
Db,
Cache,
}
impl DataPaths {
pub fn get_path(&self) -> Result<PathBuf, Error> {
let base_path = match self {
DataPaths::Log => dirs::data_local_dir(),
DataPaths::Setting | DataPaths::Keys | DataPaths::SelectedKey => {
dirs::config_local_dir()
}
impl DataPath {
pub fn rel_path(&self, typ: DataPathType) -> PathBuf {
match typ {
DataPathType::Log => PathBuf::from("logs"),
DataPathType::Setting => PathBuf::from("settings"),
DataPathType::Keys => PathBuf::from("storage").join("accounts"),
DataPathType::SelectedKey => PathBuf::from("storage").join("selected_account"),
DataPathType::Db => PathBuf::from("db"),
DataPathType::Cache => PathBuf::from("cache"),
}
.ok_or(Error::Generic(
"Could not open well known OS directory".to_owned(),
))?;
}
let specific_path = match self {
DataPaths::Log => PathBuf::from("logs"),
DataPaths::Setting => PathBuf::from("settings"),
DataPaths::Keys => PathBuf::from("storage").join("accounts"),
DataPaths::SelectedKey => PathBuf::from("storage").join("selected_account"),
};
Ok(base_path.join("notedeck").join(specific_path))
pub fn path(&self, typ: DataPathType) -> PathBuf {
self.base.join(self.rel_path(typ))
}
}

View File

@@ -3,8 +3,8 @@ mod file_storage;
pub use file_key_storage::FileKeyStorage;
pub use file_storage::write_file;
pub use file_storage::DataPaths;
pub use file_storage::Directory;
pub use file_storage::{DataPath, DataPathType};
#[cfg(target_os = "macos")]
mod security_framework_key_storage;