xso: reject attempts to match the same XML attribute in different fields
This was a bit tricky to build, because it is possible to have an indirection through a `static` there. Thanks to Rust's extensive const-fn capabilities, though, it's in fact possible to cover all cases. We still do two different checks to improve user experience. If we can, from within the proc macro, determine that two fields refer to the same XML attribute (because their namespace/name values use the same Rust tokens), then we reject the fields with a clear error message pointing at both fields. In the other case, when there's e.g. `#[xml(lang)]` and `#[xml(attribute(namespace = rxml::XMLNS_XML, name = "lang"))]`, the macro cannot be sure that XMLNS_XML is in fact the XML namespace. For that case, we generate code which is evaluated at compile time (and has no runtime impact) which panics if the namespace and name of two attribute-matching fields is the same. The error message will be less clear (because it contains extra, unchangeable wording like "evaluation of constant value failed" and "the evaluated program panicked at", which may be a bit confusing) than the message generated by the macros themselves, but it's a price we have to pay unfortunately. Note that this check may seem cosmetic and purely for better user experience, but it is in fact needed to avoid generating not-well-formed and/or not-namespace-well-formed XML: As `AsXml` generates `xso::Item`, where each attribute is emitted separated (and not aggregated in a map structure), a naive (and efficient) implementation of a writer might not double-check that no duplicate attributes are generated.
This commit is contained in:
parent
174eea5e5d
commit
9fdb1564f6
10 changed files with 267 additions and 23 deletions
|
|
@ -96,6 +96,24 @@ impl ParentRef {
|
|||
}
|
||||
}
|
||||
|
||||
/// Wrapper around `Path` with a more human-readable, collapsed version of
|
||||
/// `Path`.
|
||||
pub(crate) struct PrettyPath<'x>(pub &'x Path);
|
||||
|
||||
impl fmt::Display for PrettyPath<'_> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let mut first = self.0.leading_colon.is_none();
|
||||
for segment in self.0.segments.iter() {
|
||||
if !first {
|
||||
f.write_str("::")?;
|
||||
}
|
||||
write!(f, "{}", segment.ident)?;
|
||||
first = false;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Ephemeral struct to create a nice human-readable representation of
|
||||
/// [`syn::Member`].
|
||||
///
|
||||
|
|
|
|||
Loading…
Reference in a new issue