From cd7bb95c0a31d187bfe25bad15043f0b33b111cf Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Wed, 2 Aug 2023 00:56:38 +0100 Subject: implement resource binding --- src/stanza/mod.rs | 46 ++++++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 16 deletions(-) (limited to 'src/stanza/mod.rs') diff --git a/src/stanza/mod.rs b/src/stanza/mod.rs index c29b1a2..ad9e228 100644 --- a/src/stanza/mod.rs +++ b/src/stanza/mod.rs @@ -1,5 +1,7 @@ // use quick_xml::events::BytesDecl; +pub mod bind; +pub mod iq; pub mod sasl; pub mod stream; @@ -128,28 +130,40 @@ impl<'e> Element<'e> { e => Err(ElementError::NotAStart(e.into_owned()).into()), } } -} -/// if there is only one child in the vec of children, will return that element -pub fn child<'p, 'e>(element: &'p Element<'e>) -> Result<&'p Element<'e>, ElementError<'static>> { - if let Some(children) = &element.children { - if children.len() == 1 { - return Ok(&children[0]); - } else { - return Err(ElementError::MultipleChildren); + /// if there is only one child in the vec of children, will return that element + pub fn child<'p>(&'p self) -> Result<&'p Element<'e>, ElementError<'static>> { + if let Some(children) = &self.children { + if children.len() == 1 { + return Ok(&children[0]); + } else { + return Err(ElementError::MultipleChildren); + } + } + Err(ElementError::NoChildren) + } + + /// returns reference to children + pub fn children<'p>(&'p self) -> Result<&'p Vec>, ElementError<'e>> { + if let Some(children) = &self.children { + return Ok(children); } + Err(ElementError::NoChildren) } - Err(ElementError::NoChildren) } -/// returns reference to children -pub fn children<'p, 'e>( - element: &'p Element<'e>, -) -> Result<&'p Vec>, ElementError<'e>> { - if let Some(children) = &element.children { - return Ok(children); +pub trait IntoElement<'e> { + fn event(&self) -> Event<'e>; + fn children(&self) -> Option>>; +} + +impl<'e, T: IntoElement<'e>> From for Element<'e> { + fn from(value: T) -> Self { + Element { + event: value.event(), + children: value.children(), + } } - Err(ElementError::NoChildren) } #[derive(Debug)] -- cgit