Skip to content

Commit

Permalink
Adding tracing messages for NoMatch.
Browse files Browse the repository at this point in the history
There is still a case that no message is given; what's more, I found set RUST_LOG to "trace" won't make the message visible.
  • Loading branch information
TheVeryDarkness committed Jun 18, 2023
1 parent c6311c6 commit daaeb38
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions derive/src/from_pest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,19 @@ fn derive_for_struct(
let construct = field::convert(&parse_quote!(#name), fields)?;

let extraneous = crate::trace(
quote! { "when converting {}, found extraneous {:?}", stringify!(#name), inner},
quote! { "When converting {}, found extraneous {:?}", stringify!(#name), inner},
);

let unmatched = crate::trace(quote! {
"Expected rule {:?}, found rule {:?}", expected_rule, found_rule
});

Ok(quote! {
let mut clone = pest.clone();
let pair = clone.next().ok_or(::from_pest::ConversionError::NoMatch)?;
if pair.as_rule() == #rule_enum::#rule_variant {
let found_rule = pair.as_rule();
let expected_rule = #rule_enum::#rule_variant;
if found_rule == expected_rule {
let span = pair.as_span();
let mut inner = pair.into_inner();
let inner = &mut inner;
Expand All @@ -156,6 +162,7 @@ fn derive_for_struct(
*pest = clone;
Ok(this)
} else {
#unmatched
Err(::from_pest::ConversionError::NoMatch)
}
})
Expand All @@ -180,7 +187,7 @@ fn derive_for_enum(
let variant_name = variant.ident;
let construct_variant = field::convert(&parse_quote!(#name::#variant_name), variant.fields)?;
let extraneous = crate::trace(quote! {
"when converting {}, found extraneous {:?}", stringify!(#name), stringify!(#variant_name)
"When converting {}, found extraneous {:?}", stringify!(#name), stringify!(#variant_name)
});

Ok(quote! {
Expand All @@ -199,17 +206,24 @@ fn derive_for_enum(
})
.collect::<Result<_>>()?;

let unmatched = crate::trace(quote! {
"Expected rule {:?}, found rule {:?}", expected_rule, found_rule
});

Ok(quote! {
let mut clone = pest.clone();
let pair = clone.next().ok_or(::from_pest::ConversionError::NoMatch)?;
if pair.as_rule() == #rule_enum::#rule_variant {
let found_rule = pair.as_rule();
let expected_rule = #rule_enum::#rule_variant;
if found_rule == expected_rule {
let this = Err(::from_pest::ConversionError::NoMatch)
#(.or_else(|_: ::from_pest::ConversionError<::from_pest::Void>| {
#convert_variants
}))*?;
*pest = clone;
Ok(this)
} else {
#unmatched
Err(::from_pest::ConversionError::NoMatch)
}
})
Expand Down

0 comments on commit daaeb38

Please sign in to comment.