1
1
use crate :: hash:: Source ;
2
2
3
- use std:: fs:: { self , File } ;
4
- use std:: io:: { prelude :: * , ErrorKind } ;
3
+ use std:: fs;
4
+ use std:: io:: ErrorKind ;
5
5
use std:: path:: PathBuf ;
6
- use std:: string:: String ;
7
6
8
7
use failure:: Error ;
9
8
10
- pub const DEFAULT_RELATIVE_COMPONENT : & ' static str = ".shadowenv.d" ;
9
+ pub const DEFAULT_RELATIVE_COMPONENT : & str = ".shadowenv.d" ;
11
10
12
11
/// Search upwards the filesystem branch starting with `at` and then its ancestors looking
13
12
/// for a file or directory named `relative_component`.
@@ -16,41 +15,71 @@ pub fn find_root(at: PathBuf, relative_component: &str) -> Result<Option<PathBuf
16
15
let dirpath = curr. join ( relative_component) ;
17
16
18
17
match fs:: read_dir ( & dirpath) {
19
- Ok ( _) => return Ok ( Some ( std :: fs:: canonicalize ( dirpath) ?) ) ,
18
+ Ok ( _) => return Ok ( Some ( fs:: canonicalize ( dirpath) ?) ) ,
20
19
Err ( ref e) if e. kind ( ) == ErrorKind :: NotFound => ( ) ,
21
20
Err ( e) => return Err ( e. into ( ) ) ,
22
21
}
23
22
}
24
- return Ok ( None ) ;
23
+ Ok ( None )
25
24
}
26
25
27
26
/// Load all .lisp files in the directory pointed by `dirpath` storing their names and contents as
28
27
/// `SourceFiles` inside a `Source` struct.
29
28
///
30
29
/// Note that this function assumes that the dirpath is trusted.
31
30
pub fn load ( dirpath : PathBuf ) -> Result < Option < Source > , Error > {
32
- let mut source = Source :: new ( dirpath. parent ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) . to_string ( ) ) ;
33
-
34
- for entry in fs:: read_dir ( dirpath) ? {
35
- if let Ok ( entry) = entry {
36
- let path = entry. path ( ) ;
37
- if path. is_file ( ) {
38
- // TODO: there HAS to be a better way to do this.
39
- let basename = path. file_name ( ) . unwrap ( ) . to_str ( ) . unwrap ( ) . to_string ( ) ;
40
- if !basename. ends_with ( ".lisp" ) {
41
- continue ;
42
- }
43
- let mut file = File :: open ( & path) ?;
44
- let mut contents = String :: new ( ) ;
45
- file. read_to_string ( & mut contents) ?;
46
- // TODO: surely there's a better way to do this.
47
- source. add_file ( basename, contents) ?;
31
+ let mut source = Source :: new ( dirpath. parent ( ) . unwrap ( ) . to_string_lossy ( ) . to_string ( ) ) ;
32
+
33
+ for entry in fs:: read_dir ( dirpath) ?. flatten ( ) {
34
+ let path = entry. path ( ) ;
35
+ if path. is_file ( ) {
36
+ // TODO: there HAS to be a better way to do this.
37
+ let basename = path. file_name ( ) . unwrap ( ) . to_string_lossy ( ) . to_string ( ) ;
38
+ if !basename. ends_with ( ".lisp" ) {
39
+ continue ;
48
40
}
41
+ let contents = fs:: read_to_string ( & path) ?;
42
+ source. add_file ( basename, contents) ;
49
43
}
50
44
}
51
45
52
- if source. files . len ( ) == 0 {
46
+ if source. files . is_empty ( ) {
53
47
return Ok ( None ) ;
54
48
}
55
- return Ok ( Some ( source) ) ;
49
+ Ok ( Some ( source) )
50
+ }
51
+
52
+ #[ cfg( test) ]
53
+ mod tests {
54
+ use super :: * ;
55
+ use crate :: hash:: SourceFile ;
56
+
57
+ #[ test]
58
+ fn test_load ( ) {
59
+ let path: PathBuf = [ env ! ( "CARGO_MANIFEST_DIR" ) , "tests" , "fixtures" , "simple" ]
60
+ . iter ( )
61
+ . collect ( ) ;
62
+ let res = load ( path) ;
63
+ let source = res. unwrap ( ) . unwrap ( ) ;
64
+ assert_eq ! ( source. files. len( ) , 2 , "it should contain 2 files" ) ;
65
+ let mut files = source. files . clone ( ) ;
66
+ files. sort_by ( |a, b| a. name . cmp ( & b. name ) ) ;
67
+
68
+ let expected = vec ! [
69
+ SourceFile {
70
+ name: "550_dev_ruby.lisp" . to_string( ) ,
71
+ contents: r#"(provide "ruby" "3.1.2")
72
+ "#
73
+ . to_string( ) ,
74
+ } ,
75
+ SourceFile {
76
+ name: "585_dev_rust.lisp" . to_string( ) ,
77
+ contents: r#"(provide "rust" "stable")
78
+ "#
79
+ . to_string( ) ,
80
+ } ,
81
+ ] ;
82
+
83
+ assert_eq ! ( files, expected)
84
+ }
56
85
}
0 commit comments