Skip to content

Commit f1ff0b2

Browse files
committed
implemented helper for generating docs from directory
1 parent 1ed25b4 commit f1ff0b2

File tree

3 files changed

+80
-35
lines changed

3 files changed

+80
-35
lines changed

src/error.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ pub enum DocError {
2929
/// Duplicate tables with same name were found when searching [`crate::SqlDoc`]
3030
DuplicateTablesFound {
3131
/// `Vec` of the [`TableDoc`] for each duplicate table found
32-
tables: Vec<TableDoc>
33-
}
32+
tables: Vec<TableDoc>,
33+
},
3434
}
3535

3636
impl fmt::Display for DocError {
@@ -43,16 +43,15 @@ impl fmt::Display for DocError {
4343
Self::SqlParserError(parser_error) => write!(f, "SQL parse error {parser_error}"),
4444
Self::InvalidObjectName { message, line, column } => {
4545
write!(f, "{message} at line {line}, column {column}")
46-
},
47-
Self::TableNotFound{name} => write!(f, "Table not found in SqlDoc: {name}"),
46+
}
47+
Self::TableNotFound { name } => write!(f, "Table not found in SqlDoc: {name}"),
4848
Self::DuplicateTablesFound { tables } => {
4949
writeln!(f, "Duplicate tables found:")?;
5050
for t in tables {
51-
writeln!(f, "{t}")?;
51+
writeln!(f, "{t}")?;
5252
}
5353
Ok(())
5454
}
55-
5655
}
5756
}
5857
}
@@ -63,7 +62,9 @@ impl error::Error for DocError {
6362
Self::FileReadError(e) => Some(e),
6463
Self::CommentError(e) => Some(e),
6564
Self::SqlParserError(e) => Some(e),
66-
Self::InvalidObjectName { .. } | Self::TableNotFound { .. } | Self::DuplicateTablesFound { .. } => None,
65+
Self::InvalidObjectName { .. }
66+
| Self::TableNotFound { .. }
67+
| Self::DuplicateTablesFound { .. } => None,
6768
}
6869
}
6970
}

src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,8 @@ mod tests {
121121
.map_or_else(|| panic!("unable to find first value"), |(_, docs)| docs.tables());
122122
for (i, column) in first_tables[0].columns().iter().enumerate() {
123123
assert_eq!(column.name(), user_columns[i]);
124-
let column_doc = column
125-
.doc()
126-
.map_or_else(|| panic!("unable to find column doc value"), |val| val);
124+
let column_doc =
125+
column.doc().map_or_else(|| panic!("unable to find column doc value"), |val| val);
127126
assert_eq!(column_doc, user_columns_comments[i]);
128127
}
129128
Ok(())

src/sql_doc.rs

Lines changed: 70 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
1-
//! Module for the top level `SqlDoc` structure.
1+
//! Module for the top level `SqlDoc` structure.
22
33
use std::path::{Path, PathBuf};
44

55
use 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
1110
pub 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`]
2018
pub 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

3331
impl 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

8799
impl 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

Comments
 (0)