1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//! A forest is a set of trees, including:
//!
//! - A main tree.
//! - Project trees (cwd-based).

use crate::node::Tree;
use serde::{Deserialize, Serialize};
use std::{
  collections::HashMap,
  path::{Path, PathBuf},
};

#[derive(Debug, Serialize, Deserialize)]
pub struct Forest {
  /// The main tree.
  tree: Tree,

  /// CWD-based trees.
  ///
  /// The keys are absolute paths.
  projects: HashMap<PathBuf, Tree>,
}

impl Forest {
  /// Create a new empty [`Forest`].
  ///
  /// This function creates an empty main tree with the provided `name` and `icon`, and no CWD-based project is
  /// initialized.
  pub fn new(tree: Tree) -> Self {
    Self {
      tree,
      projects: HashMap::new(),
    }
  }

  /// Get the main [`Tree`].
  pub fn main_tree(&self) -> &Tree {
    &self.tree
  }

  /// Get a CWD-based [`Tree`].
  pub fn cwd_tree(&self, cwd: impl AsRef<Path>) -> Option<&Tree> {
    self.projects.get(cwd.as_ref())
  }

  /// Add a [`Tree`] for the given CWD.
  pub fn add_cwd_tree(&mut self, cwd: impl Into<PathBuf>, tree: Tree) {
    let _ = self.projects.insert(cwd.into(), tree);
  }
}