From 512c1d1aaef3857aad7642050a66a96e3e482680 Mon Sep 17 00:00:00 2001 From: xmppftw Date: Sat, 3 Jun 2023 10:55:34 +0200 Subject: [PATCH] Presence now has constructors for each type variant, and a with_payload builder method (closes #79) --- parsers/ChangeLog | 2 ++ parsers/src/presence.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/parsers/ChangeLog b/parsers/ChangeLog index 8569d9f7..c0e88c51 100644 --- a/parsers/ChangeLog +++ b/parsers/ChangeLog @@ -5,6 +5,8 @@ xxx * Improvements: - muc::user::Item: Added with_ helpers - Correct cargo doc warnings + - Presence now has constructors for each variant so you don't have to import presence::Type, where Presence::available represents type None (#79) + - Presence::with_payload builds a payload into the presence (#79) * Breaking changes: - Removed the 'serde' feature. Add it directly by using 'jid'. `jid = { version = "*", features = ["serde"] }`. diff --git a/parsers/src/presence.rs b/parsers/src/presence.rs index fcd81b96..7ceb20cf 100644 --- a/parsers/src/presence.rs +++ b/parsers/src/presence.rs @@ -191,6 +191,41 @@ impl Presence { } } + /// Create a presence without a type, which means available + pub fn available() -> Presence { + Self::new(Type::None) + } + + /// Builds a presence of type Error + pub fn error() -> Presence { + Self::new(Type::Error) + } + + /// Builds a presence of type Probe + pub fn probe() -> Presence { + Self::new(Type::Probe) + } + + /// Builds a presence of type Subscribe + pub fn subscribe() -> Presence { + Self::new(Type::Subscribe) + } + + /// Builds a presence of type Subscribed + pub fn subscribed() -> Presence { + Self::new(Type::Subscribed) + } + + /// Builds a presence of type Unavailable + pub fn unavailable() -> Presence { + Self::new(Type::Unavailable) + } + + /// Builds a presence of type Unsubscribe + pub fn unsubscribe() -> Presence { + Self::new(Type::Unsubscribe) + } + /// Set the emitter of this presence, this should only be useful for /// servers and components, as clients can only send presences from their /// own resource (which is implicit). @@ -224,6 +259,12 @@ impl Presence { self } + /// Set a payload inside this presence. + pub fn with_payload(mut self, payload: P) -> Presence { + self.payloads.push(payload.into()); + self + } + /// Set the payloads of this presence. pub fn with_payloads(mut self, payloads: Vec) -> Presence { self.payloads = payloads;