Skip to main content

kimun_notes/settings/
workspace_config.rs

1use chrono::{DateTime, Utc};
2use kimun_core::nfs::filename::{InvalidFilenameError, validate_filename};
3use serde::{Deserialize, Serialize};
4use std::collections::BTreeMap;
5use std::path::PathBuf;
6
7#[derive(Debug, Clone)]
8pub enum WorkspaceConfigError {
9    DuplicateWorkspace {
10        name: String,
11        existing_path: PathBuf,
12    },
13    InvalidName {
14        name: String,
15        error: InvalidFilenameError,
16    },
17}
18
19impl std::fmt::Display for WorkspaceConfigError {
20    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21        match self {
22            WorkspaceConfigError::DuplicateWorkspace {
23                name,
24                existing_path,
25            } => {
26                write!(
27                    f,
28                    "Workspace '{}' already exists at {:?}",
29                    name, existing_path
30                )
31            }
32            WorkspaceConfigError::InvalidName { error, .. } => {
33                write!(f, "Workspace {error}")
34            }
35        }
36    }
37}
38
39impl std::error::Error for WorkspaceConfigError {}
40
41#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
42pub struct GlobalConfig {
43    pub current_workspace: String,
44}
45
46#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
47pub struct WorkspaceEntry {
48    pub path: PathBuf,
49    #[serde(default, skip_serializing)]
50    pub last_paths: Vec<String>,
51    pub created: DateTime<Utc>,
52    #[serde(default)]
53    pub quick_note_path: Option<String>,
54    #[serde(default)]
55    pub inbox_path: Option<String>,
56    /// Absolute resolved path for runtime use. Not serialized — `path` is
57    /// written to disk as the user configured it (relative, ~/..., or absolute).
58    #[serde(skip)]
59    pub resolved_path: Option<PathBuf>,
60}
61
62impl WorkspaceEntry {
63    /// Returns the resolved absolute path if available, otherwise the original path.
64    pub fn effective_path(&self) -> &PathBuf {
65        self.resolved_path.as_ref().unwrap_or(&self.path)
66    }
67
68    pub fn effective_quick_note_path(&self) -> String {
69        self.quick_note_path
70            .clone()
71            .unwrap_or_else(|| kimun_core::nfs::VaultPath::root().to_string())
72    }
73
74    pub fn effective_inbox_path(&self) -> String {
75        self.inbox_path
76            .clone()
77            .unwrap_or_else(|| kimun_core::DEFAULT_INBOX_PATH.to_string())
78    }
79}
80
81#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
82pub struct WorkspaceConfig {
83    pub global: GlobalConfig,
84    /// Keyed by workspace name. `BTreeMap` (not `HashMap`) so serialization
85    /// order is deterministic — otherwise every config save reshuffles the
86    /// `[workspaces.*]` sections in the TOML file.
87    pub workspaces: BTreeMap<String, WorkspaceEntry>,
88}
89
90impl WorkspaceConfig {
91    pub fn new_empty() -> Self {
92        Self {
93            global: GlobalConfig {
94                current_workspace: String::new(),
95            },
96            workspaces: BTreeMap::new(),
97        }
98    }
99
100    pub fn add_workspace(
101        &mut self,
102        name: String,
103        path: PathBuf,
104    ) -> Result<(), WorkspaceConfigError> {
105        if let Err(error) = validate_filename(&name) {
106            return Err(WorkspaceConfigError::InvalidName {
107                name: name.clone(),
108                error,
109            });
110        }
111        if self.workspaces.contains_key(&name) {
112            return Err(WorkspaceConfigError::DuplicateWorkspace {
113                name: name.clone(),
114                existing_path: self.workspaces[&name].path.clone(),
115            });
116        }
117
118        let entry = WorkspaceEntry {
119            path,
120            last_paths: Vec::new(),
121            created: Utc::now(),
122            quick_note_path: None,
123            inbox_path: None,
124            resolved_path: None,
125        };
126
127        self.workspaces.insert(name.clone(), entry);
128
129        // Set as current if it's the first workspace
130        if self.workspaces.len() == 1 {
131            self.global.current_workspace = name.clone();
132        }
133
134        Ok(())
135    }
136
137    pub fn get_current_workspace(&self) -> Option<&WorkspaceEntry> {
138        self.workspaces.get(&self.global.current_workspace)
139    }
140
141    pub fn get_workspace(&self, name: &str) -> Option<&WorkspaceEntry> {
142        self.workspaces.get(name)
143    }
144
145    pub fn from_phase1_migration(workspace_dir: PathBuf, last_paths: Vec<String>) -> Self {
146        let mut config = Self::new_empty();
147
148        let entry = WorkspaceEntry {
149            path: workspace_dir,
150            last_paths,
151            created: Utc::now(),
152            quick_note_path: None,
153            inbox_path: None,
154            resolved_path: None,
155        };
156
157        config.workspaces.insert("default".to_string(), entry);
158        config.global.current_workspace = "default".to_string();
159
160        config
161    }
162}
163
164#[cfg(test)]
165mod validate_tests {
166    use super::*;
167
168    #[test]
169    fn add_workspace_rejects_disallowed_chars() {
170        let mut wc = WorkspaceConfig::new_empty();
171        let err = wc
172            .add_workspace("bad/name".to_string(), PathBuf::from("/tmp/x"))
173            .unwrap_err();
174        match err {
175            WorkspaceConfigError::InvalidName { name, .. } => assert_eq!(name, "bad/name"),
176            _ => panic!("expected InvalidName"),
177        }
178    }
179
180    #[test]
181    fn add_workspace_rejects_windows_reserved() {
182        let mut wc = WorkspaceConfig::new_empty();
183        assert!(
184            wc.add_workspace("con".to_string(), PathBuf::from("/tmp/x"))
185                .is_err()
186        );
187    }
188
189    #[test]
190    fn add_workspace_accepts_simple_names() {
191        let mut wc = WorkspaceConfig::new_empty();
192        assert!(
193            wc.add_workspace("notes".to_string(), PathBuf::from("/tmp/x"))
194                .is_ok()
195        );
196    }
197}