11//! The module for working with and loading the content from `sql` files
22use std:: fs;
33use std:: io;
4+ use std:: io:: Error ;
45use std:: path:: { Path , PathBuf } ;
56
67pub struct SqlFilesList {
@@ -14,6 +15,9 @@ impl SqlFilesList {
1415 sql_files : recursive_scan,
1516 } )
1617 }
18+ pub fn sql_files ( & self ) -> & Vec < PathBuf > {
19+ return & self . sql_files ;
20+ }
1721}
1822
1923/// Helper function to recursively scan the specified directory and collect all sql files found
@@ -22,7 +26,10 @@ fn recursive_dir_scan(path: &Path) -> io::Result<Vec<PathBuf>> {
2226 for entry in fs:: read_dir ( path) ? {
2327 let entry = entry?;
2428 let path = entry. path ( ) ;
25- if path. is_file ( ) && path. extension ( ) . unwrap ( ) == "sql" {
29+ if path. is_file ( ) && path
30+ . extension ( )
31+ . and_then ( |ext| ext. to_str ( ) )
32+ == Some ( "sql" ) {
2633 sql_files. push ( path) ;
2734 } else if path. is_dir ( ) {
2835 let nested = recursive_dir_scan ( & path) ?;
@@ -32,15 +39,39 @@ fn recursive_dir_scan(path: &Path) -> io::Result<Vec<PathBuf>> {
3239 Ok ( sql_files)
3340}
3441
42+ #[ derive( Debug ) ]
3543pub struct SqlFile {
3644 path : PathBuf ,
3745 content : String ,
3846}
47+ impl SqlFile {
48+ pub fn new ( path : & Path ) -> io:: Result < SqlFile > {
49+ let content = fs:: read_to_string ( path) ?;
50+ Ok ( SqlFile { path : path. to_owned ( ) , content } )
51+ }
52+ }
3953
54+ #[ derive( Debug ) ]
4055pub struct SqlFileSet {
4156 files_contents : Vec < SqlFile >
4257}
4358
59+ impl SqlFileSet {
60+ pub fn new ( path : & Path ) -> io:: Result < SqlFileSet > {
61+ let sql_files_list = SqlFilesList :: new ( path) ?;
62+
63+ let files_contents = sql_files_list
64+ . sql_files ( )
65+ . iter ( )
66+ . map ( |p| SqlFile :: new ( p) )
67+ . collect :: < io:: Result < Vec < _ > > > ( ) ?;
68+
69+ Ok ( SqlFileSet { files_contents } )
70+ }
71+ pub fn iter ( & self ) -> impl Iterator < Item = & SqlFile > {
72+ self . files_contents . iter ( )
73+ }
74+ }
4475
4576#[ cfg( test) ]
4677mod tests {
0 commit comments