Skip to content

Commit 32e9f67

Browse files
committed
Renamed SqlDocs to SqlFileDoc for disambiguation. Fixes #17
1 parent 5c84465 commit 32e9f67

4 files changed

Lines changed: 88 additions & 20 deletions

File tree

proposed_mods.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
sql_docs:
44
builder for
55
```rust
6-
SqlDocs
6+
SqlFileDoc
77
```
88

src/docs.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ impl TableDoc {
9191
/// Structure for containing the docs for every `Table` in an `.sql` file as a
9292
/// `Vec` of [`TableDoc`]
9393
#[derive(Clone, Debug, Eq, PartialEq)]
94-
pub struct SqlDocs {
94+
pub struct SqlFileDoc {
9595
tables: Vec<TableDoc>,
9696
}
9797

98-
impl SqlDocs {
99-
/// Create a new instance of [`SqlDocs`]
98+
impl SqlFileDoc {
99+
/// Create a new instance of [`SqlFileDoc`]
100100
///
101101
/// # Parameters
102102
/// - `tables` the `Vec` of [`TableDoc`] for the struct
@@ -200,7 +200,7 @@ fn schema_and_table(name: &ObjectName) -> Result<(Option<String>, String), DocEr
200200

201201
#[cfg(test)]
202202
mod tests {
203-
use crate::docs::{ColumnDoc, SqlDocs, TableDoc};
203+
use crate::docs::{ColumnDoc, SqlFileDoc, TableDoc};
204204

205205
#[test]
206206
fn test_sql_docs_struct() {
@@ -213,7 +213,7 @@ mod tests {
213213
columns,
214214
);
215215
let tables = vec![table_doc];
216-
let sql_doc = SqlDocs::new(tables);
216+
let sql_doc = SqlFileDoc::new(tables);
217217
let sql_doc_val =
218218
sql_doc.tables().first().map_or_else(|| panic!("unable to find tables"), |val| val);
219219
assert_eq!(sql_doc_val.name(), "user");
@@ -235,7 +235,7 @@ mod tests {
235235

236236
for file in parsed_set.files() {
237237
let comments = Comments::parse_all_comments_from_file(file)?;
238-
let docs = SqlDocs::from_parsed_file(file, &comments);
238+
let docs = SqlFileDoc::from_parsed_file(file, &comments);
239239
let filename = file
240240
.file()
241241
.path()
@@ -265,8 +265,8 @@ mod tests {
265265
Ok(())
266266
}
267267

268-
fn expected_without_comments_docs() -> SqlDocs {
269-
SqlDocs::new(vec![
268+
fn expected_without_comments_docs() -> SqlFileDoc {
269+
SqlFileDoc::new(vec![
270270
TableDoc::new(
271271
None,
272272
"users".to_string(),
@@ -293,10 +293,10 @@ mod tests {
293293
])
294294
}
295295

296-
fn expect_values() -> Vec<SqlDocs> {
296+
fn expect_values() -> Vec<SqlFileDoc> {
297297
let mut docs = Vec::new();
298298

299-
let first_docs = SqlDocs::new(vec![
299+
let first_docs = SqlFileDoc::new(vec![
300300
TableDoc::new(
301301
None,
302302
"users".to_string(),
@@ -332,7 +332,7 @@ mod tests {
332332
]);
333333
docs.push(first_docs);
334334

335-
let second_docs = SqlDocs::new(vec![
335+
let second_docs = SqlFileDoc::new(vec![
336336
TableDoc::new(
337337
None,
338338
"users".to_string(),

src/lib.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
//! - [`ast`] : parse SQL into an AST using [`sqlparser`]
55
//! - [`comments`] : extract and model comments and spans
66
7-
use crate::{ast::ParsedSqlFileSet, comments::Comments, docs::SqlDocs, files::SqlFileSet};
7+
use crate::{ast::ParsedSqlFileSet, comments::Comments, docs::SqlFileDoc, files::SqlFileSet};
88
pub use error::DocError;
99
use std::path::{Path, PathBuf};
1010
pub mod ast;
1111
pub mod comments;
1212
pub mod docs;
1313
pub mod error;
1414
pub mod files;
15+
mod sql_doc;
16+
pub use sql_doc::SqlDoc;
1517

16-
/// Primary Entry point. Returns a tuple of [`PathBuf`] and [`SqlDocs`].
18+
/// Primary Entry point. Returns a tuple of [`PathBuf`] and [`SqlFileDoc`].
1719
///
1820
/// # Parameters:
1921
/// - `dir`: the [`Path`] to recursively parse `.sql` files. Allows for coercion
@@ -33,7 +35,7 @@ pub mod files;
3335
pub fn generate_docs_from_dir<P: AsRef<Path>, S: AsRef<str>>(
3436
dir: P,
3537
deny_list: &[S],
36-
) -> Result<Vec<(PathBuf, SqlDocs)>, DocError> {
38+
) -> Result<Vec<(PathBuf, SqlFileDoc)>, DocError> {
3739
// Convert deny list to a `Vec<String>`
3840
let deny_vec: Vec<String> = deny_list.iter().map(|file| file.as_ref().to_string()).collect();
3941
// verify whether deny_list is empty and return correct `Option`
@@ -43,17 +45,17 @@ pub fn generate_docs_from_dir<P: AsRef<Path>, S: AsRef<str>>(
4345
// parse all files sql
4446
let parsed_files = ParsedSqlFileSet::parse_all(file_set)?;
4547
let mut sql_docs = Vec::new();
46-
// iterate on each file and generate the `SqlDocs` and associate with the `Path`
48+
// iterate on each file and generate the `SqlFileDoc` and associate with the `Path`
4749
for file in parsed_files.files() {
4850
let comments = Comments::parse_all_comments_from_file(file)?;
49-
let docs = SqlDocs::from_parsed_file(file, &comments)?;
51+
let docs = SqlFileDoc::from_parsed_file(file, &comments)?;
5052
let path = file.file().path().to_path_buf();
5153
sql_docs.push((path, docs));
5254
}
5355
Ok(sql_docs)
5456
}
5557

56-
/// Secondary Entry point. Returns a tuple of [`PathBuf`] and [`SqlDocs`].
58+
/// Secondary Entry point. Returns a tuple of [`PathBuf`] and [`SqlFileDoc`].
5759
/// Useful when no deny list is needed
5860
///
5961
/// # Parameters:
@@ -71,7 +73,7 @@ pub fn generate_docs_from_dir<P: AsRef<Path>, S: AsRef<str>>(
7173
/// ```
7274
pub fn generate_docs_from_dir_no_deny<P: AsRef<Path>>(
7375
dir: P,
74-
) -> Result<Vec<(PathBuf, SqlDocs)>, DocError> {
76+
) -> Result<Vec<(PathBuf, SqlFileDoc)>, DocError> {
7577
generate_docs_from_dir::<P, &str>(dir, &[])
7678
}
7779

@@ -109,7 +111,7 @@ mod tests {
109111
let parsed_doc_table_doc = parsed_doc.tables()[i]
110112
.doc()
111113
.as_ref()
112-
.map_or_else(|| panic!("unable to find SqlDocs table doc"), |val| val);
114+
.map_or_else(|| panic!("unable to find SqlFileDoc table doc"), |val| val);
113115
assert_eq!(parsed_doc_table_doc, table_comments[i]);
114116
}
115117
let user_columns = ["id", "username", "email", "created_at"];

src/sql_doc.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//! Module for the top level `SqlDoc` structure.
2+
3+
use std::path::{Path, PathBuf};
4+
5+
use crate::{
6+
ast::ParsedSqlFileSet,
7+
comments::Comments,
8+
docs::{SqlFileDoc, TableDoc, ColumnDoc},
9+
error::DocError,
10+
files::SqlFileSet,
11+
};
12+
13+
pub struct SqlDoc {
14+
// e.g. all tables across all files
15+
tables: Vec<TableDoc>, // or a map keyed by (schema, name)
16+
// optionally: keep per-file docs too
17+
files: Vec<(PathBuf, SqlFileDoc)>,
18+
}
19+
20+
pub struct SqlDocBuilder {
21+
source: SqlFileDocource,
22+
deny: Vec<String>,
23+
}
24+
25+
enum SqlFileDocource {
26+
Dir(PathBuf),
27+
File(PathBuf),
28+
}
29+
30+
impl SqlDoc {
31+
pub fn from_dir<P: AsRef<Path>>(root: P) -> SqlDocBuilder {
32+
SqlDocBuilder {
33+
source: SqlFileDocource::Dir(root.as_ref().to_path_buf()),
34+
deny: Vec::new(),
35+
}
36+
}
37+
38+
pub fn from_path<P: AsRef<Path>>(path: P) -> SqlDocBuilder {
39+
SqlDocBuilder {
40+
source: SqlFileDocource::File(path.as_ref().to_path_buf()),
41+
deny: Vec::new(),
42+
}
43+
}
44+
45+
// later:
46+
// pub fn table(&self, name: &str) -> Result<&TableDoc, DocError> { ... }
47+
// pub fn table_with_schema(&self, schema: &str, name: &str) -> Result<&TableDoc, DocError> { ... }
48+
}
49+
50+
impl SqlDocBuilder {
51+
pub fn deny<S: AsRef<str>>(mut self, pattern: S) -> Self {
52+
self.deny.push(pattern.as_ref().to_string());
53+
self
54+
}
55+
56+
pub fn build(self) -> Result<SqlDoc, DocError> {
57+
match self.source {
58+
SqlFileDocource::Dir(root) => {
59+
// Use your existing directory logic here
60+
}
61+
SqlFileDocource::File(path) => {
62+
// Same plumbing but for a single file
63+
}
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)