fetchium_core/collab/
workspace.rs1use crate::error::FetchiumError;
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct Workspace {
8 pub id: String,
9 pub name: String,
10 pub created_at: String,
11 pub members: Vec<String>,
12 pub sync_method: SyncMethod,
13}
14
15#[derive(Debug, Clone, Serialize, Deserialize)]
16#[serde(tag = "type", rename_all = "snake_case")]
17pub enum SyncMethod {
18 Local { shared_dir: std::path::PathBuf },
19 Git { remote_url: String },
20}
21
22impl Workspace {
23 pub fn create(name: &str, path: &std::path::Path) -> Result<Self, FetchiumError> {
25 let ws = Self {
26 id: uuid::Uuid::new_v4().to_string(),
27 name: name.to_string(),
28 created_at: chrono::Utc::now().to_rfc3339(),
29 members: vec![],
30 sync_method: SyncMethod::Local {
31 shared_dir: path.to_path_buf(),
32 },
33 };
34 std::fs::create_dir_all(path.join("sessions"))?;
35 std::fs::create_dir_all(path.join("knowledge_graph"))?;
36 std::fs::create_dir_all(path.join("evidence"))?;
37 let manifest = serde_json::to_string_pretty(&ws)?;
38 std::fs::write(path.join("workspace.json"), manifest)?;
39 tracing::info!(name, "Workspace created at {:?}", path);
40 Ok(ws)
41 }
42
43 pub fn load(path: &std::path::Path) -> Result<Self, FetchiumError> {
45 let content = std::fs::read_to_string(path.join("workspace.json"))?;
46 Ok(serde_json::from_str(&content)?)
47 }
48
49 pub fn list(base_dir: &std::path::Path) -> Result<Vec<Self>, FetchiumError> {
51 if !base_dir.exists() {
52 return Ok(vec![]);
53 }
54 let mut workspaces = Vec::new();
55 for entry in std::fs::read_dir(base_dir)? {
56 let path = entry?.path();
57 if path.is_dir() && path.join("workspace.json").exists() {
58 if let Ok(ws) = Self::load(&path) {
59 workspaces.push(ws);
60 }
61 }
62 }
63 workspaces.sort_by(|a, b| a.name.cmp(&b.name));
64 Ok(workspaces)
65 }
66
67 pub fn default_base_dir() -> std::path::PathBuf {
69 dirs::home_dir()
70 .unwrap_or_else(|| std::path::PathBuf::from("."))
71 .join(".fetchium")
72 .join("workspaces")
73 }
74
75 pub fn path_for(name: &str) -> std::path::PathBuf {
77 Self::default_base_dir().join(name)
78 }
79}