Skip to content

Commit 3709ec6

Browse files
committed
docs mod should be complete. next need unit tests. Closes #2
1 parent 725d590 commit 3709ec6

3 files changed

Lines changed: 82 additions & 6 deletions

File tree

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/comments.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,17 @@ impl Comments {
375375
pub fn comments(&self) -> &[Comment] {
376376
&self.comments
377377
}
378+
379+
/// Helper method for checking and finding for a comment before a specific
380+
/// line
381+
///
382+
/// # Parameters
383+
/// - `self` an instance of [`Comments`]
384+
/// - `line` an `u64` value representing the desired line to check above.
385+
#[must_use]
386+
pub fn leading_comment(&self, line: u64) -> Option<&Comment> {
387+
self.comments().iter().rev().find(|comment| comment.span().end().line() + 1 == line)
388+
}
378389
}
379390

380391
#[cfg(test)]

src/docs.rs

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Module for parsing sql and comments and returning only comments connected to
22
//! statements
3+
use sqlparser::ast::{Spanned, Statement};
4+
35
use crate::{ast::ParsedSqlFile, comments::Comments};
46

57
/// Structure for containing the `name` of the `Column` and an [`Option`] for
@@ -9,6 +11,17 @@ pub struct ColumnDoc {
911
doc: Option<String>,
1012
}
1113
impl ColumnDoc {
14+
/// Creates a new [`ColumnDoc`]
15+
///
16+
/// # Parameters
17+
/// - name: `String` - the name of the column
18+
/// - doc: `Option<String>` the comment for the column
19+
#[must_use]
20+
#[allow(clippy::missing_const_for_fn)]
21+
pub fn new(name: String, doc: Option<String>) -> Self {
22+
Self { name, doc }
23+
}
24+
1225
/// Getter for the `name` field
1326
#[must_use]
1427
pub fn name(&self) -> &str {
@@ -17,6 +30,7 @@ impl ColumnDoc {
1730

1831
/// Getter for the field `doc`
1932
#[must_use]
33+
#[allow(clippy::missing_const_for_fn)]
2034
pub fn doc(&self) -> &Option<String> {
2135
&self.doc
2236
}
@@ -31,21 +45,35 @@ pub struct TableDoc {
3145
}
3246

3347
impl TableDoc {
48+
/// Creates a new [`TableDoc`]
49+
///
50+
/// # Parameters
51+
/// - name: `String` - the name of the table
52+
/// - doc: `Option<String>` of the comment for table
53+
/// - columns: the `Vec<ColumnDoc>` of all [`ColumnDoc`] for this table
54+
#[must_use]
55+
#[allow(clippy::missing_const_for_fn)]
56+
pub fn new(name: String, doc: Option<String>, columns: Vec<ColumnDoc>) -> Self {
57+
Self { name, doc, columns }
58+
}
3459

3560
/// Getter for the `name` field
3661
#[must_use]
62+
#[allow(clippy::missing_const_for_fn)]
3763
pub fn name(&self) -> &str {
3864
&self.name
3965
}
4066

41-
/// Getter for the `doc` field
67+
/// Getter for the `doc` field
4268
#[must_use]
69+
#[allow(clippy::missing_const_for_fn)]
4370
pub fn doc(&self) -> &Option<String> {
4471
&self.doc
4572
}
4673

4774
/// Getter for the `columns` field
4875
#[must_use]
76+
#[allow(clippy::missing_const_for_fn)]
4977
pub fn columns(&self) -> &[ColumnDoc] {
5078
&self.columns
5179
}
@@ -68,7 +96,44 @@ impl SqlDocs {
6896
/// tables and columns
6997
#[must_use]
7098
pub fn from_parsed_file(file: &ParsedSqlFile, comments: &Comments) -> Self {
71-
todo!()
99+
let mut tables = Vec::new();
100+
for statement in file.statements() {
101+
#[allow(clippy::single_match)]
102+
match statement {
103+
Statement::CreateTable(table) => {
104+
let table_start_line = table.span().start.line;
105+
let mut column_docs = Vec::new();
106+
for column in &table.columns {
107+
let column_start = column.span().start.line;
108+
let column_leading = comments.leading_comment(column_start);
109+
let column_name = column.name.value.clone();
110+
let column_doc = match column_leading {
111+
Some(col_comment) => {
112+
ColumnDoc::new(column_name, Some(col_comment.text().to_string()))
113+
}
114+
None => ColumnDoc::new(column_name, None),
115+
};
116+
column_docs.push(column_doc);
117+
}
118+
let table_leading = comments.leading_comment(table_start_line);
119+
let table_doc = match table_leading {
120+
Some(comment) => {
121+
TableDoc::new(
122+
table.name.to_string(),
123+
Some(comment.text().to_string()),
124+
column_docs,
125+
)
126+
}
127+
None => TableDoc::new(table.name.to_string(), None, column_docs),
128+
};
129+
tables.push(table_doc);
130+
}
131+
// can add support for other types of statements below
132+
_ => {}
133+
}
134+
}
135+
136+
Self { tables }
72137
}
73138

74139
/// Getter function to get a slice of [`TableDoc`]

0 commit comments

Comments
 (0)