Do some manual formatting in macros.

This commit is contained in:
Emmanuel Gil Peyrot 2018-12-29 18:29:11 +01:00
parent f6593aa74b
commit 96c8b05677
4 changed files with 47 additions and 44 deletions

View file

@ -29,7 +29,7 @@ macro_rules! get_attr {
"Required attribute '", "Required attribute '",
$attr, $attr,
"' missing." "' missing."
))) )));
} }
} }
}; };
@ -151,8 +151,8 @@ macro_rules! generate_element_enum {
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum $elem { pub enum $elem {
$( $(
$(#[$enum_meta])* $(#[$enum_meta])*
$enum $enum
),+ ),+
} }
impl ::try_from::TryFrom<::minidom::Element> for $elem { impl ::try_from::TryFrom<::minidom::Element> for $elem {
@ -169,10 +169,13 @@ macro_rules! generate_element_enum {
} }
impl From<$elem> for ::minidom::Element { impl From<$elem> for ::minidom::Element {
fn from(elem: $elem) -> ::minidom::Element { fn from(elem: $elem) -> ::minidom::Element {
::minidom::Element::builder(match elem { ::minidom::Element::builder(
$($elem::$enum => $enum_name,)+ match elem {
}).ns(crate::ns::$ns) $($elem::$enum => $enum_name,)+
.build() }
)
.ns(crate::ns::$ns)
.build()
} }
} }
); );
@ -187,8 +190,8 @@ macro_rules! generate_attribute_enum {
#[derive(Debug, Clone, PartialEq)] #[derive(Debug, Clone, PartialEq)]
pub enum $elem { pub enum $elem {
$( $(
$(#[$enum_meta])* $(#[$enum_meta])*
$enum $enum
),+ ),+
} }
impl ::try_from::TryFrom<::minidom::Element> for $elem { impl ::try_from::TryFrom<::minidom::Element> for $elem {
@ -206,11 +209,11 @@ macro_rules! generate_attribute_enum {
impl From<$elem> for ::minidom::Element { impl From<$elem> for ::minidom::Element {
fn from(elem: $elem) -> ::minidom::Element { fn from(elem: $elem) -> ::minidom::Element {
::minidom::Element::builder($name) ::minidom::Element::builder($name)
.ns(crate::ns::$ns) .ns(crate::ns::$ns)
.attr($attr, match elem { .attr($attr, match elem {
$($elem::$enum => $enum_name,)+ $($elem::$enum => $enum_name,)+
}) })
.build() .build()
} }
} }
); );
@ -271,9 +274,9 @@ macro_rules! check_no_unknown_attributes {
($elem:ident, $name:tt, [$($attr:tt),*]) => ( ($elem:ident, $name:tt, [$($attr:tt),*]) => (
for (_attr, _) in $elem.attrs() { for (_attr, _) in $elem.attrs() {
$( $(
if _attr == $attr { if _attr == $attr {
continue; continue;
} }
)* )*
return Err(crate::error::Error::ParseError(concat!("Unknown attribute in ", $name, " element."))); return Err(crate::error::Error::ParseError(concat!("Unknown attribute in ", $name, " element.")));
} }
@ -300,8 +303,8 @@ macro_rules! generate_empty_element {
impl From<$elem> for ::minidom::Element { impl From<$elem> for ::minidom::Element {
fn from(_: $elem) -> ::minidom::Element { fn from(_: $elem) -> ::minidom::Element {
::minidom::Element::builder($name) ::minidom::Element::builder($name)
.ns(crate::ns::$ns) .ns(crate::ns::$ns)
.build() .build()
} }
} }
); );
@ -352,9 +355,9 @@ macro_rules! generate_elem_id {
impl From<$elem> for ::minidom::Element { impl From<$elem> for ::minidom::Element {
fn from(elem: $elem) -> ::minidom::Element { fn from(elem: $elem) -> ::minidom::Element {
::minidom::Element::builder($name) ::minidom::Element::builder($name)
.ns(crate::ns::$ns) .ns(crate::ns::$ns)
.append(elem.0) .append(elem.0)
.build() .build()
} }
} }
); );
@ -488,16 +491,16 @@ macro_rules! generate_element {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct $elem { pub struct $elem {
$( $(
$(#[$attr_meta])* $(#[$attr_meta])*
pub $attr: $attr_type, pub $attr: $attr_type,
)* )*
$( $(
$(#[$child_meta])* $(#[$child_meta])*
pub $child_ident: start_decl!($coucou, $child_type), pub $child_ident: start_decl!($coucou, $child_type),
)* )*
$( $(
$(#[$text_meta])* $(#[$text_meta])*
pub $text_ident: $text_type, pub $text_ident: $text_type,
)* )*
} }
@ -508,7 +511,7 @@ macro_rules! generate_element {
check_self!(elem, $name, $ns); check_self!(elem, $name, $ns);
check_no_unknown_attributes!(elem, $name, [$($attr_name),*]); check_no_unknown_attributes!(elem, $name, [$($attr_name),*]);
$( $(
start_parse_elem!($child_ident: $coucou); start_parse_elem!($child_ident: $coucou);
)* )*
for _child in elem.children() { for _child in elem.children() {
$( $(
@ -521,13 +524,13 @@ macro_rules! generate_element {
} }
Ok($elem { Ok($elem {
$( $(
$attr: get_attr!(elem, $attr_name, $attr_action), $attr: get_attr!(elem, $attr_name, $attr_action),
)* )*
$( $(
$child_ident: finish_parse_elem!($child_ident: $coucou = $child_name, $name), $child_ident: finish_parse_elem!($child_ident: $coucou = $child_name, $name),
)* )*
$( $(
$text_ident: $codec::decode(&elem.text())?, $text_ident: $codec::decode(&elem.text())?,
)* )*
}) })
} }
@ -536,17 +539,17 @@ macro_rules! generate_element {
impl From<$elem> for ::minidom::Element { impl From<$elem> for ::minidom::Element {
fn from(elem: $elem) -> ::minidom::Element { fn from(elem: $elem) -> ::minidom::Element {
::minidom::Element::builder($name) ::minidom::Element::builder($name)
.ns(crate::ns::$ns) .ns(crate::ns::$ns)
$( $(
.attr($attr_name, elem.$attr) .attr($attr_name, elem.$attr)
)* )*
$( $(
.append(generate_serialiser!(elem, $child_ident, $coucou, $child_constructor, ($child_name, $child_ns))) .append(generate_serialiser!(elem, $child_ident, $coucou, $child_constructor, ($child_name, $child_ns)))
)* )*
$( $(
.append($codec::encode(&elem.$text_ident)) .append($codec::encode(&elem.$text_ident))
)* )*
.build() .build()
} }
} }
); );

View file

@ -102,7 +102,7 @@ impl TryFrom<Element> for Actor {
(Some(_), Some(_)) | (None, None) => { (Some(_), Some(_)) | (None, None) => {
return Err(Error::ParseError( return Err(Error::ParseError(
"Either 'jid' or 'nick' attribute is required.", "Either 'jid' or 'nick' attribute is required.",
)) ));
} }
(Some(jid), _) => Ok(Actor::Jid(jid)), (Some(jid), _) => Ok(Actor::Jid(jid)),
(_, Some(nick)) => Ok(Actor::Nick(nick)), (_, Some(nick)) => Ok(Actor::Nick(nick)),

View file

@ -136,7 +136,7 @@ impl FromStr for Type {
_ => { _ => {
return Err(Error::ParseError( return Err(Error::ParseError(
"Invalid 'type' attribute on presence element.", "Invalid 'type' attribute on presence element.",
)) ));
} }
}) })
} }

View file

@ -138,7 +138,7 @@ fn parse_items(elem: Element, node: NodeName) -> Result<PubSubEvent, Error> {
Some(true) => { Some(true) => {
return Err(Error::ParseError( return Err(Error::ParseError(
"Mix of item and retract in items element.", "Mix of item and retract in items element.",
)) ));
} }
} }
items.push(Item::try_from(child.clone())?); items.push(Item::try_from(child.clone())?);
@ -149,7 +149,7 @@ fn parse_items(elem: Element, node: NodeName) -> Result<PubSubEvent, Error> {
Some(false) => { Some(false) => {
return Err(Error::ParseError( return Err(Error::ParseError(
"Mix of item and retract in items element.", "Mix of item and retract in items element.",
)) ));
} }
} }
check_no_children!(child, "retract"); check_no_children!(child, "retract");