Skip to content

Commit b7aadc6

Browse files
committed
Refactoring crate for clarity and idiomatic
1 parent ea835af commit b7aadc6

File tree

5 files changed

+130
-135
lines changed

5 files changed

+130
-135
lines changed

src/ast.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Module for parsing the SQL and then using the resulting AST that will later
22
//! be used in `comments` module
3-
use std::path::Path;
3+
use std::path::{Path, PathBuf};
44

55
use sqlparser::{
66
ast::Statement,
@@ -46,6 +46,12 @@ impl ParsedSqlFile {
4646
self.file.path()
4747
}
4848

49+
/// Getter that returns an [`PathBuf`] for the path rather than `&Path`
50+
#[must_use]
51+
pub fn path_into_path_buf(&self) -> PathBuf {
52+
self.file.path_into_path_buf()
53+
}
54+
4955
/// Getter for the file's content
5056
#[must_use]
5157
pub fn content(&self) -> &str {

src/docs.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ impl TableDoc {
8383
name: String,
8484
doc: Option<String>,
8585
columns: Vec<ColumnDoc>,
86+
file: Option<PathBuf>,
8687
) -> Self {
87-
Self { schema, name, doc, columns }
88+
Self { schema, name, doc, columns, file }
8889
}
8990

9091
/// Getter for the `Schema` of the table (if there is one)
@@ -207,8 +208,15 @@ impl SqlFileDoc {
207208
name,
208209
Some(comment.text().to_string()),
209210
column_docs,
211+
Some(file.path_into_path_buf()),
212+
),
213+
None => TableDoc::new(
214+
schema,
215+
name,
216+
None,
217+
column_docs,
218+
Some(file.path_into_path_buf()),
210219
),
211-
None => TableDoc::new(schema, name, None, column_docs),
212220
};
213221
tables.push(table_doc);
214222
}
@@ -301,6 +309,7 @@ mod tests {
301309
"user".to_string(),
302310
Some("The table for users".to_string()),
303311
columns,
312+
None,
304313
);
305314
let tables = vec![table_doc];
306315
let sql_doc = SqlFileDoc::new(tables);
@@ -365,6 +374,7 @@ mod tests {
365374
ColumnDoc::new("email".to_string(), None),
366375
ColumnDoc::new("created_at".to_string(), None),
367376
],
377+
None,
368378
),
369379
TableDoc::new(
370380
None,
@@ -377,6 +387,7 @@ mod tests {
377387
ColumnDoc::new("body".to_string(), None),
378388
ColumnDoc::new("published_at".to_string(), None),
379389
],
390+
None,
380391
),
381392
])
382393
}
@@ -398,6 +409,7 @@ mod tests {
398409
Some("When the user registered".to_string()),
399410
),
400411
],
412+
None,
401413
),
402414
TableDoc::new(
403415
None,
@@ -416,6 +428,7 @@ mod tests {
416428
Some("When the post was created".to_string()),
417429
),
418430
],
431+
None,
419432
),
420433
]);
421434
docs.push(first_docs);
@@ -440,6 +453,7 @@ mod tests {
440453
Some("When the user registered\nmultiline".to_string()),
441454
),
442455
],
456+
None,
443457
),
444458
TableDoc::new(
445459
None,
@@ -461,6 +475,7 @@ mod tests {
461475
Some("When the post was created\nmultiline".to_string()),
462476
),
463477
],
478+
None,
464479
),
465480
]);
466481
docs.push(second_docs);
@@ -486,8 +501,10 @@ mod tests {
486501
"table".to_string(),
487502
Some("table doc".to_string()),
488503
vec![col_doc],
504+
None,
489505
);
490-
let table_doc_no_doc = TableDoc::new(None, "table".to_string(), None, vec![col_doc_no_doc]);
506+
let table_doc_no_doc =
507+
TableDoc::new(None, "table".to_string(), None, vec![col_doc_no_doc], None);
491508
assert_eq!(table_doc.name(), "table");
492509
assert_eq!(table_doc.schema(), Some("schema"));
493510
assert_eq!(
@@ -600,6 +617,7 @@ mod tests {
600617
"users".into(),
601618
Some("table doc".into()),
602619
vec![col_with_doc.clone(), col_without_doc],
620+
None,
603621
);
604622

605623
let col_writes = count_writes(&col_with_doc);
@@ -634,7 +652,7 @@ mod tests {
634652

635653
#[test]
636654
fn table_doc_set_doc_updates_doc() {
637-
let mut table = TableDoc::new(None, "users".to_string(), None, Vec::new());
655+
let mut table = TableDoc::new(None, "users".to_string(), None, Vec::new(), None);
638656
assert_eq!(table.name(), "users");
639657
assert_eq!(table.schema(), None);
640658
assert_eq!(table.doc(), None);
@@ -654,6 +672,7 @@ mod tests {
654672
ColumnDoc::new("id".to_string(), None),
655673
ColumnDoc::new("username".to_string(), None),
656674
],
675+
None,
657676
);
658677

659678
{

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ mod tests {
184184
}
185185

186186
fn table_doc_for_test(name: &str) -> TableDoc {
187-
TableDoc::new(None, name.to_string(), None, vec![])
187+
TableDoc::new(None, name.to_string(), None, vec![], None)
188188
}
189189
#[test]
190190
fn test_doc_error_display_invalid_object_name() {

src/files.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ impl SqlFilesList {
7777
}
7878
}
7979

80+
impl From<SqlFilesList> for Vec<PathBuf> {
81+
fn from(value: SqlFilesList) -> Self {
82+
value.sql_files
83+
}
84+
}
85+
8086
/// Recursively scans the directory for `.sql` files.
8187
///
8288
/// This function:
@@ -125,6 +131,11 @@ impl SqlFile {
125131
pub fn path(&self) -> &Path {
126132
&self.path
127133
}
134+
/// Returns the [`PathBuf`] for the current path
135+
#[must_use]
136+
pub fn path_into_path_buf(&self) -> PathBuf {
137+
self.path.clone()
138+
}
128139

129140
/// Returns the raw SQL text contained in this file.
130141
#[must_use]

0 commit comments

Comments
 (0)