1use crate::node::Tree;
7use serde::{Deserialize, Serialize};
8use std::{
9 collections::HashMap,
10 path::{Path, PathBuf},
11};
12
13#[derive(Debug, Serialize, Deserialize)]
14pub struct Forest {
15 tree: Tree,
17
18 projects: HashMap<PathBuf, Tree>,
22}
23
24impl Forest {
25 pub fn new(tree: Tree) -> Self {
30 Self {
31 tree,
32 projects: HashMap::new(),
33 }
34 }
35
36 pub fn main_tree(&self) -> &Tree {
38 &self.tree
39 }
40
41 pub fn cwd_trees(&self) -> impl Iterator<Item = (&Path, &Tree)> {
43 self
44 .projects
45 .iter()
46 .map(|(cwd, tree)| (cwd.as_path(), tree))
47 }
48
49 pub fn cwd_tree(&self, cwd: impl AsRef<Path>) -> Option<&Tree> {
51 self.projects.get(cwd.as_ref())
52 }
53
54 pub fn add_cwd_tree(&mut self, cwd: impl Into<PathBuf>, tree: Tree) {
56 let _ = self.projects.insert(cwd.into(), tree);
57 }
58}