Skip to content

Commit 1539a5a

Browse files
committed
work towards allowing paths via str inputs to be added to TableDoc
1 parent 1a876d9 commit 1539a5a

2 files changed

Lines changed: 34 additions & 17 deletions

File tree

src/files.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,12 @@ impl SqlFilesList {
6363

6464
/// Returns discovered `.sql` files in discovery order (filesystem-dependent).
6565
#[must_use]
66-
pub fn sql_files(&self) -> &[PathBuf] {
67-
&self.sql_files
68-
}
69-
70-
/// Returns a sorted view of the discovered `.sql` files.
71-
#[must_use]
72-
pub fn sql_files_sorted(&self) -> Vec<&PathBuf> {
66+
pub fn sql_files(&self) -> Vec<&PathBuf> {
7367
let mut files: Vec<&PathBuf> = self.sql_files.iter().collect();
7468
files.sort();
7569
files
7670
}
71+
7772
}
7873

7974
impl From<SqlFilesList> for Vec<PathBuf> {
@@ -116,10 +111,10 @@ impl SqlFile {
116111
Ok(Self { path: Some(path.to_owned()), content })
117112
}
118113

119-
/// Creates an [`SqlFile`] from a string with a dummy path
114+
/// Creates an [`SqlFile`] from a a [`String`] and a [`Option<PathBuf>`]
120115
#[must_use]
121-
pub const fn new_from_str(content: String) -> Self {
122-
Self { path: None, content }
116+
pub const fn new_from_str(content: String, path: Option<PathBuf>) -> Self {
117+
Self { path, content }
123118
}
124119

125120
/// Returns the filesystem path associated with this SQL file.
@@ -259,7 +254,7 @@ mod tests {
259254
let sql_file_list = SqlFilesList::new(&base, &[])?;
260255
let mut expected = vec![&file1, &file2];
261256
expected.sort();
262-
assert_eq!(sql_file_list.sql_files_sorted(), expected);
257+
assert_eq!(sql_file_list.sql_files(), expected);
263258
let _ = fs::remove_dir_all(&base);
264259

265260
Ok(())

src/sql_doc.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::{
44
path::{Path, PathBuf},
5-
str::FromStr,
5+
str::FromStr, vec,
66
};
77

88
use crate::{
@@ -49,6 +49,7 @@ enum SqlFileDocSource<'a> {
4949
File(PathBuf),
5050
Files(Vec<PathBuf>),
5151
FromString(&'a str),
52+
FromStringsWithPaths(&'a[(String, PathBuf)])
5253
}
5354

5455
impl SqlDoc {
@@ -97,6 +98,17 @@ impl SqlDoc {
9798
}
9899
}
99100

101+
/// Creates a builder from a vec of tuples containing the `sql` as [`String`] and the path as [`PathBuf`]
102+
#[must_use]
103+
pub const fn builder_from_strs_with_paths(string_with_path: &[(String, PathBuf)]) -> SqlDocBuilder<'_> {
104+
SqlDocBuilder {
105+
source: SqlFileDocSource::FromStringsWithPaths(string_with_path),
106+
deny: Vec::new(),
107+
multiline_flat: MultiFlatten::NoFlat
108+
}
109+
}
110+
111+
100112
/// Method for finding a specific [`TableDoc`] by `name`
101113
///
102114
/// # Parameters
@@ -209,9 +221,10 @@ impl SqlDocBuilder<'_> {
209221
vec![sql_doc]
210222
}
211223
SqlFileDocSource::FromString(content) => {
212-
let sql_docs = generate_docs_str(content)?;
224+
let sql_docs = generate_docs_str(content, None)?;
213225
vec![sql_docs]
214-
}
226+
},
227+
SqlFileDocSource::FromStringsWithPaths(strings_paths ) => generate_docs_from_strs_with_paths(strings_paths)?,
215228
SqlFileDocSource::Files(files) => generate_docs_from_files(files)?,
216229
};
217230
let num_of_tables = docs.iter().map(super::docs::SqlFileDoc::number_of_tables).sum();
@@ -269,7 +282,7 @@ fn generate_docs_from_dir<P: AsRef<Path>, S: AsRef<str>>(
269282
let deny_list: Vec<String> = deny.iter().map(|file| file.as_ref().to_owned()).collect();
270283
let file_set = SqlFilesList::new(source, &deny_list)?;
271284
let mut sql_docs = Vec::new();
272-
for file in file_set.sql_files_sorted() {
285+
for file in file_set.sql_files() {
273286
let docs = generate_docs_from_file(file)?;
274287
sql_docs.push(docs);
275288
}
@@ -293,14 +306,23 @@ fn generate_docs_from_file<P: AsRef<Path>>(source: P) -> Result<SqlFileDoc, DocE
293306
Ok(docs)
294307
}
295308

296-
fn generate_docs_str(content: &str) -> Result<SqlFileDoc, DocError> {
297-
let dummy_file = SqlFile::new_from_str(content.to_owned());
309+
fn generate_docs_str(content: &str, path: Option<PathBuf>) -> Result<SqlFileDoc, DocError> {
310+
let dummy_file = SqlFile::new_from_str(content.to_owned(), path);
298311
let parsed_sql = ParsedSqlFile::parse(dummy_file)?;
299312
let comments = Comments::parse_all_comments_from_file(&parsed_sql)?;
300313
let docs = SqlFileDoc::from_parsed_file(&parsed_sql, &comments)?;
301314
Ok(docs)
302315
}
303316

317+
fn generate_docs_from_strs_with_paths(strings_with_paths: &[(String, PathBuf)]) -> Result<Vec<SqlFileDoc>, DocError> {
318+
let mut docs = Vec::new();
319+
for (content,path) in strings_with_paths {
320+
docs.push(generate_docs_str(content, Some(path.to_owned()))?);
321+
}
322+
323+
Ok(docs)
324+
}
325+
304326
#[cfg(test)]
305327
mod tests {
306328
use std::{

0 commit comments

Comments
 (0)