From 93f01c6c7b209799b41825acb8b469156f7743f1 Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 5 Jul 2024 08:32:26 +0200 Subject: [PATCH] Minor QoL additions to UUri and usubscription types, add missing usubscription exports (#137) --- src/core/usubscription.rs | 37 ++++++++++++++++++++++++++++++++----- src/uri.rs | 22 ++++++++++++++++++++++ 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/src/core/usubscription.rs b/src/core/usubscription.rs index 84a7fcc..0b2aa23 100644 --- a/src/core/usubscription.rs +++ b/src/core/usubscription.rs @@ -12,11 +12,11 @@ ********************************************************************************/ pub use crate::up_core_api::usubscription::{ - subscription_status::State, EventDeliveryConfig, FetchSubscribersRequest, - FetchSubscribersResponse, FetchSubscriptionsRequest, FetchSubscriptionsResponse, - NotificationsRequest, NotificationsResponse, SubscribeAttributes, SubscriberInfo, Subscription, - SubscriptionRequest, SubscriptionResponse, SubscriptionStatus, UnsubscribeRequest, - UnsubscribeResponse, Update, + fetch_subscriptions_request::Request, subscription_status::State, EventDeliveryConfig, + FetchSubscribersRequest, FetchSubscribersResponse, FetchSubscriptionsRequest, + FetchSubscriptionsResponse, NotificationsRequest, NotificationsResponse, SubscribeAttributes, + SubscriberInfo, Subscription, SubscriptionRequest, SubscriptionResponse, SubscriptionStatus, + UnsubscribeRequest, UnsubscribeResponse, Update, }; use crate::UStatus; @@ -42,6 +42,33 @@ impl SubscriberInfo { } } +impl SubscriptionResponse { + /// Checks if this `SubscriptionResponse` is in a specific state (`usubscription::State``). + /// + /// Returns `true` if SubscriptionReponse contains a valied SusbcriptionStatus, which has a + /// state property that is equal to state passed as argument. + /// + /// # Examples + /// + /// ```rust + /// use up_rust::core::usubscription::{SubscriptionResponse, SubscriptionStatus, State}; + /// + /// let subscription_response = SubscriptionResponse { + /// status: Some(SubscriptionStatus { + /// state: State::SUBSCRIBED.into(), + /// ..Default::default() + /// }).into(), + /// ..Default::default() + /// }; + /// assert!(subscription_response.is_state(State::SUBSCRIBED)); + /// ``` + pub fn is_state(&self, state: State) -> bool { + self.status + .as_ref() + .is_some_and(|ss| ss.state.enum_value().is_ok_and(|s| s.eq(&state))) + } +} + /// `USubscription` is the uP-L3 client interface to the uSubscription service. /// /// A client would use a concrete implementation of `USubscription` typically to subscribe to diff --git a/src/uri.rs b/src/uri.rs index 8f820a3..9b14328 100644 --- a/src/uri.rs +++ b/src/uri.rs @@ -294,6 +294,28 @@ impl UUri { } } + /// Check if an `UUri` is remote, by comparing authority fields. + /// + /// # Returns + /// + /// 'true' if other_uri has a different authority than `Self`, `false` otherwise. + /// + /// # Examples + /// + /// ```rust + /// use std::str::FromStr; + /// use up_rust::UUri; + /// + /// let authority_a = UUri::from_str("up://Authority.A/100A/1/0").unwrap(); + /// let authority_b = UUri::from_str("up://Authority.B/200B/2/20").unwrap(); + /// assert!(authority_a.is_remote_authority(&authority_b)); + /// ```` + pub fn is_remote_authority(&self, other_uri: &UUri) -> bool { + self.authority_name != WILDCARD_AUTHORITY + && other_uri.authority_name != WILDCARD_AUTHORITY + && self.authority_name != other_uri.authority_name + } + /// Serializes this UUri to a URI string. /// /// # Arguments