From f9539f92f08e6f91aea793a44c13a4c828399ce4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Thu, 27 Jul 2023 23:54:20 +0200 Subject: [PATCH] element: Add conversions from/to ScanElement MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- src/element.rs | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/src/element.rs b/src/element.rs index 0f63597..661a7e7 100644 --- a/src/element.rs +++ b/src/element.rs @@ -38,6 +38,7 @@ use std::fmt::Debug; use std::marker::PhantomData; use std::ops::Deref; +use std::str::FromStr; use crate::parsers::parse_variable; use crate::types::{Client, Context, Entity, VariableAttr}; @@ -206,6 +207,75 @@ impl Deref for ScanElement { } } +impl From for String { + fn from(scan: ScanElement) -> String { + String::from(&scan.elem) + } +} + +impl From<&ScanElement> for String { + fn from(scan: &ScanElement) -> String { + String::from(&scan.elem) + } +} + +impl From for ScanElement { + fn from(elem: Element) -> ScanElement { + ScanElement::new(elem) + } +} + +impl From for ScanElement { + fn from(elem: Iq) -> ScanElement { + ScanElement::new(elem.into()) + } +} + +impl TryFrom for Iq { + type Error = XMPPParserError; + + fn try_from(scan: ScanElement) -> Result { + scan.elem.try_into() + } +} + +impl From for ScanElement { + fn from(elem: Message) -> ScanElement { + ScanElement::new(elem.into()) + } +} + +impl TryFrom for Message { + type Error = XMPPParserError; + + fn try_from(scan: ScanElement) -> Result { + scan.elem.try_into() + } +} + +impl From for ScanElement { + fn from(elem: Presence) -> ScanElement { + ScanElement::new(elem.into()) + } +} + +impl TryFrom for Presence { + type Error = XMPPParserError; + + fn try_from(scan: ScanElement) -> Result { + scan.elem.try_into() + } +} + +impl FromStr for ScanElement { + type Err = MinidomError; + + fn from_str(s: &str) -> Result { + let elem = s.parse::()?; + Ok(ScanElement::new(elem)) + } +} + fn apply_context(mut elem: Element, context: &Context) -> Element { // Parse variables in attributes. for (_attr, val) in elem.attrs_mut() {