summaryrefslogtreecommitdiff
path: root/src/codegen.rs
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2022-05-06 08:39:14 +0300
committerLars Wirzenius <liw@liw.fi>2022-05-06 09:34:28 +0300
commit2b7160ebdf950ef872f785336fcfe17e0c4cb347 (patch)
tree216fb5b9c2cf5fa6121e1b2d357508a0c675e3e9 /src/codegen.rs
parent6498b2eecb21ad87069be8f501f35374745106d9 (diff)
downloadsubplot-2b7160ebdf950ef872f785336fcfe17e0c4cb347.tar.gz
refactor! split SubplotError::IoError into more specific errors
Replace SubplotError::IoError with ::Spawn, ::WriteToChild, ::WaitForChild, ::ReadFile, ::CreateFile, ::Writefile. IoError was a catchall error and as such, so generic that it didn't help the user to figure out what actually is wrong. For example, there was no indication what operation was attempted or on what file. The new error variants are specific. Sponsored-by: author
Diffstat (limited to 'src/codegen.rs')
-rw-r--r--src/codegen.rs13
1 files changed, 9 insertions, 4 deletions
diff --git a/src/codegen.rs b/src/codegen.rs
index b940b4d..5c4255f 100644
--- a/src/codegen.rs
+++ b/src/codegen.rs
@@ -62,12 +62,15 @@ fn tera(tmplspec: &TemplateSpec, templatename: &str) -> Result<Tera, SubplotErro
let dirname = tmplspec.template_filename().parent().unwrap();
for helper in tmplspec.helpers() {
let helper_path = dirname.join(helper);
- let helper_content = resource::read_as_string(helper_path, Some(templatename))?;
+ let helper_content = resource::read_as_string(&helper_path, Some(templatename))
+ .map_err(|err| SubplotError::ReadFile(helper_path.clone(), err))?;
let helper_name = helper.display().to_string();
tera.add_raw_template(&helper_name, &helper_content)
.map_err(|err| SubplotError::TemplateError(helper_name.to_string(), err))?;
}
- let template = resource::read_as_string(tmplspec.template_filename(), Some(templatename))?;
+ let path = tmplspec.template_filename();
+ let template = resource::read_as_string(path, Some(templatename))
+ .map_err(|err| SubplotError::ReadFile(path.to_path_buf(), err))?;
tera.add_raw_template("template", &template)
.map_err(|err| {
SubplotError::TemplateError(tmplspec.template_filename().display().to_string(), err)
@@ -76,8 +79,10 @@ fn tera(tmplspec: &TemplateSpec, templatename: &str) -> Result<Tera, SubplotErro
}
fn write(filename: &Path, content: &str) -> Result<(), SubplotError> {
- let mut f: File = File::create(filename)?;
- f.write_all(content.as_bytes())?;
+ let mut f: File = File::create(filename)
+ .map_err(|err| SubplotError::CreateFile(filename.to_path_buf(), err))?;
+ f.write_all(content.as_bytes())
+ .map_err(|err| SubplotError::WriteFile(filename.to_path_buf(), err))?;
Ok(())
}