forked from cornucopia-rs/cornucopia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_queries.rs
66 lines (59 loc) · 1.88 KB
/
read_queries.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use error::Error;
#[derive(Debug, Clone)]
pub(crate) struct ModuleInfo {
pub(crate) path: String,
pub(crate) name: String,
pub(crate) content: String,
}
/// Reads queries in the directory. Only .sql files are considered.
///
/// # Error
/// Returns an error if `dir_path` does not point to a valid directory or if a query file cannot be parsed.
pub(crate) fn read_query_modules(dir_path: &str) -> Result<Vec<ModuleInfo>, Error> {
let mut modules_info = Vec::new();
for entry_result in std::fs::read_dir(dir_path).map_err(|err| Error {
err,
path: String::from(dir_path),
})? {
// Directory entry
let entry = entry_result.map_err(|err| Error {
err,
path: dir_path.to_owned(),
})?;
let path_buf = entry.path();
// Check we're dealing with a .sql file
if path_buf
.extension()
.map(|extension| extension == "sql")
.unwrap_or_default()
{
let module_name = path_buf
.file_stem()
.expect("is a file")
.to_str()
.expect("file name is valid utf8")
.to_string();
let file_contents = std::fs::read_to_string(&path_buf).map_err(|err| Error {
err,
path: dir_path.to_owned(),
})?;
modules_info.push(ModuleInfo {
path: String::from(path_buf.to_string_lossy()),
name: module_name,
content: file_contents,
});
} else {
continue;
}
}
Ok(modules_info)
}
pub(crate) mod error {
use thiserror::Error as ThisError;
#[derive(Debug, ThisError)]
#[error("Error while reading queries [path: \"{path}\"]: {err}.")]
pub struct Error {
pub(crate) err: std::io::Error,
pub(crate) path: String,
}
}