11use crate :: rust_file:: RustFile ;
22use std:: collections:: HashSet ;
3- use std:: fs;
43use std:: ops:: Deref ;
54use syn:: { File , Item , ItemUse , UseTree } ;
65
7- pub fn get_dependencies_in_file ( file : & RustFile ) -> Vec < String > {
8- let content = match fs:: read_to_string ( & file. path ) {
9- Ok ( content) => content,
10- Err ( e) => panic ! ( "Failed to read file file://{}: {}" , file. path, e) ,
11- } ;
12-
13- let ast = match syn:: parse_file ( & content) {
14- Ok ( ast) => ast,
15- Err ( e) => panic ! ( "Failed to parse file file://{}: {}" , file. path, e) ,
16- } ;
17-
18- get_dependencies_in_ast ( & ast, & file. logical_path )
19- }
20-
216fn parse_module_item ( item : & Item , dependencies : & mut Vec < String > , current_module : & str ) {
227 match item {
238 Item :: Use ( ItemUse { tree, .. } ) => {
@@ -34,17 +19,11 @@ fn parse_module_item(item: &Item, dependencies: &mut Vec<String>, current_module
3419 }
3520}
3621
37- #[ allow( dead_code) ]
38- fn get_dependencies_in_str ( s : & str , module : & str ) -> Vec < String > {
39- let ast: File = match syn:: parse_str ( s) {
40- Ok ( ast) => ast,
41- Err ( e) => panic ! ( "Failed to parse string '{}': {}" , s, e) ,
42- } ;
43-
44- get_dependencies_in_ast ( & ast, module)
22+ pub fn get_dependencies_in_file ( file : & RustFile ) -> Vec < String > {
23+ get_dependencies_in_ast ( & file. ast , & file. logical_path )
4524}
4625
47- pub fn get_dependencies_in_ast ( ast : & File , current_module_logical_path : & str ) -> Vec < String > {
26+ fn get_dependencies_in_ast ( ast : & File , current_module_logical_path : & str ) -> Vec < String > {
4827 let mut dependencies = Vec :: new ( ) ;
4928
5029 for item in ast. items . iter ( ) {
@@ -69,12 +48,9 @@ pub fn get_dependencies_in_ast(ast: &File, current_module_logical_path: &str) ->
6948 }
7049 }
7150
72- unique_values ( dependencies)
73- }
74-
75- fn unique_values < T : std:: hash:: Hash + Eq + Clone > ( vec : Vec < T > ) -> Vec < T > {
7651 let mut unique_set = HashSet :: new ( ) ;
77- vec. into_iter ( )
52+ dependencies
53+ . into_iter ( )
7854 . filter ( |item| unique_set. insert ( item. clone ( ) ) )
7955 . collect ( )
8056}
@@ -144,6 +120,15 @@ fn collect_dependencies_from_tree(
144120mod tests {
145121 use super :: * ;
146122
123+ fn get_dependencies_in_str ( s : & str , current_module_logical_path : & str ) -> Vec < String > {
124+ let ast: File = match syn:: parse_str ( s) {
125+ Ok ( ast) => ast,
126+ Err ( e) => panic ! ( "Failed to parse string '{}': {}" , s, e) ,
127+ } ;
128+
129+ get_dependencies_in_ast ( & ast, current_module_logical_path)
130+ }
131+
147132 #[ test]
148133 pub fn test_parsing ( ) {
149134 let dependencies = get_dependencies_in_file ( & RustFile :: from (
0 commit comments