From 92e69cf59fd99c40d08d939b8eb1697e4189d0a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Sch=C3=A4fer?= Date: Wed, 26 Jun 2024 17:54:14 +0200 Subject: [PATCH] xso-proc: add debug mode for development purposes --- xso-proc/src/lib.rs | 8 ++++++++ xso-proc/src/meta.rs | 11 +++++++++++ xso-proc/src/structs.rs | 8 ++++++++ 3 files changed, 27 insertions(+) diff --git a/xso-proc/src/lib.rs b/xso-proc/src/lib.rs index cdf93623..c5b60597 100644 --- a/xso-proc/src/lib.rs +++ b/xso-proc/src/lib.rs @@ -90,6 +90,10 @@ fn from_xml_impl(input: Item) -> Result { } }); + if def.debug() { + println!("{}", result); + } + Ok(result) } @@ -151,6 +155,10 @@ fn into_xml_impl(input: Item) -> Result { } }); + if def.debug() { + println!("{}", result); + } + Ok(result) } diff --git a/xso-proc/src/meta.rs b/xso-proc/src/meta.rs index 53bd2594..19cee15e 100644 --- a/xso-proc/src/meta.rs +++ b/xso-proc/src/meta.rs @@ -154,6 +154,9 @@ pub(crate) struct XmlCompoundMeta { /// The value assigned to `name` inside `#[xml(..)]`, if any. pub(crate) name: Option, + + /// The debug flag. + pub(crate) debug: Flag, } impl XmlCompoundMeta { @@ -164,6 +167,7 @@ impl XmlCompoundMeta { fn parse_from_attribute(attr: &Attribute) -> Result { let mut namespace = None; let mut name = None; + let mut debug = Flag::Absent; attr.parse_nested_meta(|meta| { if meta.path.is_ident("name") { @@ -178,6 +182,12 @@ impl XmlCompoundMeta { } namespace = Some(meta.value()?.parse()?); Ok(()) + } else if meta.path.is_ident("debug") { + if debug.is_set() { + return Err(Error::new_spanned(meta.path, "duplicate `debug` key")); + } + debug = (&meta.path).into(); + Ok(()) } else { Err(Error::new_spanned(meta.path, "unsupported key")) } @@ -187,6 +197,7 @@ impl XmlCompoundMeta { span: attr.span(), namespace, name, + debug, }) } diff --git a/xso-proc/src/structs.rs b/xso-proc/src/structs.rs index f6012e66..0de95ee7 100644 --- a/xso-proc/src/structs.rs +++ b/xso-proc/src/structs.rs @@ -56,6 +56,9 @@ pub(crate) struct StructDef { /// Name of the iterator type. event_iter_ty_ident: Ident, + + /// Flag whether debug mode is enabled. + debug: bool, } impl StructDef { @@ -76,6 +79,7 @@ impl StructDef { target_ty_ident: ident.clone(), builder_ty_ident: quote::format_ident!("{}FromXmlBuilder", ident), event_iter_ty_ident: quote::format_ident!("{}IntoXmlIterator", ident), + debug: meta.debug.is_set(), }) } @@ -172,4 +176,8 @@ impl StructDef { event_iter_ty_ident: event_iter_ty_ident.clone(), }) } + + pub(crate) fn debug(&self) -> bool { + self.debug + } }