@@ -14,6 +14,8 @@ pub struct Builder<'git, 'charset, P: AsRef<Path>> {
1414 color_choice : Option < ColorChoice > ,
1515 charset : Option < Charset < ' charset > > ,
1616 max_level : Option < usize > ,
17+ /// Override the level limit that may be set by the configuration.
18+ unset_level : bool ,
1719 config : Option < config:: Main > ,
1820 icons : Option < config:: Icons > ,
1921 colors : Option < config:: Colors > ,
3032 root,
3133 git : None ,
3234 max_level : None ,
35+ unset_level : false ,
3336 charset : None ,
3437 color_choice : None ,
3538 config : None ,
5861 }
5962 }
6063
64+ /// Unsets the maximum depth level for the [`Tree`], returning to the default
65+ /// behavior of searching infinitely deep.
66+ ///
67+ /// This helps override a maximum level that may have been set by the configuration.
68+ #[ inline]
69+ #[ must_use]
70+ pub fn unset_level ( self ) -> Self {
71+ Self {
72+ unset_level : true ,
73+ ..self
74+ }
75+ }
76+
6177 /// Sets the [`Charset`] for the [`Tree`].
6278 #[ inline]
6379 #[ must_use]
@@ -110,11 +126,25 @@ where
110126 }
111127
112128 /// Creates the [`Tree`].
129+ ///
130+ /// # Panics
131+ ///
132+ /// - Panics if `max_level` and `unset_level` were both called.
113133 pub fn build ( self ) -> Tree < ' git , ' charset , P > {
134+ assert ! (
135+ !( self . unset_level && self . max_level. is_some( ) ) ,
136+ "max_level cannot be set when unset_level is true"
137+ ) ;
138+ let max_level = if self . unset_level {
139+ None
140+ } else {
141+ self . max_level
142+ . or ( self . config . as_ref ( ) . and_then ( |config| config. level ( ) ) )
143+ } ;
114144 Tree {
115145 root : self . root ,
116146 git : self . git ,
117- max_level : self . max_level ,
147+ max_level,
118148 charset : self . charset . unwrap_or_default ( ) ,
119149 color_choice : self . color_choice ,
120150 config : self . config . unwrap_or_default ( ) ,
@@ -123,3 +153,14 @@ where
123153 }
124154 }
125155}
156+
157+ #[ cfg( test) ]
158+ mod tests {
159+ use super :: * ;
160+
161+ #[ test]
162+ #[ should_panic]
163+ fn test_cannot_build_unset_level_with_max_level ( ) {
164+ Builder :: new ( "." ) . max_level ( 1 ) . unset_level ( ) . build ( ) ;
165+ }
166+ }
0 commit comments