use crate::node::Tree;
use serde::{Deserialize, Serialize};
use std::{
collections::HashMap,
path::{Path, PathBuf},
};
#[derive(Debug, Serialize, Deserialize)]
pub struct Forest {
tree: Tree,
projects: HashMap<PathBuf, Tree>,
}
impl Forest {
pub fn new(tree: Tree) -> Self {
Self {
tree,
projects: HashMap::new(),
}
}
pub fn main_tree(&self) -> &Tree {
&self.tree
}
pub fn cwd_trees(&self) -> impl Iterator<Item = (&Path, &Tree)> {
self
.projects
.iter()
.map(|(cwd, tree)| (cwd.as_path(), tree))
}
pub fn cwd_tree(&self, cwd: impl AsRef<Path>) -> Option<&Tree> {
self.projects.get(cwd.as_ref())
}
pub fn add_cwd_tree(&mut self, cwd: impl Into<PathBuf>, tree: Tree) {
let _ = self.projects.insert(cwd.into(), tree);
}
}