forester_rs/runtime/builder/
file_builder.rs

1use crate::runtime::{RtResult, RuntimeError};
2use crate::tree::project::{FileName, Project, TreeName};
3use std::path::PathBuf;
4
5/// The builder to build Forester from the tree files.
6pub struct FileForesterBuilder {
7    main_file: Option<FileName>,
8    main: Option<TreeName>,
9    pub(super) root: Option<PathBuf>,
10}
11
12impl FileForesterBuilder {
13    pub fn new() -> Self {
14        Self {
15            main_file: None,
16            main: None,
17            root: None,
18        }
19    }
20
21    /// Root folder.
22    pub fn root(&mut self, root: PathBuf) {
23        self.root = Some(root);
24    }
25    /// A file that has a main root definition
26    pub fn main_file(&mut self, main_file: FileName) {
27        self.main_file = Some(main_file);
28    }
29    /// A name of the main root definition
30    pub fn main_tree(&mut self, main_tree: TreeName) {
31        self.main = Some(main_tree);
32    }
33
34    /// The method to build forester
35    pub fn build(self) -> RtResult<Project> {
36        match (self.main, self.root.clone(), self.main_file) {
37            (None, Some(root), Some(mf)) => Ok(Project::build(mf, root)?),
38            (Some(mt), Some(root), Some(mf)) => Ok(Project::build_with_root(mf, mt, root)?),
39            _ => Err(RuntimeError::UnImplementedAction(
40                "not enough arguments to initialize the project".to_string(),
41            )),
42        }
43    }
44}