1use std::collections::HashMap;
7use std::path::PathBuf;
8
9use uuid::Uuid;
10
11use crate::document::Document;
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
15pub struct DocumentId(Uuid);
16
17impl DocumentId {
18 pub fn new() -> Self {
20 Self(Uuid::new_v4())
21 }
22}
23
24impl Default for DocumentId {
25 fn default() -> Self {
26 Self::new()
27 }
28}
29
30#[derive(Debug, Default)]
32pub struct Workspace {
33 documents: HashMap<DocumentId, Document>,
34 pub root: Option<PathBuf>,
36}
37
38impl Workspace {
39 pub fn new() -> Self {
41 Self::default()
42 }
43
44 pub fn open(&mut self, doc: Document) -> DocumentId {
46 let id = DocumentId::new();
47 self.documents.insert(id, doc);
48 id
49 }
50
51 pub fn get(&self, id: &DocumentId) -> Option<&Document> {
53 self.documents.get(id)
54 }
55
56 pub fn get_mut(&mut self, id: &DocumentId) -> Option<&mut Document> {
58 self.documents.get_mut(id)
59 }
60
61 pub fn close(&mut self, id: &DocumentId) -> Option<Document> {
63 self.documents.remove(id)
64 }
65
66 pub fn len(&self) -> usize {
68 self.documents.len()
69 }
70
71 pub fn is_empty(&self) -> bool {
73 self.documents.is_empty()
74 }
75}
76
77#[cfg(test)]
78mod tests {
79 use super::*;
80
81 #[test]
82 fn test_workspace_open_close() {
83 let mut ws = Workspace::new();
84 let doc = Document::new("# Test");
85 let id = ws.open(doc);
86 assert_eq!(ws.len(), 1);
87 assert!(ws.get(&id).is_some());
88 ws.close(&id);
89 assert!(ws.is_empty());
90 }
91}