1
- use std:: path:: PathBuf ;
1
+ use std:: path:: { Path , PathBuf } ;
2
2
use std:: thread;
3
3
4
4
use anstyle:: AnsiColor ;
5
5
use anyhow:: { anyhow, Error } ;
6
6
use clap:: builder:: { styling:: Styles , ValueParser } ;
7
7
use clap:: Parser ;
8
8
use clap:: ValueHint ;
9
+ use normpath:: PathExt ;
9
10
10
11
const STYLES : Styles = Styles :: styled ( )
11
12
. header ( AnsiColor :: Yellow . on_default ( ) )
@@ -41,7 +42,8 @@ pub struct Args {
41
42
pub blacklist_threshold : u64 ,
42
43
43
44
/// Number of threads to use when calibrating and scanning
44
- #[ clap( short = 'x' , long, value_parser = ValueParser :: new( parse_threads) , default_value_t = thread:: available_parallelism( ) . map( | n | n. get( ) ) . unwrap_or( 2 ) ) ]
45
+ #[ clap( short = 'x' , long, value_parser = ValueParser :: new( parse_threads) , default_value_t = thread:: available_parallelism( ) . map( | n | n. get( ) ) . unwrap_or( 2 )
46
+ ) ]
45
47
pub threads : usize ,
46
48
47
49
/// Seconds between status updates, set to 0 to disable
@@ -61,7 +63,8 @@ pub struct Args {
61
63
pub skip_path : Vec < PathBuf > ,
62
64
63
65
/// Paths to check for large directories
64
- #[ clap( required = true , value_parser, value_hint = ValueHint :: AnyPath ) ]
66
+ #[ clap( required = true , value_parser = ValueParser :: new( parse_paths) , value_hint = ValueHint :: AnyPath
67
+ ) ]
65
68
pub path : Vec < PathBuf > ,
66
69
}
67
70
@@ -77,3 +80,37 @@ fn parse_threads(x: &str) -> Result<usize, Error> {
77
80
Err ( e) => Err ( Error :: from ( e) ) ,
78
81
}
79
82
}
83
+
84
+ /// Parses a string into a `PathBuf`, checking if the path is a directory and exists.
85
+ ///
86
+ /// # Arguments
87
+ ///
88
+ /// * `x` - A string slice to be parsed into a `PathBuf`.
89
+ ///
90
+ /// # Returns
91
+ ///
92
+ /// * `Result<PathBuf, Error>` - An `Ok` variant containing a normalized `PathBuf` if the path is an existing directory,
93
+ /// or an `Err` variant with an error message if the path does not exist or is not a directory.
94
+ fn parse_paths ( x : & str ) -> Result < PathBuf , Error > {
95
+ let p = Path :: new ( x) ;
96
+
97
+ if directory_exists ( p) {
98
+ Ok ( p. normalize ( ) ?. into_path_buf ( ) )
99
+ } else {
100
+ Err ( anyhow ! ( "'{}' is not an existing directory" , x) )
101
+ }
102
+ }
103
+
104
+ /// Checks if the given path is a directory and exists.
105
+ ///
106
+ /// # Arguments
107
+ ///
108
+ /// * `x` - A reference to the path to check.
109
+ ///
110
+ /// # Returns
111
+ ///
112
+ /// * `bool` - `true` if the path is an existing directory, `false` otherwise.
113
+ #[ inline]
114
+ fn directory_exists ( x : & Path ) -> bool {
115
+ x. is_dir ( ) && x. normalize ( ) . is_ok ( )
116
+ }
0 commit comments