From 95a19b4bb4ec648d58c82a9b17f68ee50409a0cb Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sun, 25 Jun 2017 22:14:21 +0100 Subject: [PATCH] lib: Implement a generate_id! macro. --- src/lib.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 00b2592c..cb8b9d4a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -109,6 +109,25 @@ macro_rules! generate_attribute { ); } +macro_rules! generate_id { + ($elem:ident) => ( + #[derive(Debug, Clone, PartialEq, Eq, Hash)] + pub struct $elem(String); + impl FromStr for $elem { + type Err = Error; + fn from_str(s: &str) -> Result<$elem, Error> { + // TODO: add a way to parse that differently when needed. + Ok($elem(String::from(s))) + } + } + impl IntoAttributeValue for $elem { + fn into_attribute_value(self) -> Option { + Some(self.0) + } + } + ); +} + /// Error type returned by every parser on failure. pub mod error; /// XML namespace definitions used through XMPP.