Skip to content

Commit

Permalink
fix: fix clippy warnings and failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
parfeon committed Aug 27, 2023
1 parent 351845c commit ee7958a
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 59 deletions.
4 changes: 2 additions & 2 deletions src/core/utils/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub fn join_url_encoded(strings: &[&str], sep: &str) -> Option<String> {
/// URL-encode channels list.
///
/// Channels list used as part of URL path and therefore required.
#[cfg(all(any(feature = "subscribe", feature = "presence"), feature = "std"))]
#[cfg(any(feature = "subscribe", feature = "presence"))]
pub(crate) fn url_encoded_channels(channels: &[String]) -> String {
join_url_encoded(
channels
Expand All @@ -88,7 +88,7 @@ pub(crate) fn url_encoded_channels(channels: &[String]) -> String {
}

/// URL-encode channel groups list.
#[cfg(all(any(feature = "subscribe", feature = "presence"), feature = "std"))]
#[cfg(any(feature = "subscribe", feature = "presence"))]
pub(crate) fn url_encoded_channel_groups(channel_groups: &[String]) -> Option<String> {
join_url_encoded(
channel_groups
Expand Down
14 changes: 12 additions & 2 deletions src/core/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
#[cfg(any(feature = "publish", feature = "access", feature = "subscribe"))]
#[cfg(any(
feature = "publish",
feature = "access",
feature = "subscribe",
feature = "presence"
))]
pub mod encoding;
#[cfg(any(feature = "publish", feature = "access", feature = "subscribe"))]
#[cfg(any(
feature = "publish",
feature = "access",
feature = "subscribe",
feature = "presence"
))]
pub mod headers;

pub mod metadata;
7 changes: 4 additions & 3 deletions src/dx/presence/builders/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
//! [`PubNub`]: https://www.pubnub.com
#[doc(inline)]
pub(crate) use heartbeat::{HeartbeatRequest, HeartbeatRequestBuilder};
pub(crate) use heartbeat::HeartbeatRequestBuilder;
pub(crate) mod heartbeat;

#[doc(inline)]
pub use set_state::{SetStateRequest, SetStateRequestBuilder};
pub mod set_state;

#[doc(inline)]
pub(crate) use leave::{LeaveRequest, LeaveRequestBuilder};
pub(crate) use leave::LeaveRequestBuilder;
pub(crate) mod leave;

use crate::{dx::pubnub_client::PubNubClientInstance, lib::alloc::string::String};
Expand All @@ -28,7 +28,8 @@ pub(in crate::dx::presence::builders) fn validate_configuration<T, D>(
) -> Result<(), String> {
let client = client
.as_ref()
.expect("PubNub client instance not set.".into());
.unwrap_or_else(|| panic!("PubNub client instance not set."));

if client.config.subscribe_key.is_empty() {
return Err("Incomplete PubNub client configuration: 'subscribe_key' is empty.".into());
}
Expand Down
18 changes: 3 additions & 15 deletions src/dx/presence/event_engine/effects/heartbeat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ pub(super) async fn execute(
effect_id,
})
.map_ok_or_else(
|error| {
log::error!("Heartbeat error: {error:?}");
vec![PresenceEvent::HeartbeatFailure { reason: error }]
},
|error| vec![PresenceEvent::HeartbeatFailure { reason: error }],
|_| vec![PresenceEvent::HeartbeatSuccess],
)
.await
Expand All @@ -75,13 +72,7 @@ mod it_should {
assert_eq!(parameters.channel_groups, &Some(vec!["cg1".to_string()]));
assert_eq!(parameters.channels, &Some(vec!["ch1".to_string()]));
assert_eq!(parameters.attempt, 0);
assert_eq!(
parameters.reason.unwrap(),
PubNubError::Transport {
details: "test".into(),
response: None
}
);
assert_eq!(parameters.reason, None);
assert_eq!(parameters.effect_id, "id");

async move { Ok(HeartbeatResult) }.boxed()
Expand All @@ -93,10 +84,7 @@ mod it_should {
&Some(vec!["cg1".to_string()]),
),
0,
Some(PubNubError::Transport {
details: "test".into(),
response: None,
}),
None,
"id",
&Some(RequestRetryPolicy::None),
&mocked_heartbeat_function,
Expand Down
16 changes: 3 additions & 13 deletions src/dx/presence/event_engine/effects/leave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,7 @@ mod it_should {
assert_eq!(parameters.channel_groups, &Some(vec!["cg2".to_string()]));
assert_eq!(parameters.channels, &Some(vec!["ch2".to_string()]));
assert_eq!(parameters.attempt, 0);
assert_eq!(
parameters.reason.unwrap(),
PubNubError::Transport {
details: "test".into(),
response: None
}
);
assert_eq!(parameters.reason, None);
assert_eq!(parameters.effect_id, "id");

async move { Ok(LeaveResult) }.boxed()
Expand All @@ -82,7 +76,7 @@ mod it_should {
}

#[tokio::test]
async fn return_heartbeat_failed_event_on_error() {
async fn return_leave_failed_event_on_error() {
let mocked_leave_function: Arc<LeaveEffectExecutor> = Arc::new(move |_| {
async move {
Err(PubNubError::Transport {
Expand All @@ -106,10 +100,6 @@ mod it_should {
)
.await;

assert!(!result.is_empty());
assert!(matches!(
result.first().unwrap(),
PresenceEvent::HeartbeatFailure { .. }
));
assert!(result.is_empty());
}
}
1 change: 1 addition & 0 deletions src/dx/presence/event_engine/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use crate::core::{event_engine::Event, PubNubError};

#[derive(Debug)]
pub(crate) enum PresenceEvent {
/// Announce join to channels and groups.
///
Expand Down
32 changes: 17 additions & 15 deletions src/dx/presence/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub mod result;

#[cfg(feature = "std")]
#[doc(inline)]
pub(crate) use presence_manager::{PresenceManager, PresenceManagerRef};
pub(crate) use presence_manager::PresenceManager;
#[cfg(feature = "std")]
pub(crate) mod presence_manager;
#[cfg(feature = "std")]
Expand Down Expand Up @@ -181,33 +181,35 @@ where
#[allow(dead_code)]
pub(crate) fn announce_join(
&self,
_channels: Option<Vec<String>>,
_channel_groups: Option<Vec<String>>,
channels: Option<Vec<String>>,
channel_groups: Option<Vec<String>>,
) {
self.configure_presence();

// if let Some(presence) = self.presence.clone().write().as_mut() {
// params
// .state
// .map(|state| presence.state = Some(Arc::new(state)));
// }
{
let slot = self.presence.read();
if let Some(presence) = slot.as_ref() {
presence.announce_join(channels, channel_groups);
}
};
}

/// Announce `leave` for `user_id` on provided channels and groups.
#[cfg(feature = "std")]
#[allow(dead_code)]
pub(crate) fn announce_left(
&self,
_channels: Option<Vec<String>>,
_channel_groups: Option<Vec<String>>,
channels: Option<Vec<String>>,
channel_groups: Option<Vec<String>>,
) {
self.configure_presence();

// if let Some(presence) = self.presence.clone().write().as_mut() {
// params
// .state
// .map(|state| presence.state = Some(Arc::new(state)));
// }
{
let slot = self.presence.read();
if let Some(presence) = slot.as_ref() {
presence.announce_left(channels, channel_groups);
}
};
}

/// Complete presence configuration.
Expand Down
30 changes: 29 additions & 1 deletion src/dx/presence/presence_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! presence / heartbeat module components.
use crate::{
dx::presence::event_engine::PresenceEventEngine,
dx::presence::event_engine::{PresenceEvent, PresenceEventEngine},
lib::{
alloc::sync::Arc,
core::{
Expand Down Expand Up @@ -86,6 +86,34 @@ pub(crate) struct PresenceManagerRef {
pub state: Option<Vec<u8>>,
}

impl PresenceManagerRef {
/// Announce `join` for `user_id` on provided channels and groups.
#[allow(dead_code)]
pub(crate) fn announce_join(
&self,
channels: Option<Vec<String>>,
channel_groups: Option<Vec<String>>,
) {
self.event_engine.process(&PresenceEvent::Joined {
channels,
channel_groups,
})
}

/// Announce `leave` for `user_id` on provided channels and groups.
#[allow(dead_code)]
pub(crate) fn announce_left(
&self,
channels: Option<Vec<String>>,
channel_groups: Option<Vec<String>>,
) {
self.event_engine.process(&PresenceEvent::Left {
channels,
channel_groups,
})
}
}

impl Debug for PresenceManagerRef {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(
Expand Down
7 changes: 3 additions & 4 deletions src/dx/publish/builders.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
//! Publish builders module.
//! # Publish builders module.
//!
//! This module contains all builders for the publish operation.
use derive_builder::Builder;

use crate::{
core::Serialize,
dx::pubnub_client::PubNubClientInstance,
lib::{alloc::string::String, collections::HashMap},
};

use crate::core::Transport;
use derive_builder::Builder;

/// The [`PublishMessageBuilder`] is used to publish a message to a channel.
///
/// This struct is used by the [`publish_message`] method of the
Expand Down
2 changes: 1 addition & 1 deletion src/dx/subscribe/builders/subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl<T, D> SubscribeRequest<T, D> {

// Serialize list of channel groups and add into query parameters list.
url_encoded_channel_groups(&self.channel_groups)
.and_then(|groups| query.insert("channel-group".into(), groups.into()));
.and_then(|groups| query.insert("channel-group".into(), groups));

self.filter_expression
.as_ref()
Expand Down
2 changes: 1 addition & 1 deletion src/dx/subscribe/builders/subscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ impl SubscriptionBuilder {
let input = self
.input
.as_ref()
.expect("Subscription input should be set by default".into());
.unwrap_or_else(|| panic!("Subscription input should be set by default"));

if input.is_empty {
return Err("Either channels or channel groups should be provided".into());
Expand Down
6 changes: 4 additions & 2 deletions src/dx/subscribe/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use futures::{
future::{ready, BoxFuture},
FutureExt,
};
#[cfg(feature = "std")]
use spin::RwLock;

use crate::dx::{pubnub_client::PubNubClientInstance, subscribe::raw::RawSubscriptionBuilder};
Expand Down Expand Up @@ -43,10 +44,11 @@ pub mod result;
pub(crate) use subscription_manager::SubscriptionManager;
#[cfg(feature = "std")]
pub(crate) mod subscription_manager;
use crate::subscribe::event_engine::SubscribeEventEngine;
#[cfg(feature = "std")]
#[doc(inline)]
use event_engine::{types::SubscriptionParams, SubscribeEffectHandler, SubscribeState};
use event_engine::{
types::SubscriptionParams, SubscribeEffectHandler, SubscribeEventEngine, SubscribeState,
};

#[cfg(feature = "std")]
pub(crate) mod event_engine;
Expand Down

0 comments on commit ee7958a

Please sign in to comment.