Skip to content

Commit

Permalink
Minor QoL additions to UUri and usubscription types, add missing usub…
Browse files Browse the repository at this point in the history
…scription exports (eclipse-uprotocol#137)
  • Loading branch information
AnotherDaniel authored Jul 5, 2024
1 parent a0cdb95 commit 93f01c6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
37 changes: 32 additions & 5 deletions src/core/usubscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
22 changes: 22 additions & 0 deletions src/uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 93f01c6

Please sign in to comment.