Skip to content

Commit 9fc2886

Browse files
committed
extract settings_base.rs, moving to vault design
1 parent e1857a7 commit 9fc2886

File tree

4 files changed

+42
-19
lines changed

4 files changed

+42
-19
lines changed

plugins/settings/src/ext.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use std::path::PathBuf;
22

33
use crate::content_base;
44
use crate::obsidian::ObsidianVault;
5-
6-
pub const FILENAME: &str = "settings.json";
5+
use crate::settings_base;
76

87
pub struct Settings<'a, R: tauri::Runtime, M: tauri::Manager<R>> {
98
manager: &'a M,
@@ -13,19 +12,8 @@ pub struct Settings<'a, R: tauri::Runtime, M: tauri::Manager<R>> {
1312
impl<'a, R: tauri::Runtime, M: tauri::Manager<R>> Settings<'a, R, M> {
1413
pub fn default_base(&self) -> Result<PathBuf, crate::Error> {
1514
let bundle_id: &str = self.manager.config().identifier.as_ref();
16-
let data_dir = self
17-
.manager
18-
.path()
19-
.data_dir()
20-
.map_err(|_| crate::Error::DataDirUnavailable)?;
21-
22-
let app_folder = if cfg!(debug_assertions) || bundle_id == "com.hyprnote.staging" {
23-
bundle_id
24-
} else {
25-
"hyprnote"
26-
};
27-
28-
let path = data_dir.join(app_folder);
15+
let path = settings_base::compute_default_base(bundle_id)
16+
.ok_or(crate::Error::DataDirUnavailable)?;
2917
std::fs::create_dir_all(&path)?;
3018
Ok(path)
3119
}
@@ -36,7 +24,7 @@ impl<'a, R: tauri::Runtime, M: tauri::Manager<R>> Settings<'a, R, M> {
3624

3725
pub fn settings_path(&self) -> Result<PathBuf, crate::Error> {
3826
let base = self.settings_base()?;
39-
Ok(base.join(FILENAME))
27+
Ok(settings_base::compute_settings_path(&base))
4028
}
4129

4230
pub fn content_base(&self) -> Result<PathBuf, crate::Error> {
@@ -89,7 +77,7 @@ impl<'a, R: tauri::Runtime, M: tauri::Manager<R> + tauri::Emitter<R>> Settings<'
8977
std::fs::create_dir_all(&new_path)?;
9078
crate::fs::copy_content_items(&old_content_base, &new_path).await?;
9179

92-
let settings_path = default_base.join(FILENAME);
80+
let settings_path = settings_base::compute_settings_path(&default_base);
9381
let existing_json = std::fs::read_to_string(&settings_path).ok();
9482
let content = content_base::prepare_settings_json_for_content_base(
9583
existing_json.as_deref(),

plugins/settings/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod error;
66
mod ext;
77
mod fs;
88
mod obsidian;
9+
mod settings_base;
910
mod state;
1011

1112
pub use error::{Error, Result};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use std::path::{Path, PathBuf};
2+
3+
pub const FILENAME: &str = "settings.json";
4+
5+
pub fn compute_default_base(bundle_id: &str) -> Option<PathBuf> {
6+
let data_dir = dirs::data_dir()?;
7+
let app_folder = resolve_app_folder(bundle_id);
8+
Some(data_dir.join(app_folder))
9+
}
10+
11+
pub fn compute_settings_path(base: &Path) -> PathBuf {
12+
base.join(FILENAME)
13+
}
14+
15+
fn resolve_app_folder(bundle_id: &str) -> &str {
16+
if cfg!(debug_assertions) || bundle_id == "com.hyprnote.staging" {
17+
bundle_id
18+
} else {
19+
"hyprnote"
20+
}
21+
}
22+
23+
#[cfg(test)]
24+
mod tests {
25+
use super::*;
26+
27+
#[test]
28+
fn resolve_app_folder_returns_bundle_id_for_staging() {
29+
assert_eq!(
30+
resolve_app_folder("com.hyprnote.staging"),
31+
"com.hyprnote.staging"
32+
);
33+
}
34+
}

plugins/settings/src/state.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::path::PathBuf;
22
use tokio::sync::RwLock;
33

4-
use crate::ext::FILENAME;
4+
use crate::settings_base;
55

66
pub struct State {
77
settings_base: PathBuf,
@@ -19,7 +19,7 @@ impl State {
1919
}
2020

2121
fn path(&self) -> PathBuf {
22-
self.settings_base.join(FILENAME)
22+
settings_base::compute_settings_path(&self.settings_base)
2323
}
2424

2525
pub fn content_base(&self) -> &PathBuf {

0 commit comments

Comments
 (0)