Mountain/ApplicationState/Internal/PathResolution/ResolveMementoStorageFilePath.rs
1//! # ResolveMementoStorageFilePath Module (Internal)
2//!
3//! ## RESPONSIBILITIES
4//! Resolves the absolute path for a Memento storage file based on scope.
5//! Handles both global and workspace-scoped memento paths with proper
6//! sanitization.
7//!
8//! ## ARCHITECTURAL ROLE
9//! ResolveMementoStorageFilePath is part of the **Internal::PathResolution**
10//! module, resolving memento storage file paths.
11//!
12//! ## KEY COMPONENTS
13//! - ResolveMementoStorageFilePath: Function to resolve memento paths
14//!
15//! ## ERROR HANDLING
16//! - Sanitizes workspace identifiers to be filesystem-safe
17//! - Uses alphanumeric, hyphens, and underscores only
18//!
19//! ## LOGGING
20//! Operations are logged at appropriate levels (debug).
21//!
22//! ## PERFORMANCE CONSIDERATIONS
23//! - Efficient path manipulation
24//! - Sanitization prevents filesystem issues
25//!
26//! ## TODO
27//! - [ ] Add path validation
28//! - [ ] Implement path normalization
29//! - [ ] Add cross-platform path handling
30
31use std::path::Path;
32
33use log::debug;
34
35/// Resolves the absolute path for a Memento storage file based on scope.
36///
37/// # Arguments
38/// * `ApplicationDataDirectory` - Base application data directory
39/// * `IsGlobalScope` - True for global storage, false for workspace storage
40/// * `WorkspaceIdentifier` - Workspace identifier (ignored for global scope)
41///
42/// # Returns
43/// PathBuf pointing to the memento storage file
44///
45/// # Behavior
46/// - Global scope: `{AppData}/User/globalStorage.json`
47/// - Workspace scope:
48/// `{AppData}/User/workspaceStorage/{sanitized-id}/storage.json`
49/// - Sanitizes workspace identifier (alphanumeric, hyphens, underscores only)
50pub fn ResolveMementoStorageFilePath(
51 ApplicationDataDirectory:&Path,
52 IsGlobalScope:bool,
53 WorkspaceIdentifier:&str,
54) -> std::path::PathBuf {
55 let user_storage_base_path = ApplicationDataDirectory.join("User");
56
57 if IsGlobalScope {
58 let path = user_storage_base_path.join("globalStorage.json");
59 debug!(
60 "[ResolveMementoStorageFilePath] Resolved global memento path: {}",
61 path.display()
62 );
63 path
64 } else {
65 // Sanitize the workspace identifier to be a safe directory name
66 let segment = WorkspaceIdentifier.replace(|c:char| !c.is_alphanumeric() && c != '-' && c != '_', "_");
67
68 let path = user_storage_base_path
69 .join("workspaceStorage")
70 .join(&segment)
71 .join("storage.json");
72
73 debug!(
74 "[ResolveMementoStorageFilePath] Resolved workspace memento path: {}",
75 path.display()
76 );
77
78 path
79 }
80}