1- //! Module for the top level `SqlDoc` structure.
1+ //! Module for the top level `SqlDoc` structure.
22
33use std:: path:: { Path , PathBuf } ;
44
55use crate :: {
6- docs:: { SqlFileDoc , TableDoc } ,
7- error:: DocError ,
6+ ast:: ParsedSqlFileSet , comments:: Comments , docs:: { SqlFileDoc , TableDoc } , error:: DocError , files:: SqlFileSet
87} ;
98
10- /// Structure for Sql Documentation, built from [`TableDoc`] and
9+ /// Structure for Sql Documentation, built from [`TableDoc`] and
1110pub struct SqlDoc {
1211 /// Holds the [`Vec`] of all tables found in all specified files.
1312 tables : Vec < TableDoc > ,
14- /// Holds the [`Vec`] of each file's [`PathBuf`] and the original file's [`SqlFileDoc`]
15- files : Vec < ( PathBuf , SqlFileDoc ) > ,
13+ /// Optionally holds the [`Vec`] of each file's [`PathBuf`] and the original file's [`SqlFileDoc`]
14+ files : Option < Vec < ( PathBuf , SqlFileDoc ) > > ,
1615}
1716
18-
1917/// Builder structure for the [`SqlDoc`]
2018pub struct SqlDocBuilder {
2119 /// The source for implementing the [`SqlDoc`] to be built
2220 source : SqlFileDocSource ,
23- /// The list of files to be ignored for parsing purposes.
21+ /// The list of Paths to be ignored for parsing purposes.
2422 deny : Vec < String > ,
2523}
2624
@@ -31,51 +29,65 @@ enum SqlFileDocSource {
3129}
3230
3331impl SqlDoc {
34- /// Method for generating builder from a directory.
32+ /// Method for creating a new SqlDoc
33+ pub fn new ( tables : Vec < TableDoc > , files : Option < Vec < ( PathBuf , SqlFileDoc ) > > ) -> Self {
34+ SqlDoc { tables, files }
35+ }
36+ /// Method for generating builder from a directory.
3537 pub fn from_dir < P : AsRef < Path > > ( root : P ) -> SqlDocBuilder {
36- SqlDocBuilder { source : SqlFileDocSource :: Dir ( root. as_ref ( ) . to_path_buf ( ) ) , deny : Vec :: new ( ) }
38+ SqlDocBuilder {
39+ source : SqlFileDocSource :: Dir ( root. as_ref ( ) . to_path_buf ( ) ) ,
40+ deny : Vec :: new ( ) ,
41+ }
3742 }
38- /// Method for generating builder from a [`Path`] of a single file
43+ /// Method for generating builder from a [`Path`] of a single file
3944 pub fn from_path < P : AsRef < Path > > ( path : P ) -> SqlDocBuilder {
40- SqlDocBuilder { source : SqlFileDocSource :: File ( path. as_ref ( ) . to_path_buf ( ) ) , deny : Vec :: new ( ) }
45+ SqlDocBuilder {
46+ source : SqlFileDocSource :: File ( path. as_ref ( ) . to_path_buf ( ) ) ,
47+ deny : Vec :: new ( ) ,
48+ }
4149 }
4250
4351 /// Method for finding a specific [`TableDoc`] by `name`
44- ///
52+ ///
4553 /// # Parameters
4654 /// - the table `name` as a [`str`]
47- ///
55+ ///
4856 /// # Errors
4957 /// - Will return [`DocError::TableNotFound`] if the expected table is not found
5058 /// - Will return [`DocError::DuplicateTablesFound`] if more than one table is found
5159 pub fn table ( & self , table : & str ) -> Result < & TableDoc , DocError > {
52- let matches = self . tables . iter ( ) . filter ( |table_doc| table_doc. name ( ) == table) . collect :: < Vec < & TableDoc > > ( ) ;
60+ let matches = self
61+ . tables
62+ . iter ( )
63+ . filter ( |table_doc| table_doc. name ( ) == table)
64+ . collect :: < Vec < & TableDoc > > ( ) ;
5365 match matches. as_slice ( ) {
54- [ ] => Err ( DocError :: TableNotFound {
55- name : table. to_string ( ) ,
56- } ) ,
66+ [ ] => Err ( DocError :: TableNotFound { name : table. to_string ( ) } ) ,
5767 [ only] => Ok ( * only) ,
5868 _ => Err ( DocError :: DuplicateTablesFound {
5969 tables : matches. into_iter ( ) . cloned ( ) . collect ( ) ,
6070 } ) ,
6171 }
6272 }
63-
73+
6474 /// Method for finding a specific [`TableDoc`] from `schema` and table `name`
65- ///
75+ ///
6676 /// # Parameters
6777 /// - the table's `schema` as a [`str`]
6878 /// - the table's `name` as a [`str`]
69- ///
79+ ///
7080 /// # Errors
7181 /// - Will return [`DocError::TableNotFound`] if the expected table is not found
7282 /// - Will return [`DocError::DuplicateTablesFound`] if more than one table is found
7383 pub fn table_with_schema ( & self , schema : & str , name : & str ) -> Result < & TableDoc , DocError > {
74- let matches = self . tables . iter ( ) . filter ( |table_doc| table_doc. name ( ) == name && table_doc. schema ( ) == Some ( schema) ) . collect :: < Vec < & TableDoc > > ( ) ;
84+ let matches = self
85+ . tables
86+ . iter ( )
87+ . filter ( |table_doc| table_doc. name ( ) == name && table_doc. schema ( ) == Some ( schema) )
88+ . collect :: < Vec < & TableDoc > > ( ) ;
7589 match matches. as_slice ( ) {
76- [ ] => Err ( DocError :: TableNotFound {
77- name : name. to_string ( ) ,
78- } ) ,
90+ [ ] => Err ( DocError :: TableNotFound { name : name. to_string ( ) } ) ,
7991 [ only] => Ok ( * only) ,
8092 _ => Err ( DocError :: DuplicateTablesFound {
8193 tables : matches. into_iter ( ) . cloned ( ) . collect ( ) ,
@@ -85,5 +97,38 @@ impl SqlDoc {
8597}
8698
8799impl SqlDocBuilder {
100+ /// Method for adding an item to the deny list
101+ ///
102+ /// # Parameters
103+ /// - The `path` that will be added to deny path `Vec`
104+ pub fn deny ( mut self , deny_path : & str ) -> Self {
105+ self . deny . push ( deny_path. into ( ) ) ;
106+ self
107+ }
108+
109+ pub fn build ( & self ) -> SqlDoc {
110+
88111
112+ SqlDoc { tables : ( ) , files : ( ) }
113+ }
89114}
115+
116+
117+ fn generate_docs_from_dir < P : AsRef < Path > , S : AsRef < str > > ( source : P , deny : & [ S ] ) -> Result < Vec < ( PathBuf , SqlFileDoc ) > , DocError > {
118+ let deny_list: Vec < String > = deny. iter ( ) . map ( |file| file. as_ref ( ) . to_string ( ) ) . collect ( ) ;
119+ let deny_option = if deny_list. is_empty ( ) { None } else { Some ( deny_list) } ;
120+ let file_set = SqlFileSet :: new ( source. as_ref ( ) , deny_option) ?;
121+ let parsed_files = ParsedSqlFileSet :: parse_all ( file_set) ?;
122+ let mut sql_docs = Vec :: new ( ) ;
123+ for file in parsed_files. files ( ) {
124+ let comments = Comments :: parse_all_comments_from_file ( file) ?;
125+ let docs = SqlFileDoc :: from_parsed_file ( file, & comments) ?;
126+ let path = file. file ( ) . path ( ) . to_path_buf ( ) ;
127+ sql_docs. push ( ( path, docs) ) ;
128+ }
129+ Ok ( sql_docs)
130+ }
131+
132+ fn generate_docs_from_file < P : AsRef < Path > , S : AsRef < str > > ( source : P , deny : & [ S ] ) -> Result < Vec < ( PathBuf , SqlFileDoc ) > , DocError > {
133+
134+ }
0 commit comments