100 lines
2.6 KiB
Rust
100 lines
2.6 KiB
Rust
use peanuts::{
|
|
element::{FromElement, IntoElement},
|
|
Element,
|
|
};
|
|
|
|
use crate::JID;
|
|
|
|
pub const XMLNS: &str = "urn:ietf:params:xml:ns:xmpp-bind";
|
|
|
|
#[derive(Clone)]
|
|
pub struct Bind {
|
|
pub r#type: Option<BindType>,
|
|
}
|
|
|
|
impl FromElement for Bind {
|
|
fn from_element(mut element: peanuts::Element) -> peanuts::element::DeserializeResult<Self> {
|
|
element.check_name("bind");
|
|
element.check_name(XMLNS);
|
|
|
|
let r#type = element.pop_child_opt()?;
|
|
|
|
Ok(Bind { r#type })
|
|
}
|
|
}
|
|
|
|
impl IntoElement for Bind {
|
|
fn builder(&self) -> peanuts::element::ElementBuilder {
|
|
Element::builder("bind", Some(XMLNS)).push_child_opt(self.r#type.clone())
|
|
}
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub enum BindType {
|
|
Resource(ResourceType),
|
|
Jid(FullJidType),
|
|
}
|
|
|
|
impl FromElement for BindType {
|
|
fn from_element(element: peanuts::Element) -> peanuts::element::DeserializeResult<Self> {
|
|
match element.identify() {
|
|
(Some(XMLNS), "resource") => {
|
|
Ok(BindType::Resource(ResourceType::from_element(element)?))
|
|
}
|
|
(Some(XMLNS), "jid") => Ok(BindType::Jid(FullJidType::from_element(element)?)),
|
|
_ => Err(peanuts::DeserializeError::UnexpectedElement(element)),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl IntoElement for BindType {
|
|
fn builder(&self) -> peanuts::element::ElementBuilder {
|
|
match self {
|
|
BindType::Resource(resource_type) => resource_type.builder(),
|
|
BindType::Jid(full_jid_type) => full_jid_type.builder(),
|
|
}
|
|
}
|
|
}
|
|
|
|
// minLength 8 maxLength 3071
|
|
#[derive(Clone)]
|
|
pub struct FullJidType(pub JID);
|
|
|
|
impl FromElement for FullJidType {
|
|
fn from_element(mut element: peanuts::Element) -> peanuts::element::DeserializeResult<Self> {
|
|
element.check_name("jid");
|
|
element.check_namespace(XMLNS);
|
|
|
|
let jid = element.pop_value()?;
|
|
|
|
Ok(FullJidType(jid))
|
|
}
|
|
}
|
|
|
|
impl IntoElement for FullJidType {
|
|
fn builder(&self) -> peanuts::element::ElementBuilder {
|
|
Element::builder("jid", Some(XMLNS)).push_text(self.0.clone())
|
|
}
|
|
}
|
|
|
|
// minLength 1 maxLength 1023
|
|
#[derive(Clone)]
|
|
pub struct ResourceType(pub String);
|
|
|
|
impl FromElement for ResourceType {
|
|
fn from_element(mut element: peanuts::Element) -> peanuts::element::DeserializeResult<Self> {
|
|
element.check_name("resource")?;
|
|
element.check_namespace(XMLNS)?;
|
|
|
|
let resource = element.pop_value()?;
|
|
|
|
Ok(ResourceType(resource))
|
|
}
|
|
}
|
|
|
|
impl IntoElement for ResourceType {
|
|
fn builder(&self) -> peanuts::element::ElementBuilder {
|
|
Element::builder("resource", Some(XMLNS)).push_text(self.0.clone())
|
|
}
|
|
}
|