From d5f88d26369ea633286f228c922d7f219c9ffedd Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Wed, 16 May 2018 14:49:00 +0200 Subject: [PATCH] iq: Add helper constructors. --- src/iq.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/iq.rs b/src/iq.rs index 73395c6f..e695dc3e 100644 --- a/src/iq.rs +++ b/src/iq.rs @@ -55,6 +55,59 @@ pub struct Iq { pub payload: IqType, } +impl Iq { + pub fn from_get(payload: impl IqGetPayload) -> Iq { + Iq { + from: None, + to: None, + id: None, + payload: IqType::Get(payload.into()), + } + } + + pub fn from_set(payload: impl IqSetPayload) -> Iq { + Iq { + from: None, + to: None, + id: None, + payload: IqType::Set(payload.into()), + } + } + + pub fn from_result(payload: Option) -> Iq { + Iq { + from: None, + to: None, + id: None, + payload: IqType::Result(payload.map(|payload| payload.into())), + } + } + + pub fn from_error(payload: StanzaError) -> Iq { + Iq { + from: None, + to: None, + id: None, + payload: IqType::Error(payload), + } + } + + pub fn with_to(mut self, to: Jid) -> Iq { + self.to = Some(to); + self + } + + pub fn with_from(mut self, from: Jid) -> Iq { + self.from = Some(from); + self + } + + pub fn with_id(mut self, id: String) -> Iq { + self.id = Some(id); + self + } +} + impl TryFrom for Iq { type Err = Error;