1
0
mirror of https://gitlab.com/xmpp-rs/xmpp-rs.git synced 2024-06-18 05:35:57 +02:00

parsers-macros: improve error message syntax slightly

This should make it clearer what the opaque words inside the message
refer to.
This commit is contained in:
Jonas Schäfer 2024-03-26 16:22:35 +01:00
parent 4d652df498
commit df4691cff1

View File

@ -3,7 +3,6 @@ use std::fmt;
use proc_macro2::Span;
use quote::ToTokens;
use syn::{spanned::Spanned, Member, Path};
/// Reference to a compound's parent
@ -75,9 +74,19 @@ impl ParentRef {
impl fmt::Display for ParentRef {
fn fmt<'f>(&self, f: &'f mut fmt::Formatter) -> fmt::Result {
match self {
Self::Named(name) => write!(f, "{} element", name.to_token_stream()),
Self::Named(name) => {
let mut first = true;
for segment in name.segments.iter() {
if !first {
write!(f, "::")?;
}
first = false;
write!(f, "{}", segment.ident)?;
}
write!(f, " element")
}
Self::Unnamed { parent, field } => {
write!(f, "extraction for child {} in {}", FieldName(field), parent)
write!(f, "extraction for {} in {}", FieldName(field), parent)
}
}
}
@ -94,8 +103,8 @@ struct FieldName<'x>(&'x Member);
impl<'x> fmt::Display for FieldName<'x> {
fn fmt<'f>(&self, f: &'f mut fmt::Formatter) -> fmt::Result {
match self.0 {
Member::Named(v) => v.fmt(f),
Member::Unnamed(v) => write!(f, "<unnamed field {}>", v.index),
Member::Named(v) => write!(f, "field '{}'", v),
Member::Unnamed(v) => write!(f, "unnamed field {}", v.index),
}
}
}
@ -114,7 +123,7 @@ pub(super) fn on_missing_child(parent_name: &ParentRef, field: &Member) -> Strin
/// `field` should be the field to which the child belongs.
pub(super) fn on_duplicate_child(parent_name: &ParentRef, field: &Member) -> String {
format!(
"{} must not have more than one {} child.",
"{} must not have more than one child in {}.",
parent_name,
FieldName(&field)
)