fancy_tree/tree/
builder.rs

1//! Provides tools for building a [`Tree`].
2use super::Tree;
3use super::charset::Charset;
4use crate::color::ColorChoice;
5use crate::config;
6use crate::git::Git;
7use std::path::Path;
8
9pub struct Builder<'git, 'charset, P: AsRef<Path>> {
10    /// The root path for the [`Tree`].
11    root: P,
12    /// The optional git state.
13    git: Option<&'git Git>,
14    color_choice: ColorChoice,
15    max_level: Option<usize>,
16    charset: Option<Charset<'charset>>,
17    config: Option<config::Main>,
18    icons: Option<config::Icons>,
19    colors: Option<config::Colors>,
20}
21
22impl<'git, 'charset, P> Builder<'git, 'charset, P>
23where
24    P: AsRef<Path>,
25{
26    /// Creates a new [`Builder`]
27    #[inline]
28    pub fn new(root: P, color_choice: ColorChoice) -> Self {
29        Self {
30            root,
31            git: None,
32            color_choice,
33            max_level: None,
34            charset: None,
35            config: None,
36            icons: None,
37            colors: None,
38        }
39    }
40
41    /// Adds a git state for the [`Tree`].
42    #[inline]
43    #[must_use]
44    pub fn git(self, git: &'git Git) -> Self {
45        Self {
46            git: Some(git),
47            ..self
48        }
49    }
50
51    /// Sets the maximum depth level for the [`Tree`].
52    #[inline]
53    #[must_use]
54    pub fn max_level(self, level: usize) -> Self {
55        Self {
56            max_level: Some(level),
57            ..self
58        }
59    }
60
61    /// Sets the [`Charset`] for the [`Tree`].
62    #[inline]
63    #[must_use]
64    pub fn charset(self, charset: Charset<'charset>) -> Self {
65        Self {
66            charset: Some(charset),
67            ..self
68        }
69    }
70
71    /// Sets the configuration for the [`Tree`].
72    #[inline]
73    #[must_use]
74    pub fn config(self, config: config::Main) -> Self {
75        Self {
76            config: Some(config),
77            ..self
78        }
79    }
80
81    /// Sets the icon configuration for the [`Tree`].
82    #[inline]
83    #[must_use]
84    pub fn icons(self, icons: config::Icons) -> Self {
85        Self {
86            icons: Some(icons),
87            ..self
88        }
89    }
90
91    /// Sets the colors configuration for the [`Tree`].
92    #[inline]
93    #[must_use]
94    pub fn colors(self, colors: config::Colors) -> Self {
95        Self {
96            colors: Some(colors),
97            ..self
98        }
99    }
100
101    /// Creates the [`Tree`].
102    pub fn build(self) -> Tree<'git, 'charset, P> {
103        Tree {
104            root: self.root,
105            git: self.git,
106            max_level: self.max_level,
107            charset: self.charset.unwrap_or_default(),
108            color_choice: self.color_choice,
109            config: self.config,
110            icons: self.icons,
111            colors: self.colors,
112        }
113    }
114}