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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//! Vault lifecycle management tools
//!
//! Manages vault creation, registration, and switching via MultiVaultManager.
//! Provides MCP tools for users to create new vaults or register existing ones.
use std::path::{Path, PathBuf};
use std::sync::Arc;
use turbovault_core::prelude::*;
/// Vault lifecycle operations
pub struct VaultLifecycleTools {
/// Multi-vault manager for registration/switching
multi_manager: Arc<MultiVaultManager>,
}
impl VaultLifecycleTools {
/// Create new lifecycle tools
pub fn new(multi_manager: Arc<MultiVaultManager>) -> Self {
Self { multi_manager }
}
/// Create a new vault at the specified path
///
/// # Arguments
/// - `name`: Unique vault identifier (no spaces)
/// - `path`: Directory to create vault in (supports tilde expansion)
/// - `template`: Optional template name ("default", "research", "team")
///
/// # Returns
/// VaultInfo with the created vault details (includes fully resolved path)
///
/// # Errors
/// - Invalid name (empty, spaces)
/// - Path I/O errors
/// - Vault already registered
pub async fn create_vault(
&self,
name: &str,
path: &Path,
template: Option<&str>,
) -> Result<VaultInfo> {
// Validation: name format
if name.is_empty() {
return Err(Error::config_error(
"Vault name cannot be empty".to_string(),
));
}
if name.contains(' ') {
return Err(Error::config_error(
"Vault name cannot contain spaces".to_string(),
));
}
if name.len() > 64 {
return Err(Error::config_error(
"Vault name too long (max 64 chars)".to_string(),
));
}
// Check if already registered
if self.multi_manager.vault_exists(name).await {
return Err(Error::config_error(format!(
"Vault '{}' already registered",
name
)));
}
// Expand tilde and convert to absolute path
let expanded_path = Self::expand_path(path)?;
// If path doesn't exist, create it
if !expanded_path.exists() {
tokio::fs::create_dir_all(&expanded_path)
.await
.map_err(|e| {
Error::config_error(format!("Failed to create vault directory: {}", e))
})?;
}
if !expanded_path.is_dir() {
return Err(Error::invalid_path(format!(
"Path is not a directory: {}",
expanded_path.display()
)));
}
// Create .obsidian directory (marks directory as Obsidian vault)
let obsidian_dir = expanded_path.join(".obsidian");
tokio::fs::create_dir_all(&obsidian_dir)
.await
.map_err(|e| {
Error::config_error(format!("Failed to create .obsidian directory: {}", e))
})?;
// Initialize template structure if specified
if let Some(tmpl) = template {
self.initialize_template_structure(&expanded_path, tmpl)
.await?;
} else {
// Create default structure
self.initialize_default_structure(&expanded_path).await?;
}
// Create vault configuration (uses expanded path)
let config = VaultConfig::builder(name, &expanded_path).build()?;
// Register with multi-vault manager
self.multi_manager.add_vault(config).await?;
// Return vault info
self.get_vault_info(name).await
}
/// Add an existing vault (path must exist and be a directory)
///
/// # Arguments
/// - `name`: Unique vault identifier
/// - `path`: Existing vault directory path (supports tilde expansion)
///
/// # Returns
/// VaultInfo with the registered vault details
pub async fn add_vault_from_path(&self, name: &str, path: &Path) -> Result<VaultInfo> {
// Validation: name format
if name.is_empty() || name.contains(' ') {
return Err(Error::config_error(
"Invalid vault name (cannot be empty or contain spaces)".to_string(),
));
}
// Check if already registered
if self.multi_manager.vault_exists(name).await {
return Err(Error::config_error(format!(
"Vault '{}' already registered",
name
)));
}
// Expand tilde and convert to absolute path
let expanded_path = Self::expand_path(path)?;
// Create the directory if it doesn't exist
if !expanded_path.exists() {
std::fs::create_dir_all(&expanded_path).map_err(|e| {
Error::invalid_path(format!(
"Path does not exist and could not be created: {} ({})",
expanded_path.display(),
e
))
})?;
}
if !expanded_path.is_dir() {
return Err(Error::invalid_path(format!(
"Path is not a directory: {}",
expanded_path.display()
)));
}
// Create vault config
let config = VaultConfig::builder(name, &expanded_path).build()?;
// Register with multi-vault manager
self.multi_manager.add_vault(config).await?;
// Return vault info
self.get_vault_info(name).await
}
/// List all registered vaults
pub async fn list_vaults(&self) -> Result<Vec<VaultInfo>> {
self.multi_manager.list_vaults().await
}
/// Get configuration for a specific vault
pub async fn get_vault_config(&self, name: &str) -> Result<VaultConfig> {
self.multi_manager.get_vault_config(name).await
}
/// Get the currently active vault
pub async fn get_active_vault(&self) -> Result<String> {
Ok(self.multi_manager.get_active_vault().await)
}
/// Set the active vault (all subsequent operations use this vault)
pub async fn set_active_vault(&self, name: &str) -> Result<()> {
self.multi_manager.set_active_vault(name).await
}
/// Remove a vault from registration (cannot remove active vault)
pub async fn remove_vault(&self, name: &str) -> Result<()> {
self.multi_manager.remove_vault(name).await
}
/// Validate that a vault directory is properly formatted
///
/// Checks for:
/// - .obsidian directory exists
/// - Has readable permissions
/// - Contains expected structure
pub async fn validate_vault(&self, name: &str) -> Result<serde_json::Value> {
let vault_info = self
.multi_manager
.list_vaults()
.await?
.into_iter()
.find(|v| v.name == name)
.ok_or_else(|| Error::not_found(format!("Vault '{}' not found", name)))?;
let mut issues = Vec::new();
let mut is_valid = true;
// Check .obsidian directory
let obsidian_dir = vault_info.path.join(".obsidian");
if !obsidian_dir.exists() {
issues.push("Missing .obsidian directory".to_string());
is_valid = false;
}
// Check readable
match tokio::fs::metadata(&vault_info.path).await {
Ok(meta) => {
if !meta.is_dir() {
issues.push("Vault path is not a directory".to_string());
is_valid = false;
}
}
Err(e) => {
issues.push(format!("Cannot access vault: {}", e));
is_valid = false;
}
}
Ok(serde_json::json!({
"vault": name,
"path": vault_info.path.display().to_string(),
"is_valid": is_valid,
"issues": issues,
}))
}
// Private helpers
/// Expand tilde and environment variables in path, then canonicalize to absolute path
///
/// Uses shellexpand for tilde/env expansion (best-in-class library)
fn expand_path(path: &Path) -> Result<PathBuf> {
let path_str = path
.to_str()
.ok_or_else(|| Error::invalid_path("Path contains invalid UTF-8".to_string()))?;
// Expand tilde and environment variables
let expanded = shellexpand::full(path_str)
.map_err(|e| Error::invalid_path(format!("Failed to expand path: {}", e)))?;
let expanded_path = PathBuf::from(expanded.as_ref());
// Convert to absolute path (canonicalize if exists, otherwise resolve relative to cwd)
if expanded_path.exists() {
// Canonicalize to absolute path
expanded_path
.canonicalize()
.map_err(|e| Error::invalid_path(format!("Failed to resolve path: {}", e)))
} else {
// Path doesn't exist yet - make it absolute relative to current directory
if expanded_path.is_absolute() {
Ok(expanded_path)
} else {
std::env::current_dir()
.map(|cwd| cwd.join(&expanded_path))
.map_err(|e| {
Error::invalid_path(format!("Failed to get current directory: {}", e))
})
}
}
}
async fn get_vault_info(&self, name: &str) -> Result<VaultInfo> {
let vaults = self.multi_manager.list_vaults().await?;
vaults
.into_iter()
.find(|v| v.name == name)
.ok_or_else(|| Error::not_found(format!("Vault '{}' not found", name)))
}
async fn initialize_default_structure(&self, path: &Path) -> Result<()> {
// Create standard Obsidian directory structure
let dirs = ["Areas", "Projects", "Resources", "Archive"];
for dir in &dirs {
tokio::fs::create_dir_all(path.join(dir))
.await
.map_err(|e| {
Error::config_error(format!("Failed to create {} directory: {}", dir, e))
})?;
}
// Create README.md
let vault_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("Vault");
let readme_content = format!(
"# {}\n\nWelcome to your Obsidian vault!\n\n## Structure\n\n- **Areas**: Spaces of activities and responsibilities\n- **Projects**: Short-term efforts\n- **Resources**: Reference material\n- **Archive**: Completed or inactive items\n",
vault_name
);
tokio::fs::write(path.join("README.md"), readme_content)
.await
.map_err(|e| Error::config_error(format!("Failed to create README.md: {}", e)))?;
Ok(())
}
async fn initialize_template_structure(&self, path: &Path, template: &str) -> Result<()> {
match template {
"default" => self.initialize_default_structure(path).await,
"research" => {
// Research-specific structure
let dirs = ["Literature", "Theory", "Findings", "Hypotheses"];
for dir in &dirs {
tokio::fs::create_dir_all(path.join(dir))
.await
.map_err(|e| {
Error::config_error(format!(
"Failed to create {} directory: {}",
dir, e
))
})?;
}
Ok(())
}
"team" => {
// Team collaboration structure
let dirs = ["Team", "Projects", "Decisions", "Documentation"];
for dir in &dirs {
tokio::fs::create_dir_all(path.join(dir))
.await
.map_err(|e| {
Error::config_error(format!(
"Failed to create {} directory: {}",
dir, e
))
})?;
}
Ok(())
}
_ => Err(Error::config_error(format!(
"Unknown template: {} (supported: default, research, team)",
template
))),
}
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_placeholder() {
// Tests are in integration tests file
// This module is kept for future unit tests
}
}