use std::collections::HashMap;
use std::path::PathBuf;
use uuid::Uuid;
use crate::document::Document;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct DocumentId(Uuid);
impl DocumentId {
pub fn new() -> Self {
Self(Uuid::new_v4())
}
}
impl Default for DocumentId {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Default)]
pub struct Workspace {
documents: HashMap<DocumentId, Document>,
pub root: Option<PathBuf>,
}
impl Workspace {
pub fn new() -> Self {
Self::default()
}
pub fn open(&mut self, doc: Document) -> DocumentId {
let id = DocumentId::new();
self.documents.insert(id, doc);
id
}
pub fn get(&self, id: &DocumentId) -> Option<&Document> {
self.documents.get(id)
}
pub fn get_mut(&mut self, id: &DocumentId) -> Option<&mut Document> {
self.documents.get_mut(id)
}
pub fn close(&mut self, id: &DocumentId) -> Option<Document> {
self.documents.remove(id)
}
pub fn len(&self) -> usize {
self.documents.len()
}
pub fn is_empty(&self) -> bool {
self.documents.is_empty()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_workspace_open_close() {
let mut ws = Workspace::new();
let doc = Document::new("# Test");
let id = ws.open(doc);
assert_eq!(ws.len(), 1);
assert!(ws.get(&id).is_some());
ws.close(&id);
assert!(ws.is_empty());
}
}