Skip to content

Commit 364a35d

Browse files
committed
ctest: Simplify render error handling
The types of templates are statically known, as is the error type. We can give this a strong typing rather than using strings.
1 parent 892cf58 commit 364a35d

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

ctest-next/src/generator.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use thiserror::Error;
1010
use crate::ffi_items::FfiItems;
1111
use crate::template::{CTestTemplate, RustTestTemplate};
1212
use crate::{
13-
Const, Field, MapInput, Parameter, Result, Static, Struct, Type, VolatileItemKind, expand,
13+
Const, Field, MapInput, Parameter, Result, Static, Struct, TranslationError, Type,
14+
VolatileItemKind, expand,
1415
};
1516

1617
/// A function that takes a mappable input and returns its mapping as Some, otherwise
@@ -46,6 +47,12 @@ pub enum GenerationError {
4647
MacroExpansion(PathBuf, String),
4748
#[error("unable to parse expanded crate {0}: {1}")]
4849
RustSyntax(String, String),
50+
#[error("unable to prepare template input: {0}")]
51+
Translation(#[from] TranslationError),
52+
#[error("unable to render Rust template: {0}")]
53+
RustTemplateRender(askama::Error),
54+
#[error("unable to render C template: {0}")]
55+
CTemplateRender(askama::Error),
4956
#[error("unable to render {0} template: {1}")]
5057
TemplateRender(String, String),
5158
#[error("unable to create or write template file: {0}")]
@@ -605,10 +612,9 @@ impl TestGenerator {
605612
s.push('\n');
606613
};
607614

608-
let mut rust_file = RustTestTemplate::new(&ffi_items, self)
609-
.map_err(|e| GenerationError::TemplateRender("Rust".to_string(), e.to_string()))?
615+
let mut rust_file = RustTestTemplate::new(&ffi_items, self)?
610616
.render()
611-
.map_err(|e| GenerationError::TemplateRender("Rust".to_string(), e.to_string()))?;
617+
.map_err(GenerationError::RustTemplateRender)?;
612618
ensure_trailing_newline(&mut rust_file);
613619

614620
// Generate the Rust side of the tests.
@@ -617,10 +623,9 @@ impl TestGenerator {
617623
.write_all(rust_file.as_bytes())
618624
.map_err(GenerationError::OsError)?;
619625

620-
let mut c_file = CTestTemplate::new(&ffi_items, self)
621-
.map_err(|e| GenerationError::TemplateRender("C".to_string(), e.to_string()))?
626+
let mut c_file = CTestTemplate::new(&ffi_items, self)?
622627
.render()
623-
.map_err(|e| GenerationError::TemplateRender("C".to_string(), e.to_string()))?;
628+
.map_err(GenerationError::CTemplateRender)?;
624629
ensure_trailing_newline(&mut c_file);
625630

626631
// Generate the C/Cxx side of the tests.

0 commit comments

Comments
 (0)