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
77
78
79
80
|
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use std::time::SystemTime;
use crate::skaldpress::error::SkaldpressError;
use crate::skaldpress::error::SP_COMPILE_FILE_EXTENSION_ERROR_2;
use crate::skaldpress::metadata_parser::extract_parse_yaml_metadata;
use crate::skaldpress::metadata_parser::YamlValue;
fn get_template_path(template: &str, template_dir: &str) -> PathBuf {
Path::new(&format!("{}{}", template_dir, template)).to_path_buf()
}
pub fn get_all_meta(
file_path: &Path,
template_dir: &str,
meta: HashMap<String, YamlValue>,
) -> Result<(HashMap<String, YamlValue>, String, SystemTime), SkaldpressError> {
let extension = file_path
.extension()
.unwrap_or(std::ffi::OsStr::new(""))
.to_str()
.ok_or(SkaldpressError::PathOperationError(
SP_COMPILE_FILE_EXTENSION_ERROR_2,
None,
))?;
let fs_metadata = match fs::metadata(&file_path) {
Ok(metadata) => metadata,
Err(e) => {
return Err(SkaldpressError::MetadataError(87, e));
}
};
let fs_modified = match fs_metadata.modified() {
Ok(r) => r,
Err(e) => {
return Err(SkaldpressError::MetadataError(89, e));
}
};
let file_content = fs::read_to_string(file_path).map_err(|e| {
SkaldpressError::FileReadError(
1,
e,
file_path.to_str().unwrap_or("unknown file").to_string(),
)
})?;
let (map_with_meta, _file_content) = match extract_parse_yaml_metadata(&file_content) {
Some((map, file_content)) => (map, file_content),
None => (HashMap::new(), file_content.as_str()),
};
let mut map_base = meta;
map_base.extend(map_with_meta);
let Some(template) = &map_base.get("template") else {
return Ok((map_base, extension.to_string(), fs_modified));
};
let template_file = get_template_path(
&TryInto::<String>::try_into(*template)
.map_err(|e| SkaldpressError::MetadataError(12, std::io::Error::other(e)))?,
template_dir,
);
let (mut map_templated, extension, template_fs_modified) =
get_all_meta(&template_file, template_dir, HashMap::new())?;
map_templated.extend(map_base);
// Shuld really add a cutsom extend function to the hashmap,
// so lists can be merged and such
Ok((
map_templated,
String::from(extension),
std::cmp::max(fs_modified, template_fs_modified),
))
}
|