stakpak_shared/
local_store.rs1use std::{fs, path::PathBuf};
2
3pub struct LocalStore {}
4
5impl LocalStore {
6 pub fn get_local_session_store_path() -> PathBuf {
7 std::env::current_dir()
8 .unwrap_or_else(|_| PathBuf::from("."))
9 .join(".stakpak")
10 .join("session")
11 }
12
13 pub fn get_backup_session_path(session_id: &str) -> PathBuf {
14 PathBuf::from("backups").join(session_id)
15 }
16
17 pub fn write_session_data(path: &str, data: &str) -> Result<String, String> {
18 let session_dir = Self::get_local_session_store_path();
19 if !session_dir.exists() {
20 std::fs::create_dir_all(&session_dir)
21 .map_err(|e| format!("Failed to create session directory: {}", e))?;
22 }
23
24 let full_path = Self::get_local_session_store_path().join(path);
25
26 if let Some(parent_dir) = full_path.parent()
28 && !parent_dir.exists()
29 {
30 std::fs::create_dir_all(parent_dir).map_err(|e| {
31 format!(
32 "Failed to create parent directory {}: {}",
33 parent_dir.display(),
34 e
35 )
36 })?;
37 }
38
39 std::fs::write(&full_path, data).map_err(|e| {
40 format!(
41 "Failed to write session data to {}: {}",
42 full_path.display(),
43 e
44 )
45 })?;
46 Ok(full_path.to_string_lossy().to_string())
47 }
48
49 pub fn read_session_data(path: &str) -> Result<String, String> {
50 let path = Self::get_local_session_store_path().join(path);
51 fs::read_to_string(&path)
52 .map_err(|e| format!("Failed to read session data from {}: {}", path.display(), e))
53 }
54}