mirror of
https://gitlab.cern.ch/wotsubo/endcap-sl-software-ri-generator.git
synced 2025-04-20 19:56:03 +09:00
update(generator): improve error message from syn error
This commit is contained in:
parent
1bb239812e
commit
8a774f1d56
1 changed files with 35 additions and 23 deletions
|
@ -26,8 +26,12 @@ use thiserror::Error;
|
|||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum CodeGenError {
|
||||
#[error("tokenization(syn) error")]
|
||||
SynError(#[from] syn::Error),
|
||||
#[error("tokenization(syn) error: {source}: {code}")]
|
||||
SynError {
|
||||
#[source]
|
||||
source: syn::Error,
|
||||
code: String,
|
||||
},
|
||||
#[error("failed to create file (name duplicated): {0}")]
|
||||
FilePathDuplicatedError(String),
|
||||
#[error("parent is required for {module}")]
|
||||
|
@ -42,11 +46,17 @@ mod util {
|
|||
use super::CodeGenError;
|
||||
|
||||
pub(super) fn parse_to_ident(s: &str) -> Result<proc_macro2::Ident, CodeGenError> {
|
||||
Ok(syn::parse_str(s)?)
|
||||
syn::parse_str(s).map_err(|e| CodeGenError::SynError {
|
||||
source: e,
|
||||
code: s.to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn parse_to_literal(s: &str) -> Result<proc_macro2::Literal, CodeGenError> {
|
||||
Ok(syn::parse_str(s)?)
|
||||
syn::parse_str(s).map_err(|e| CodeGenError::SynError {
|
||||
source: e,
|
||||
code: s.to_string(),
|
||||
})
|
||||
}
|
||||
|
||||
// currently only U32 is used, so `dead_code` for Debug, PartialEq
|
||||
|
@ -161,27 +171,29 @@ This code is auto generated using endcap_sl_software_ri_generator.
|
|||
GENERATOR_GIT_SHA,
|
||||
);
|
||||
let files = self.generate_register_interface(None, None, HashMap::new())?;
|
||||
Ok(files
|
||||
files
|
||||
.into_iter()
|
||||
.map(
|
||||
|(path, tokens)| -> Result<(PathBuf, syn::File), syn::Error> {
|
||||
let tokens = if path
|
||||
.file_name()
|
||||
.is_some_and(|file| file == "register_interface.rs")
|
||||
{
|
||||
quote! {
|
||||
#![doc = #build_metadata]
|
||||
.map(|(path, tokens)| -> Result<(PathBuf, syn::File), _> {
|
||||
let tokens = if path
|
||||
.file_name()
|
||||
.is_some_and(|file| file == "register_interface.rs")
|
||||
{
|
||||
quote! {
|
||||
#![doc = #build_metadata]
|
||||
|
||||
#tokens
|
||||
}
|
||||
} else {
|
||||
tokens
|
||||
};
|
||||
let file: syn::File = syn::parse2(tokens)?;
|
||||
Ok((path, file))
|
||||
},
|
||||
)
|
||||
.process_results(|kv| HashMap::from_iter(kv))?)
|
||||
#tokens
|
||||
}
|
||||
} else {
|
||||
tokens
|
||||
};
|
||||
let file: syn::File =
|
||||
syn::parse2(tokens.clone()).map_err(|e| CodeGenError::SynError {
|
||||
source: e,
|
||||
code: tokens.to_string(),
|
||||
})?;
|
||||
Ok((path, file))
|
||||
})
|
||||
.process_results(|kv| HashMap::from_iter(kv))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue