@@ -16,7 +16,7 @@ use std::io::{Error, ErrorKind};
1616/// which you then use to interact with the `Path`
1717pub struct Simpath {
1818 name : String ,
19- dirs : Vec < PathBuf >
19+ entries : Vec < PathBuf >
2020}
2121
2222#[ derive( Debug ) ]
@@ -33,8 +33,6 @@ pub enum FileType {
3333
3434/// When validating a `Path` there can be the following types of `PathError`s returned
3535pub enum PathError {
36- /// The syntax for a segment of the `Path` does not match a file system path
37- InvalidSyntax ( String ) ,
3836 /// The `Path` entry does not exist on the file system
3937 DoesNotExist ( String ) ,
4038 /// The `Path` entry cannot be reads
@@ -68,7 +66,7 @@ impl Simpath {
6866 pub fn new ( var_name : & str ) -> Self {
6967 let mut search_path = Simpath {
7068 name : var_name. to_string ( ) ,
71- dirs : vec ! ( )
69+ entries : vec ! ( )
7270 } ;
7371
7472 search_path. add_from_env_var ( var_name) ;
@@ -103,7 +101,7 @@ impl Simpath {
103101 /// ```
104102 ///
105103 pub fn directories ( & self ) -> & Vec < PathBuf > {
106- & self . dirs
104+ & self . entries
107105 }
108106
109107 /// Try to find a file by filename (not full path) on a search path.
@@ -144,7 +142,7 @@ impl Simpath {
144142 /// }
145143 /// ```
146144 pub fn find_type ( & self , file_name : & str , file_type : FileType ) -> Result < PathBuf , Error > {
147- for search_dir in & self . dirs {
145+ for search_dir in & self . entries {
148146 for entry in fs:: read_dir ( search_dir) ? {
149147 let file = entry?;
150148 if let Some ( filename) = file. file_name ( ) . to_str ( ) {
@@ -184,7 +182,7 @@ impl Simpath {
184182 pub fn add_directory ( & mut self , dir : & str ) {
185183 let path = PathBuf :: from ( dir) ;
186184 if path. exists ( ) && path. is_dir ( ) && path. read_dir ( ) . is_ok ( ) {
187- self . dirs . push ( path) ;
185+ self . entries . push ( path) ;
188186 }
189187 }
190188
@@ -201,7 +199,7 @@ impl Simpath {
201199 /// }
202200 /// ```
203201 pub fn contains ( & self , entry : & str ) -> bool {
204- for search_dir in & self . dirs {
202+ for search_dir in & self . entries {
205203 if search_dir. to_str ( ) . unwrap ( ) == entry {
206204 return true ;
207205 }
@@ -234,12 +232,30 @@ impl Simpath {
234232 }
235233 }
236234 }
235+
236+ /// `validate` checks that all the entries in the `Path` are of a valid syntax, exist on
237+ /// the file system and can be read
238+ pub fn validate ( & self ) -> Vec < PathError > {
239+ let mut errors = vec ! ( ) ;
240+
241+ for entry in & self . entries {
242+ if !entry. exists ( ) {
243+ errors. push ( PathError :: DoesNotExist ( entry. to_str ( ) . unwrap ( ) . into ( ) ) ) ;
244+ }
245+
246+ if fs:: metadata ( entry) . is_err ( ) {
247+ errors. push ( PathError :: CannotRead ( entry. to_str ( ) . unwrap ( ) . into ( ) ) ) ;
248+ }
249+ }
250+
251+ errors
252+ }
237253}
238254
239255impl fmt:: Display for Simpath {
240256 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
241257 write ! ( f, "Search Path: '{}', Directories: {{" , self . name) . unwrap ( ) ;
242- for dir in & self . dirs {
258+ for dir in & self . entries {
243259 write ! ( f, "'{}', " , dir. display( ) ) . unwrap ( ) ;
244260
245261 }
@@ -400,4 +416,25 @@ mod test {
400416
401417 println ! ( "Simpath can be printed: {}" , path) ;
402418 }
419+
420+ #[ test]
421+ fn entry_does_not_exist ( ) {
422+ let var_name = "MyPathEnv" ;
423+ env:: set_var ( var_name, "/foo" ) ;
424+ let mut path = Simpath :: new ( "MyName" ) ;
425+ path. add_from_env_var ( var_name) ;
426+
427+ assert_eq ! ( path. directories( ) . len( ) , 0 ) ;
428+ }
429+
430+ #[ test]
431+ fn one_entry_does_not_exist ( ) {
432+ let var_name = "MyPathEnv" ;
433+ env:: set_var ( var_name, ".:/foo" ) ;
434+ let mut path = Simpath :: new ( "MyName" ) ;
435+ path. add_from_env_var ( var_name) ;
436+
437+ assert_eq ! ( path. directories( ) . len( ) , 1 ) ;
438+ assert_eq ! ( path. validate( ) . len( ) , 0 ) ;
439+ }
403440}
0 commit comments