Skip to content

Commit

Permalink
Formatting try serenity-rs#2
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCataliasTNT2k committed Apr 26, 2024
1 parent 7cdec9f commit 643c76a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 63 deletions.
32 changes: 16 additions & 16 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use std::sync::OnceLock;
use futures::channel::mpsc::UnboundedReceiver as Receiver;
use futures::future::BoxFuture;
use futures::StreamExt as _;
use tracing::{debug};
use tracing::debug;

pub use self::context::Context;
pub use self::error::Error as ClientError;
Expand Down Expand Up @@ -163,8 +163,8 @@ impl ClientBuilder {
/// [`Framework`] on [`Arc<YourFrameworkType>`] instead of `YourFrameworkType`.
#[cfg(feature = "framework")]
pub fn framework<F>(mut self, framework: F) -> Self
where
F: Framework + 'static,
where
F: Framework + 'static,
{
self.framework = Some(Box::new(framework));

Expand All @@ -182,8 +182,8 @@ impl ClientBuilder {
/// gateway and then consider - based on its settings - whether to dispatch a command.
#[cfg(feature = "voice")]
pub fn voice_manager<V>(mut self, voice_manager: impl Into<Arc<V>>) -> Self
where
V: VoiceGatewayManager + 'static,
where
V: VoiceGatewayManager + 'static,
{
self.voice_manager = Some(voice_manager.into());
self
Expand Down Expand Up @@ -230,8 +230,8 @@ impl ClientBuilder {

/// Adds an event handler with multiple methods for each possible event.
pub fn event_handler<H>(mut self, event_handler: impl Into<Arc<H>>) -> Self
where
H: EventHandler + 'static,
where
H: EventHandler + 'static,
{
self.event_handler = Some(event_handler.into());
self
Expand All @@ -246,8 +246,8 @@ impl ClientBuilder {
/// Adds an event handler with a single method where all received gateway events will be
/// dispatched.
pub fn raw_event_handler<H>(mut self, raw_event_handler: impl Into<Arc<H>>) -> Self
where
H: RawEventHandler + 'static,
where
H: RawEventHandler + 'static,
{
self.raw_event_handler = Some(raw_event_handler.into());
self
Expand Down Expand Up @@ -290,7 +290,7 @@ impl IntoFuture for ClientBuilder {
fn into_future(self) -> Self::IntoFuture {
let data = self.data.unwrap_or(Arc::new(()));
#[cfg(feature = "framework")]
let framework = self.framework;
let framework = self.framework;
let intents = self.intents;
let presence = self.presence;
let http = self.http;
Expand All @@ -315,10 +315,10 @@ impl IntoFuture for ClientBuilder {
}

#[cfg(feature = "voice")]
let voice_manager = self.voice_manager;
let voice_manager = self.voice_manager;

#[cfg(feature = "cache")]
let cache = Arc::new(Cache::new_with_settings(self.cache_settings));
let cache = Arc::new(Cache::new_with_settings(self.cache_settings));

Box::pin(async move {
let (ws_url, shard_total, max_concurrency) = match http.get_bot_gateway().await {
Expand All @@ -330,11 +330,11 @@ impl IntoFuture for ClientBuilder {
Err(err) => {
tracing::warn!("HTTP request to get gateway URL failed: {err}");
(Arc::from("wss://gateway.discord.gg"), NonZeroU16::MIN, NonZeroU16::MIN)
}
},
};

#[cfg(feature = "framework")]
let framework_cell = Arc::new(OnceLock::new());
let framework_cell = Arc::new(OnceLock::new());
let (shard_manager, shard_manager_ret_value) = ShardManager::new(ShardManagerOptions {
data: Arc::clone(&data),
event_handler,
Expand Down Expand Up @@ -765,7 +765,7 @@ impl Client {
#[cfg(feature = "voice")]
if let Some(voice_manager) = &self.voice_manager {
#[cfg(feature = "cache")]
let cache_user_id = {
let cache_user_id = {
let cache_user = self.cache.current_user();
if cache_user.id == UserId::default() {
None
Expand All @@ -775,7 +775,7 @@ impl Client {
};

#[cfg(not(feature = "cache"))]
let cache_user_id: Option<UserId> = None;
let cache_user_id: Option<UserId> = None;

let user_id = match cache_user_id {
Some(u) => u,
Expand Down
4 changes: 2 additions & 2 deletions src/collector.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Or we'll get deprecation warnings from our own deprecated type (seriously Rust?)
#![allow(deprecated)]

use std::sync::Arc;
use futures::future::pending;
use futures::{Stream, StreamExt as _};
use std::sync::Arc;

use crate::gateway::{CollectorCallback, ShardMessenger};
use crate::internal::prelude::*;
Expand Down Expand Up @@ -35,7 +35,7 @@ use crate::model::prelude::*;
pub fn collect<T: Send + 'static>(
shard: &ShardMessenger,
extractor: impl Fn(&Event) -> Option<T> + Send + Sync + 'static,
) -> impl Stream<Item=T> {
) -> impl Stream<Item = T> {
let (sender, mut receiver) = tokio::sync::mpsc::unbounded_channel();

// Register an event callback in the shard. It's kept alive as long as we return `true`
Expand Down
2 changes: 1 addition & 1 deletion src/gateway/bridge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,4 @@ impl PartialEq for CollectorCallback {
fn eq(&self, other: &Self) -> bool {
std::ptr::addr_eq(Arc::as_ptr(&self.0), Arc::as_ptr(&other.0))
}
}
}
77 changes: 33 additions & 44 deletions src/gateway/bridge/shard_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl ShardRunner {
Some(ShardAction::Reconnect(ReconnectType::Reidentify)) => {
self.request_restart().await;
return Ok(());
}
},
Some(other) => {
if let Err(e) = self.action(&other).await {
debug!(
Expand All @@ -150,7 +150,7 @@ impl ShardRunner {
ReconnectType::Reidentify => {
self.request_restart().await;
return Ok(());
}
},
ReconnectType::Resume => {
if let Err(why) = self.shard.resume().await {
warn!(
Expand All @@ -162,19 +162,20 @@ impl ShardRunner {
self.request_restart().await;
return Ok(());
}
}
},
};
}
}
None => {}
},
None => {},
}

if let Some(event) = event {
#[cfg(feature = "collector")]
{
let read_lock = self.collectors.read();
// search all collectors to be removed and clone the Arcs
let to_remove: Vec<_> = read_lock.iter().filter(|callback| !callback.0(&event)).cloned().collect();
let to_remove: Vec<_> =
read_lock.iter().filter(|callback| !callback.0(&event)).cloned().collect();
drop(read_lock);
// remove all found arcs from the collection
// this compares the inner pointer of the Arc
Expand All @@ -188,7 +189,7 @@ impl ShardRunner {
event,
self.make_context(),
#[cfg(feature = "framework")]
self.framework.clone(),
self.framework.clone(),
self.event_handler.clone(),
),
);
Expand Down Expand Up @@ -221,7 +222,7 @@ impl ShardRunner {
ShardAction::Reconnect(ReconnectType::Reidentify) => {
self.request_restart().await;
Ok(())
}
},
ShardAction::Reconnect(ReconnectType::Resume) => self.shard.resume().await,
ShardAction::Heartbeat => self.shard.heartbeat().await,
ShardAction::Identify => self.shard.identify().await,
Expand All @@ -246,10 +247,7 @@ impl ShardRunner {
drop(
self.shard
.client
.close(Some(CloseFrame {
code: close_code.into(),
reason: Cow::from(""),
}))
.close(Some(CloseFrame { code: close_code.into(), reason: Cow::from("") }))
.await,
);

Expand All @@ -264,7 +262,7 @@ impl ShardRunner {
self.shard.shard_info(),
);
break;
}
},
_ => continue,
}
}
Expand All @@ -281,7 +279,7 @@ impl ShardRunner {
self.shard.shard_info().id,
Arc::clone(&self.http),
#[cfg(feature = "cache")]
Arc::clone(&self.cache),
Arc::clone(&self.cache),
)
}

Expand All @@ -296,38 +294,29 @@ impl ShardRunner {
match msg {
ShardRunnerMessage::Restart(id) => self.checked_shutdown(id, 4000).await,
ShardRunnerMessage::Shutdown(id, code) => self.checked_shutdown(id, code).await,
ShardRunnerMessage::ChunkGuild {
guild_id,
limit,
presences,
filter,
nonce,
} => self
ShardRunnerMessage::ChunkGuild { guild_id, limit, presences, filter, nonce } => self
.shard
.chunk_guild(guild_id, limit, presences, filter, nonce.as_deref())
.await
.is_ok(),
ShardRunnerMessage::Close(code, reason) => {
let reason = reason.unwrap_or_default();
let close = CloseFrame {
code: code.into(),
reason: Cow::from(reason),
};
let close = CloseFrame { code: code.into(), reason: Cow::from(reason) };
self.shard.client.close(Some(close)).await.is_ok()
}
},
ShardRunnerMessage::Message(msg) => self.shard.client.send(msg).await.is_ok(),
ShardRunnerMessage::SetActivity(activity) => {
self.shard.set_activity(activity);
self.shard.update_presence().await.is_ok()
}
},
ShardRunnerMessage::SetPresence(activity, status) => {
self.shard.set_presence(activity, status);
self.shard.update_presence().await.is_ok()
}
},
ShardRunnerMessage::SetStatus(status) => {
self.shard.set_status(status);
self.shard.update_presence().await.is_ok()
}
},
}
}

Expand All @@ -340,18 +329,18 @@ impl ShardRunner {
voice_manager
.register_shard(self.shard.shard_info().id.0, self.runner_tx.clone())
.await;
}
},
Event::VoiceServerUpdate(event) => {
voice_manager
.server_update(event.guild_id, event.endpoint.as_deref(), &event.token)
.await;
}
},
Event::VoiceStateUpdate(event) => {
if let Some(guild_id) = event.voice_state.guild_id {
voice_manager.state_update(guild_id, &event.voice_state).await;
}
}
_ => {}
},
_ => {},
}
}
}
Expand All @@ -371,7 +360,7 @@ impl ShardRunner {
if !self.handle_rx_value(value).await {
return false;
}
}
},
Ok(None) => {
warn!(
"[ShardRunner {:?}] Sending half DC; restarting",
Expand All @@ -380,7 +369,7 @@ impl ShardRunner {

self.request_restart().await;
return false;
}
},
Err(_) => break,
}
}
Expand All @@ -397,7 +386,7 @@ impl ShardRunner {
Ok(Some(inner)) => Ok(inner),
Ok(None) => {
return Ok((None, None, true));
}
},
Err(Error::Tungstenite(tung_err)) if matches!(*tung_err, TungsteniteError::Io(_)) => {
debug!("Attempting to auto-reconnect");

Expand All @@ -412,32 +401,32 @@ impl ShardRunner {

return Ok((None, None, false));
}
}
},
}

return Ok((None, None, true));
}
},
Err(why) => Err(why),
};

let is_ack = matches!(gateway_event, Ok(GatewayEvent::HeartbeatAck));
let (action, event) = match self.shard.handle_event(gateway_event) {
Ok((action, event)) => (action, event),
Err(Error::Gateway(
why @ (GatewayError::InvalidAuthentication
| GatewayError::InvalidGatewayIntents
| GatewayError::DisallowedGatewayIntents),
)) => {
why @ (GatewayError::InvalidAuthentication
| GatewayError::InvalidGatewayIntents
| GatewayError::DisallowedGatewayIntents),
)) => {
error!("Shard handler received fatal err: {why:?}");

self.manager.return_with_value(Err(why.clone())).await;
return Err(Error::Gateway(why));
}
},
Err(Error::Json(_)) => return Ok((None, None, true)),
Err(why) => {
error!("Shard handler recieved err: {why:?}");
return Ok((None, None, true));
}
},
};

if is_ack {
Expand Down

0 comments on commit 643c76a

Please sign in to comment.