Skip to content

Commit 540f58a

Browse files
committed
SqlDoc functionality complete. Next testing
1 parent f1ff0b2 commit 540f58a

1 file changed

Lines changed: 67 additions & 12 deletions

File tree

src/sql_doc.rs

Lines changed: 67 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
use std::path::{Path, PathBuf};
44

55
use crate::{
6-
ast::ParsedSqlFileSet, comments::Comments, docs::{SqlFileDoc, TableDoc}, error::DocError, files::SqlFileSet
6+
ast::{ParsedSqlFile, ParsedSqlFileSet},
7+
comments::Comments,
8+
docs::{SqlFileDoc, TableDoc},
9+
error::DocError,
10+
files::{SqlFile, SqlFileSet},
711
};
812

913
/// Structure for Sql Documentation, built from [`TableDoc`] and
@@ -20,6 +24,8 @@ pub struct SqlDocBuilder {
2024
source: SqlFileDocSource,
2125
/// The list of Paths to be ignored for parsing purposes.
2226
deny: Vec<String>,
27+
/// Used to indicate maintaining the [`Vec<(PathBuf, SqlFileDoc`]
28+
retain_files: bool,
2329
}
2430

2531
/// Enum for specifying a file doc source as a `directory` or a specific `file`
@@ -29,22 +35,25 @@ enum SqlFileDocSource {
2935
}
3036

3137
impl SqlDoc {
32-
/// Method for creating a new SqlDoc
33-
pub fn new(tables: Vec<TableDoc>, files: Option<Vec<(PathBuf, SqlFileDoc)>>) -> Self {
34-
SqlDoc { tables, files }
38+
/// Method for creating a new [`SqlDoc`]
39+
#[must_use]
40+
pub const fn new(tables: Vec<TableDoc>, files: Option<Vec<(PathBuf, SqlFileDoc)>>) -> Self {
41+
Self { tables, files }
3542
}
3643
/// Method for generating builder from a directory.
3744
pub fn from_dir<P: AsRef<Path>>(root: P) -> SqlDocBuilder {
3845
SqlDocBuilder {
3946
source: SqlFileDocSource::Dir(root.as_ref().to_path_buf()),
4047
deny: Vec::new(),
48+
retain_files: false,
4149
}
4250
}
4351
/// Method for generating builder from a [`Path`] of a single file
4452
pub fn from_path<P: AsRef<Path>>(path: P) -> SqlDocBuilder {
4553
SqlDocBuilder {
4654
source: SqlFileDocSource::File(path.as_ref().to_path_buf()),
4755
deny: Vec::new(),
56+
retain_files: false,
4857
}
4958
}
5059

@@ -94,6 +103,17 @@ impl SqlDoc {
94103
}),
95104
}
96105
}
106+
107+
/// Getter method for returning the `&[TableDoc]`
108+
#[must_use]
109+
pub fn tables(&self) -> &[TableDoc] {
110+
&self.tables
111+
}
112+
/// Getter method for returning the `Option<&[(PathBuf,SqlFileDoc)]>`
113+
#[must_use]
114+
pub fn files(&self) -> Option<&[(PathBuf, SqlFileDoc)]> {
115+
self.files.as_deref()
116+
}
97117
}
98118

99119
impl SqlDocBuilder {
@@ -106,15 +126,45 @@ impl SqlDocBuilder {
106126
self
107127
}
108128

109-
pub fn build(&self) -> SqlDoc {
110-
129+
/// Setter that ticks on the option to retain the [`Vec<(PathBuf,SqlFileDoc)>`]
130+
pub const fn retain_files(mut self) -> Self {
131+
self.retain_files = true;
132+
self
133+
}
111134

112-
SqlDoc { tables: (), files: () }
135+
/// Builds the [`SqlDoc`]
136+
///
137+
/// # Errors
138+
/// - Will return `DocError` bubbled up
139+
pub fn build(self) -> Result<SqlDoc, DocError> {
140+
let docs: Vec<(PathBuf, SqlFileDoc)> = match &self.source {
141+
SqlFileDocSource::Dir(path) => generate_docs_from_dir(path, &self.deny)?,
142+
SqlFileDocSource::File(file) => {
143+
let gen_files = generate_docs_from_file(file)?;
144+
let (path, sql_doc) = gen_files;
145+
vec![(path, sql_doc)]
146+
}
147+
};
148+
let mut tables = Vec::new();
149+
if self.retain_files {
150+
let files = docs;
151+
for (_, sql_doc) in &files {
152+
tables.extend(sql_doc.tables().iter().cloned());
153+
}
154+
Ok(SqlDoc { tables, files: Some(files) })
155+
} else {
156+
for (_, sql_doc) in &docs {
157+
tables.extend(sql_doc.tables().iter().cloned());
158+
}
159+
Ok(SqlDoc { tables, files: None })
160+
}
113161
}
114162
}
115163

116-
117-
fn generate_docs_from_dir<P: AsRef<Path>, S: AsRef<str>>(source: P, deny: &[S]) -> Result<Vec<(PathBuf, SqlFileDoc)>, DocError> {
164+
fn generate_docs_from_dir<P: AsRef<Path>, S: AsRef<str>>(
165+
source: P,
166+
deny: &[S],
167+
) -> Result<Vec<(PathBuf, SqlFileDoc)>, DocError> {
118168
let deny_list: Vec<String> = deny.iter().map(|file| file.as_ref().to_string()).collect();
119169
let deny_option = if deny_list.is_empty() { None } else { Some(deny_list) };
120170
let file_set = SqlFileSet::new(source.as_ref(), deny_option)?;
@@ -129,6 +179,11 @@ fn generate_docs_from_dir<P: AsRef<Path>, S: AsRef<str>>(source: P, deny: &[S])
129179
Ok(sql_docs)
130180
}
131181

132-
fn generate_docs_from_file<P: AsRef<Path>, S: AsRef<str>>(source: P, deny: &[S]) -> Result<Vec<(PathBuf, SqlFileDoc)>, DocError> {
133-
134-
}
182+
fn generate_docs_from_file<P: AsRef<Path>>(source: P) -> Result<(PathBuf, SqlFileDoc), DocError> {
183+
let file = SqlFile::new(source.as_ref())?;
184+
let parsed_file = ParsedSqlFile::parse(file)?;
185+
let comments = Comments::parse_all_comments_from_file(&parsed_file)?;
186+
let docs = SqlFileDoc::from_parsed_file(&parsed_file, &comments)?;
187+
let path = parsed_file.file().path().to_path_buf();
188+
Ok((path, docs))
189+
}

0 commit comments

Comments
 (0)