Skip to content

Commit 68957b1

Browse files
committed
file module finished (besides unit tests)
1 parent 856f046 commit 68957b1

2 files changed

Lines changed: 38 additions & 7 deletions

File tree

src/files.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! The module for working with and loading the content from `sql` files
22
use std::fs;
33
use std::io;
4+
use std::io::Error;
45
use std::path::{Path, PathBuf};
56

67
pub struct SqlFilesList {
@@ -14,6 +15,9 @@ impl SqlFilesList {
1415
sql_files: recursive_scan,
1516
})
1617
}
18+
pub fn sql_files(&self) -> &Vec<PathBuf> {
19+
return &self.sql_files;
20+
}
1721
}
1822

1923
/// Helper function to recursively scan the specified directory and collect all sql files found
@@ -22,7 +26,10 @@ fn recursive_dir_scan(path: &Path) -> io::Result<Vec<PathBuf>> {
2226
for entry in fs::read_dir(path)? {
2327
let entry = entry?;
2428
let path = entry.path();
25-
if path.is_file() && path.extension().unwrap() == "sql" {
29+
if path.is_file() && path
30+
.extension()
31+
.and_then(|ext| ext.to_str())
32+
== Some("sql") {
2633
sql_files.push(path);
2734
} else if path.is_dir() {
2835
let nested = recursive_dir_scan(&path)?;
@@ -32,15 +39,39 @@ fn recursive_dir_scan(path: &Path) -> io::Result<Vec<PathBuf>> {
3239
Ok(sql_files)
3340
}
3441

42+
#[derive(Debug)]
3543
pub struct SqlFile {
3644
path: PathBuf,
3745
content: String,
3846
}
47+
impl SqlFile {
48+
pub fn new(path: &Path) -> io::Result<SqlFile> {
49+
let content = fs::read_to_string(path)?;
50+
Ok(SqlFile { path: path.to_owned(), content })
51+
}
52+
}
3953

54+
#[derive(Debug)]
4055
pub struct SqlFileSet {
4156
files_contents: Vec<SqlFile>
4257
}
4358

59+
impl SqlFileSet {
60+
pub fn new(path: &Path) -> io::Result<SqlFileSet> {
61+
let sql_files_list = SqlFilesList::new(path)?;
62+
63+
let files_contents = sql_files_list
64+
.sql_files()
65+
.iter()
66+
.map(|p| SqlFile::new(p))
67+
.collect::<io::Result<Vec<_>>>()?;
68+
69+
Ok(SqlFileSet { files_contents })
70+
}
71+
pub fn iter(&self) -> impl Iterator<Item = &SqlFile> {
72+
self.files_contents.iter()
73+
}
74+
}
4475

4576
#[cfg(test)]
4677
mod tests {

src/main.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
//! main file. for testing. shouldn't be part of crate
22
use std::fs;
33
use std::io;
4+
use std::path::Path;
45

56
pub mod files;
67

78
fn main() -> io::Result<()> {
8-
let path = "/home/alex/Projects/sql-docs/sql_files/";
9+
let path: &Path = Path::new("/home/alex/Projects/sql-docs/sql_files/");
910

10-
println!("Contents of directory '{}':", path);
11-
for entry in fs::read_dir(path)? {
12-
let entry = entry?;
13-
let path = entry.path();
14-
println!(" {}", path.display());
11+
let sql_file_set = files::SqlFileSet::new(path)?;
12+
13+
for sql in sql_file_set.iter() {
14+
println!("{:?}",sql);
1515
}
1616

1717
Ok(())

0 commit comments

Comments
 (0)