Merge branch 'improve-errormsg' into 'main'

Improve errormsg

See merge request wotsubo/endcap-sl-software-ri-generator!15
This commit is contained in:
Wataru Otsubo 2025-04-14 07:25:44 +00:00
commit 06a0da0ddb
2 changed files with 39 additions and 23 deletions

View file

@ -12,6 +12,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Packagin with Nix flakes (with nixpkgs buildRustPackage) [!16](https://gitlab.cern.ch/wotsubo/endcap-sl-software-ri-generator/-/merge_requests/16) - Packagin with Nix flakes (with nixpkgs buildRustPackage) [!16](https://gitlab.cern.ch/wotsubo/endcap-sl-software-ri-generator/-/merge_requests/16)
- Now xml metadata can be overwritten with corresponding environmental variables. [!17](https://gitlab.cern.ch/wotsubo/endcap-sl-software-ri-generator/-/merge_requests/17) - Now xml metadata can be overwritten with corresponding environmental variables. [!17](https://gitlab.cern.ch/wotsubo/endcap-sl-software-ri-generator/-/merge_requests/17)
### Changed
- Improve error message from syn in generator. !15
## [0.3.1] - 2025-04-11 ## [0.3.1] - 2025-04-11
### Changed ### Changed

View file

@ -26,8 +26,12 @@ use thiserror::Error;
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum CodeGenError { pub enum CodeGenError {
#[error("tokenization(syn) error")] #[error("tokenization(syn) error: {source}: {code}")]
SynError(#[from] syn::Error), SynError {
#[source]
source: syn::Error,
code: String,
},
#[error("failed to create file (name duplicated): {0}")] #[error("failed to create file (name duplicated): {0}")]
FilePathDuplicatedError(String), FilePathDuplicatedError(String),
#[error("parent is required for {module}")] #[error("parent is required for {module}")]
@ -42,11 +46,17 @@ mod util {
use super::CodeGenError; use super::CodeGenError;
pub(super) fn parse_to_ident(s: &str) -> Result<proc_macro2::Ident, 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> { 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 // 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, GENERATOR_GIT_SHA,
); );
let files = self.generate_register_interface(None, None, HashMap::new())?; let files = self.generate_register_interface(None, None, HashMap::new())?;
Ok(files files
.into_iter() .into_iter()
.map( .map(|(path, tokens)| -> Result<(PathBuf, syn::File), _> {
|(path, tokens)| -> Result<(PathBuf, syn::File), syn::Error> { let tokens = if path
let tokens = if path .file_name()
.file_name() .is_some_and(|file| file == "register_interface.rs")
.is_some_and(|file| file == "register_interface.rs") {
{ quote! {
quote! { #![doc = #build_metadata]
#![doc = #build_metadata]
#tokens #tokens
} }
} else { } else {
tokens tokens
}; };
let file: syn::File = syn::parse2(tokens)?; let file: syn::File =
Ok((path, file)) syn::parse2(tokens.clone()).map_err(|e| CodeGenError::SynError {
}, source: e,
) code: tokens.to_string(),
.process_results(|kv| HashMap::from_iter(kv))?) })?;
Ok((path, file))
})
.process_results(|kv| HashMap::from_iter(kv))
} }
} }