Skip to content

Commit 4d58832

Browse files
committed
Refactoring and progagating changes
1 parent 13c4002 commit 4d58832

File tree

5 files changed

+108
-91
lines changed

5 files changed

+108
-91
lines changed

src/ast.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ impl ParsedSqlFile {
4242

4343
/// Getter method for returning the current object's file's path
4444
#[must_use]
45-
pub fn path(&self) -> &Path {
45+
pub fn path(&self) -> Option<&Path> {
4646
self.file.path()
4747
}
4848

4949
/// Getter that returns an [`PathBuf`] for the path rather than `&Path`
5050
#[must_use]
51-
pub fn path_into_path_buf(&self) -> PathBuf {
51+
pub fn path_into_path_buf(&self) -> Option<PathBuf> {
5252
self.file.path_into_path_buf()
5353
}
5454

@@ -108,7 +108,7 @@ mod tests {
108108
fs::write(&file_path, sql)?;
109109
let sql_file = SqlFile::new(&file_path)?;
110110
let parsed = ParsedSqlFile::parse(sql_file)?;
111-
assert_eq!(parsed.path(), file_path.as_path());
111+
assert_eq!(parsed.path(), Some(file_path.as_path()));
112112
assert_eq!(parsed.content(), sql);
113113
assert_eq!(parsed.statements().len(), 1);
114114
let _ = fs::remove_dir_all(&base);

src/comments.rs

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -435,9 +435,9 @@ mod tests {
435435
let filename = file
436436
.file()
437437
.path()
438-
.file_name()
438+
.and_then(|p| p.file_name())
439439
.and_then(|s| s.to_str())
440-
.unwrap_or_else(|| panic!("test file name should be valid UTF-8"));
440+
.ok_or("Should have a file name")?;
441441

442442
match filename {
443443
"with_single_line_comments.sql" => {
@@ -536,17 +536,16 @@ mod tests {
536536
let path = Path::new("sql_files");
537537
let set = SqlFileSet::new(path, &[])?;
538538
let parsed_set = ParsedSqlFileSet::parse_all(set)?;
539-
let file = {
540-
let Some(f) = parsed_set.files().iter().find(|f| {
541-
let Some(path) = f.file().path().to_str() else {
542-
return false;
543-
};
544-
path.ends_with("with_single_line_comments.sql")
545-
}) else {
546-
panic!("with_single_line_comments.sql should be present");
547-
};
548-
f
549-
};
539+
let file = parsed_set
540+
.files()
541+
.iter()
542+
.find(|f| {
543+
f.file()
544+
.path()
545+
.and_then(|p| p.to_str())
546+
.is_some_and(|p| p.ends_with("with_single_line_comments.sql"))
547+
})
548+
.ok_or("with_single_line_comments.sql should be present")?;
550549

551550
let comments = Comments::parse_all_comments_from_file(file)?;
552551
let comments = comments.comments();
@@ -574,17 +573,17 @@ mod tests {
574573
let path = Path::new("sql_files");
575574
let set = SqlFileSet::new(path, &[])?;
576575
let parsed_set = ParsedSqlFileSet::parse_all(set)?;
577-
let file = {
578-
let Some(f) = parsed_set.files().iter().find(|f| {
579-
let Some(path) = f.file().path().to_str() else {
580-
return false;
581-
};
582-
path.ends_with("with_multiline_comments.sql")
583-
}) else {
584-
panic!("with_multiline_comments.sql should be present");
585-
};
586-
f
587-
};
576+
let file = parsed_set
577+
.files()
578+
.iter()
579+
.find(|f| {
580+
f.file()
581+
.path()
582+
.and_then(|p| p.to_str())
583+
.is_some_and(|p| p.ends_with("with_multiline_comments.sql"))
584+
})
585+
.ok_or("with_multiline_comments.sql should be present")?;
586+
588587
let comments = Comments::parse_all_comments_from_file(file)?;
589588
let comments = comments.comments();
590589
assert_eq!(comments.len(), 11);

src/docs.rs

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub struct TableDoc {
6666
name: String,
6767
doc: Option<String>,
6868
columns: Vec<ColumnDoc>,
69-
file: Option<PathBuf>,
69+
path: Option<PathBuf>,
7070
}
7171

7272
impl TableDoc {
@@ -83,9 +83,9 @@ impl TableDoc {
8383
name: String,
8484
doc: Option<String>,
8585
columns: Vec<ColumnDoc>,
86-
file: Option<PathBuf>,
86+
path: Option<PathBuf>,
8787
) -> Self {
88-
Self { schema, name, doc, columns, file }
88+
Self { schema, name, doc, columns, path }
8989
}
9090

9191
/// Getter for the `Schema` of the table (if there is one)
@@ -113,6 +113,11 @@ impl TableDoc {
113113
self.doc = Some(doc.into());
114114
}
115115

116+
/// Setter for updating the table [`PathBuf`] source
117+
pub fn set_path(&mut self, path: Option<impl Into<PathBuf>>) {
118+
self.path = path.map(Into::into);
119+
}
120+
116121
/// Getter for the `columns` field
117122
#[must_use]
118123
#[allow(clippy::missing_const_for_fn)]
@@ -202,22 +207,13 @@ impl SqlFileDoc {
202207
}
203208
let table_leading = comments.leading_comment(table_start);
204209
let (schema, name) = schema_and_table(&table.name)?;
205-
let table_doc = match table_leading {
206-
Some(comment) => TableDoc::new(
207-
schema,
208-
name,
209-
Some(comment.text().to_string()),
210-
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()),
219-
),
220-
};
210+
let table_doc = TableDoc::new(
211+
schema,
212+
name,
213+
table_leading.as_ref().map(|c| c.text().to_string()),
214+
column_docs,
215+
file.path_into_path_buf(),
216+
);
221217
tables.push(table_doc);
222218
}
223219
// can add support for other types of statements below
@@ -233,6 +229,12 @@ impl SqlFileDoc {
233229
pub fn tables(&self) -> &[TableDoc] {
234230
&self.tables
235231
}
232+
233+
/// Getter that returns a mutable reference to the [`TableDoc`] vec
234+
pub fn tables_mut(&mut self) -> &mut [TableDoc] {
235+
&mut self.tables
236+
}
237+
236238
/// Returns the number fo tables in the `SqlFileDoc`
237239
#[must_use]
238240
pub fn number_of_tables(&self) -> usize {
@@ -336,32 +338,43 @@ mod tests {
336338
let filename = file
337339
.file()
338340
.path()
339-
.file_name()
341+
.and_then(|p| p.file_name())
340342
.and_then(|s| s.to_str())
341-
.unwrap_or_else(|| panic!("unable to find file name"));
343+
.ok_or("unable to parse file")?;
344+
345+
let got = docs?;
346+
let file_path = file.file().path().ok_or("missing path")?;
342347

343348
match filename {
344349
"with_single_line_comments.sql" | "with_mixed_comments.sql" => {
345-
assert_eq!(&docs?, &expected_values[0]);
350+
let expected = with_path(expected_values[0].clone(), file_path);
351+
assert_eq!(&got, &expected);
346352
}
347353
"with_multiline_comments.sql" => {
348-
assert_eq!(&docs?, &expected_values[1]);
354+
let expected = with_path(expected_values[1].clone(), file_path);
355+
assert_eq!(&got, &expected);
349356
}
350357
"without_comments.sql" => {
351-
let expected = expected_without_comments_docs();
352-
assert_eq!(&docs?, &expected);
353-
}
354-
other => {
355-
unreachable!(
356-
"unexpected test file {other}; directory should only contain known test files"
357-
);
358+
let expected = with_path(expected_without_comments_docs(), file_path);
359+
assert_eq!(&got, &expected);
358360
}
361+
_ => unreachable!(),
359362
}
360363
}
361364

362365
Ok(())
363366
}
364367

368+
fn with_path(mut doc: SqlFileDoc, path: &std::path::Path) -> SqlFileDoc {
369+
let pb = path.to_path_buf();
370+
371+
for table in doc.tables_mut() {
372+
table.set_path(Some(pb.clone()));
373+
}
374+
375+
doc
376+
}
377+
365378
fn expected_without_comments_docs() -> SqlFileDoc {
366379
SqlFileDoc::new(vec![
367380
TableDoc::new(

src/files.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ fn recursive_dir_scan(path: &Path) -> io::Result<Vec<PathBuf>> {
107107
/// A single `.sql` file and its loaded contents.
108108
#[derive(Debug)]
109109
pub struct SqlFile {
110-
path: PathBuf,
110+
path: Option<PathBuf>,
111111
content: String,
112112
}
113113

@@ -121,17 +121,23 @@ impl SqlFile {
121121
/// Returns an [`io::Error`] if the file cannot be read.
122122
pub fn new(path: &Path) -> io::Result<Self> {
123123
let content = fs::read_to_string(path)?;
124-
Ok(Self { path: path.to_owned(), content })
124+
Ok(Self { path: Some(path.to_owned()), content })
125+
}
126+
127+
/// Creates an [`SqlFile`] from a string with a dummy path
128+
#[must_use]
129+
pub const fn new_from_str(content: String) -> Self {
130+
Self { path: None, content }
125131
}
126132

127133
/// Returns the filesystem path associated with this SQL file.
128134
#[must_use]
129-
pub fn path(&self) -> &Path {
130-
&self.path
135+
pub fn path(&self) -> Option<&Path> {
136+
self.path.as_deref()
131137
}
132138
/// Returns the [`PathBuf`] for the current path
133139
#[must_use]
134-
pub fn path_into_path_buf(&self) -> PathBuf {
140+
pub fn path_into_path_buf(&self) -> Option<PathBuf> {
135141
self.path.clone()
136142
}
137143

@@ -290,7 +296,9 @@ mod tests {
290296
let sql_file_set = SqlFileSet::new(&base, &[])?;
291297
for file in sql_file_set.iter() {
292298
assert_eq!(file.content, sql_statement);
293-
found.push(&file.path);
299+
if let Some(p) = &file.path {
300+
found.push(p);
301+
}
294302
}
295303
assert_eq!(found, expected);
296304
let _ = fs::remove_dir_all(&base);

0 commit comments

Comments
 (0)