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: Option<ColorChoice>,
15    charset: Option<Charset<'charset>>,
16    max_level: Option<usize>,
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) -> Self {
29        Self {
30            root,
31            git: None,
32            max_level: None,
33            charset: None,
34            color_choice: 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 [`ColorChoice`] override for the [`Tree`]. The color choice provided by the
72    /// main configuration is used if this isn't set.
73    #[inline]
74    #[must_use]
75    pub fn color_choice(self, color_choice: ColorChoice) -> Self {
76        Self {
77            color_choice: Some(color_choice),
78            ..self
79        }
80    }
81
82    /// Sets the configuration for the [`Tree`].
83    #[inline]
84    #[must_use]
85    pub fn config(self, config: config::Main) -> Self {
86        Self {
87            config: Some(config),
88            ..self
89        }
90    }
91
92    /// Sets the icon configuration for the [`Tree`].
93    #[inline]
94    #[must_use]
95    pub fn icons(self, icons: config::Icons) -> Self {
96        Self {
97            icons: Some(icons),
98            ..self
99        }
100    }
101
102    /// Sets the colors configuration for the [`Tree`].
103    #[inline]
104    #[must_use]
105    pub fn colors(self, colors: config::Colors) -> Self {
106        Self {
107            colors: Some(colors),
108            ..self
109        }
110    }
111
112    /// Creates the [`Tree`].
113    pub fn build(self) -> Tree<'git, 'charset, P> {
114        Tree {
115            root: self.root,
116            git: self.git,
117            max_level: self.max_level,
118            charset: self.charset.unwrap_or_default(),
119            color_choice: self.color_choice,
120            config: self.config.unwrap_or_default(),
121            icons: self.icons.unwrap_or_default(),
122            colors: self.colors.unwrap_or_default(),
123        }
124    }
125}