bundled translation: Better error reporting

forward the error up the stack instead of panicking while producing the llr
This commit is contained in:
Olivier Goffart 2024-11-19 18:44:09 +01:00
parent 3f8084cdc0
commit 73b549a42c
6 changed files with 21 additions and 13 deletions

View file

@ -459,7 +459,8 @@ pub fn compile_with_output(
let output_file =
std::fs::File::create(&output_rust_file_path).map_err(CompileError::SaveError)?;
let mut code_formatter = CodeFormatter::new(BufWriter::new(output_file));
let generated = i_slint_compiler::generator::rust::generate(&doc, &loader.compiler_config);
let generated = i_slint_compiler::generator::rust::generate(&doc, &loader.compiler_config)
.map_err(|e| CompileError::CompileError(vec![e.to_string()]))?;
let mut dependencies: Vec<std::path::PathBuf> = Vec::new();

View file

@ -415,7 +415,11 @@ pub fn slint(stream: TokenStream) -> TokenStream {
return diag.report_macro_diagnostic(&tokens);
}
let mut result = generator::rust::generate(&root_component, &loader.compiler_config);
let mut result = generator::rust::generate(&root_component, &loader.compiler_config)
.unwrap_or_else(|e| {
let e_str = e.to_string();
quote!(compile_error!(#e_str))
});
// Make sure to recompile if any of the external files changes
let reload = diag

View file

@ -80,7 +80,7 @@ pub fn generate(
}
#[cfg(feature = "rust")]
OutputFormat::Rust => {
let output = rust::generate(doc, compiler_config);
let output = rust::generate(doc, compiler_config)?;
write!(destination, "{}", output)?;
}
OutputFormat::Interpreter => {
@ -90,7 +90,7 @@ pub fn generate(
)); // Perhaps byte code in the future?
}
OutputFormat::Llr => {
let root = crate::llr::lower_to_item_tree::lower_to_item_tree(doc, compiler_config);
let root = crate::llr::lower_to_item_tree::lower_to_item_tree(doc, compiler_config)?;
let mut output = String::new();
crate::llr::pretty_print::pretty_print(&root, &mut output).unwrap();
write!(destination, "{output}")?;

View file

@ -708,7 +708,7 @@ pub fn generate(
}
}
let llr = llr::lower_to_item_tree::lower_to_item_tree(&doc, compiler_config);
let llr = llr::lower_to_item_tree::lower_to_item_tree(&doc, compiler_config)?;
#[cfg(feature = "bundle-translations")]
if let Some(translations) = &llr.translations {

View file

@ -151,7 +151,10 @@ fn set_primitive_property_value(ty: &Type, value_expression: TokenStream) -> Tok
}
/// Generate the rust code for the given component.
pub fn generate(doc: &Document, compiler_config: &CompilerConfiguration) -> TokenStream {
pub fn generate(
doc: &Document,
compiler_config: &CompilerConfiguration,
) -> std::io::Result<TokenStream> {
let (structs_and_enums_ids, structs_and_enum_def): (Vec<_>, Vec<_>) = doc
.used_types
.borrow()
@ -169,10 +172,10 @@ pub fn generate(doc: &Document, compiler_config: &CompilerConfiguration) -> Toke
})
.unzip();
let llr = crate::llr::lower_to_item_tree::lower_to_item_tree(doc, compiler_config);
let llr = crate::llr::lower_to_item_tree::lower_to_item_tree(doc, compiler_config)?;
if llr.public_components.is_empty() {
return Default::default();
return Ok(Default::default());
}
let sub_compos = llr
@ -215,7 +218,7 @@ pub fn generate(doc: &Document, compiler_config: &CompilerConfiguration) -> Toke
#[cfg(feature = "bundle-translations")]
let translations = llr.translations.as_ref().map(|t| (generate_translations(t, &llr)));
quote! {
Ok(quote! {
#[allow(non_snake_case, non_camel_case_types)]
#[allow(unused_braces, unused_parens)]
#[allow(clippy::all)]
@ -236,7 +239,7 @@ pub fn generate(doc: &Document, compiler_config: &CompilerConfiguration) -> Toke
pub use #generated_mod::{#(#compo_ids,)* #(#structs_and_enums_ids,)* #(#globals_ids,)* #(#named_exports,)*};
#[allow(unused_imports)]
pub use slint::{ComponentHandle as _, Global as _, ModelExt as _};
}
})
}
fn generate_public_component(

View file

@ -17,7 +17,7 @@ use std::rc::Rc;
pub fn lower_to_item_tree(
document: &crate::object_tree::Document,
compiler_config: &CompilerConfiguration,
) -> CompilationUnit {
) -> std::io::Result<CompilationUnit> {
let mut state = LoweringState::default();
#[cfg(feature = "bundle-translations")]
@ -27,7 +27,7 @@ pub fn lower_to_item_tree(
path,
compiler_config.translation_domain.as_deref().unwrap_or(""),
)
.unwrap_or_else(|e| todo!("TODO: handle error loading translations: {e}")),
.map_err(|e| std::io::Error::other(format!("Cannot load bundled translation: {e}")))?,
));
}
@ -79,7 +79,7 @@ pub fn lower_to_item_tree(
translations: state.translation_builder.take().map(|x| x.into_inner().result()),
};
super::optim_passes::run_passes(&root);
root
Ok(root)
}
#[derive(Default)]