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
67
68
69
70
71
72
73
74
75
76
|
use serde::Deserialize;
use std::{collections::HashMap, fmt, fs::File, io, path::Path};
use walkdir::WalkDir;
use crate::types::*;
#[derive(Clone, Debug, Deserialize)]
pub struct Recipe {
pub tasks: HashMap<String, Task>,
}
#[derive(Debug)]
pub enum Error {
IOError(io::Error),
YAMLError(serde_yaml::Error),
}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Error::IOError(err)
}
}
impl From<serde_yaml::Error> for Error {
fn from(err: serde_yaml::Error) -> Self {
Error::YAMLError(err)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::IOError(err) => write!(f, "IO error: {}", err),
Error::YAMLError(err) => write!(f, "YAML error: {}", err),
}
}
}
impl std::error::Error for Error {}
pub type Result<T> = std::result::Result<T, Error>;
pub fn read_recipe(path: &Path) -> Result<Recipe> {
let f = File::open(path)?;
let recipe: Recipe = serde_yaml::from_reader(f)?;
Ok(recipe)
}
fn is_yml(path: &Path) -> bool {
path.extension() == Some("yml".as_ref())
}
pub fn read_recipes(path: &Path) -> Result<HashMap<String, Recipe>> {
let mut ret = HashMap::new();
for entry in WalkDir::new(path)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| {
let path = e.path();
path.is_file() && is_yml(path)
}) {
let path = entry.path();
let basename = match path.file_stem().map(|n| n.to_str()) {
Some(Some(v)) => v,
_ => continue,
};
let recipe = read_recipe(path)?;
ret.insert(basename.to_string(), recipe);
}
Ok(ret)
}
|