Skip to content

Commit

Permalink
Merge pull request #480 from twitch-rs/clippy-suggestions
Browse files Browse the repository at this point in the history
clippy suggestions
  • Loading branch information
Emilgardis authored Feb 22, 2025
2 parents a930d64 + ab7221a commit 738679e
Show file tree
Hide file tree
Showing 80 changed files with 216 additions and 198 deletions.
2 changes: 1 addition & 1 deletion examples/chatbot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ impl Bot {
&payload.message_id,
response
.response
.replace("{user}", &payload.chatter_user_name.as_str())
.replace("{user}", payload.chatter_user_name.as_str())
.as_str(),
token,
)
Expand Down
2 changes: 1 addition & 1 deletion examples/chatbot/src/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl ChatWebsocketClient {
if let Some(url) = data.reconnect_url {
self.connect_url = url.parse()?;
}
let mut token = self.token.lock().await;
let token = self.token.lock().await;
let transport = eventsub::Transport::websocket(data.id.clone());
for id in &self.chats {
let user_id = token.user_id().unwrap().to_owned();
Expand Down
7 changes: 4 additions & 3 deletions examples/eventsub/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,10 @@ async fn write(
if let Ok(msg) = val.to_message() {
if let Err(error) = sender.send(msg).await {
if let Some(e) = error.source() {
if let Some(tokio_tungstenite::tungstenite::error::Error::ConnectionClosed) =
e.downcast_ref()
{
if matches!(
e.downcast_ref(),
Some(tokio_tungstenite::tungstenite::error::Error::ConnectionClosed)
) {
// NOOP
} else {
return Err(error.into());
Expand Down
2 changes: 1 addition & 1 deletion examples/eventsub/src/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl std::str::FromStr for SignSecret {
type Err = std::convert::Infallible;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(SignSecret {
Ok(Self {
secret: s.to_string(),
})
}
Expand Down
6 changes: 3 additions & 3 deletions examples/eventsub/src/twitch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ pub async fn checker(
Ok::<(), eyre::Report>(())
}

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LiveStatus {
Live {
started_at: types::Timestamp,
Expand All @@ -331,12 +331,12 @@ impl LiveStatus {
/// Returns `true` if the live status is [`Live`].
///
/// [`Live`]: LiveStatus::Live
pub fn is_live(&self) -> bool { matches!(self, Self::Live { .. }) }
pub const fn is_live(&self) -> bool { matches!(self, Self::Live { .. }) }

/// Returns `true` if the live status is [`Offline`].
///
/// [`Offline`]: LiveStatus::Offline
pub fn is_offline(&self) -> bool { matches!(self, Self::Offline { .. }) }
pub const fn is_offline(&self) -> bool { matches!(self, Self::Offline { .. }) }

pub fn to_message(&self) -> eyre::Result<ws::Message> {
#[derive(serde_derive::Serialize)]
Expand Down
10 changes: 5 additions & 5 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,18 @@ pub trait ClientDefault<'a>: Clone + Sized {
pub struct DummyHttpClient;

impl Client for DummyHttpClient {
type Error = DummyHttpClient;
type Error = Self;

fn req(&self, _: Request) -> BoxedFuture<'_, Result<Response, Self::Error>> {
Box::pin(async { Err(DummyHttpClient) })
Box::pin(async { Err(Self) })
}
}

impl Client for twitch_oauth2::client::DummyClient {
type Error = twitch_oauth2::client::DummyClient;
type Error = Self;

fn req(&self, _: Request) -> BoxedFuture<'_, Result<Response, Self::Error>> {
Box::pin(async { Err(twitch_oauth2::client::DummyClient) })
Box::pin(async { Err(Self) })
}
}

Expand All @@ -187,7 +187,7 @@ where C: Client
impl ClientDefault<'static> for DummyHttpClient
where Self: Default
{
type Error = DummyHttpClient;
type Error = Self;

fn default_client_with_name(_: Option<http::HeaderValue>) -> Result<Self, Self::Error> {
Ok(Self)
Expand Down
2 changes: 1 addition & 1 deletion src/client/surf_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ where Self: Default
}
}

let client = surf::Client::default();
let client = Self::default();
let user_agent = if let Some(product) = product {
let mut user_agent = product.as_bytes().to_owned();
user_agent.push(b' ');
Expand Down
6 changes: 3 additions & 3 deletions src/client/tower_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ impl<S, ReqBody> TowerService<S, ReqBody> {
/// Make sure your service stack can be cloned, one easy way to ensure this is to use [`tower::buffer`](https://docs.rs/tower/*/tower/builder/struct.ServiceBuilder.html#method.buffer)
pub fn new(s: S) -> Self
where S: Clone {
TowerService(s, std::marker::PhantomData)
Self(s, std::marker::PhantomData)
}
}

impl<S: Default, ReqBody> Default for TowerService<S, ReqBody> {
fn default() -> Self { TowerService(<_>::default(), std::marker::PhantomData) }
fn default() -> Self { Self(<_>::default(), std::marker::PhantomData) }
}

impl<S: Clone, ReqBody> Clone for TowerService<S, ReqBody> {
fn clone(&self) -> Self { TowerService(self.0.clone(), std::marker::PhantomData) }
fn clone(&self) -> Self { Self(self.0.clone(), std::marker::PhantomData) }
}
8 changes: 4 additions & 4 deletions src/eventsub/channel/chat/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ impl Fragment {
/// Get the text data
pub fn text(&self) -> &str {
match self {
Fragment::Cheermote { text, .. } => text,
Fragment::Emote { text, .. } => text,
Fragment::Mention { text, .. } => text,
Fragment::Text { text } => text,
Self::Cheermote { text, .. } => text,
Self::Emote { text, .. } => text,
Self::Mention { text, .. } => text,
Self::Text { text } => text,
}
}
}
Expand Down
30 changes: 15 additions & 15 deletions src/eventsub/channel/chat/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ impl Chatter {
///
/// [`Anonymous`]: Chatter::Anonymous
#[must_use]
pub fn is_anonymous(&self) -> bool { matches!(self, Self::Anonymous) }
pub const fn is_anonymous(&self) -> bool { matches!(self, Self::Anonymous) }

/// Returns `true` if the chatter is [`Chatter`].
///
/// [`Chatter`]: Chatter::Chatter
#[must_use]
pub fn is_chatter(&self) -> bool { matches!(self, Self::Chatter { .. }) }
pub const fn is_chatter(&self) -> bool { matches!(self, Self::Chatter { .. }) }
}

impl serde::Serialize for Chatter {
Expand All @@ -124,7 +124,7 @@ impl serde::Serialize for Chatter {
}

match self {
Chatter::Chatter {
Self::Chatter {
chatter_user_id,
chatter_user_name,
chatter_user_login,
Expand All @@ -136,7 +136,7 @@ impl serde::Serialize for Chatter {
color: Some(color),
chatter_is_anonymous: false,
},
Chatter::Anonymous => InnerChatter {
Self::Anonymous => InnerChatter {
chatter_is_anonymous: true,
..Default::default()
},
Expand Down Expand Up @@ -165,9 +165,9 @@ impl<'de> serde::Deserialize<'de> for Chatter {
tracing::error!("got an anonymous user with color set to {c}");
}
}
Ok(Chatter::Anonymous)
Ok(Self::Anonymous)
} else {
Ok(Chatter::Chatter {
Ok(Self::Chatter {
chatter_user_id: chatter
.chatter_user_id
.ok_or_else(|| serde::de::Error::missing_field("chatter_user_id"))?,
Expand Down Expand Up @@ -350,13 +350,13 @@ impl Gifter {
///
/// [`Anonymous`]: Gifter::Anonymous
#[must_use]
pub fn is_anonymous(&self) -> bool { matches!(self, Self::Anonymous) }
pub const fn is_anonymous(&self) -> bool { matches!(self, Self::Anonymous) }

/// Returns `true` if the gifter is [`Gifter`].
///
/// [`Gifter`]: Gifter::Gifter
#[must_use]
pub fn is_gifter(&self) -> bool { matches!(self, Self::Gifter { .. }) }
pub const fn is_gifter(&self) -> bool { matches!(self, Self::Gifter { .. }) }
}

impl serde::Serialize for Gifter {
Expand All @@ -371,7 +371,7 @@ impl serde::Serialize for Gifter {
}

match self {
Gifter::Gifter {
Self::Gifter {
gifter_user_id,
gifter_user_name,
gifter_user_login,
Expand All @@ -381,11 +381,11 @@ impl serde::Serialize for Gifter {
gifter_user_login: Some(gifter_user_login),
gifter_is_anonymous: Some(false),
},
Gifter::Anonymous => InnerGifter {
Self::Anonymous => InnerGifter {
gifter_is_anonymous: Some(true),
..Default::default()
},
Gifter::None => Default::default(),
Self::None => Default::default(),
}
.serialize(serializer)
}
Expand All @@ -403,12 +403,12 @@ impl<'de> serde::Deserialize<'de> for Gifter {
}

let gifter = InnerGifter::deserialize(deserializer)?;
if let Some(true) = gifter.gifter_is_anonymous {
Ok(Gifter::Anonymous)
if gifter.gifter_is_anonymous == Some(true) {
Ok(Self::Anonymous)
} else if gifter.gifter_is_anonymous.is_none() {
Ok(Gifter::None)
Ok(Self::None)
} else {
Ok(Gifter::Gifter {
Ok(Self::Gifter {
gifter_user_id: gifter
.gifter_user_id
.ok_or_else(|| serde::de::Error::missing_field("gifter_user_id"))?,
Expand Down
12 changes: 6 additions & 6 deletions src/eventsub/channel/moderate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ pub struct ChannelModerateV1Payload {
/// Is [Some] when in a shared chat session, and the action happens in the channel of a participant other than the broadcaster.
pub source_broadcaster_user_name: Option<types::DisplayName>,

/// The broadcaster user ID.
/// The user ID of moderator who performed the action.
pub moderator_user_id: types::UserId,
/// The broadcaster display name.
/// The display name of the moderator who performed the action.
pub moderator_user_name: types::DisplayName,
/// The broadcaster login.
/// The login of the moderator who performed the action.
pub moderator_user_login: types::UserName,
/// The action being taken
#[serde(flatten)]
Expand Down Expand Up @@ -743,11 +743,11 @@ pub struct ChannelModerateV2Payload {
/// Is [Some] when in a shared chat session, and the action happens in the channel of a participant other than the broadcaster.
pub source_broadcaster_user_name: Option<types::DisplayName>,

/// The broadcaster user ID.
/// The user ID of moderator who performed the action.
pub moderator_user_id: types::UserId,
/// The broadcaster display name.
/// The display name of the moderator who performed the action.
pub moderator_user_name: types::DisplayName,
/// The broadcaster login.
/// The login of the moderator who performed the action.
pub moderator_user_login: types::UserName,
/// The action being taken
#[serde(flatten)]
Expand Down
14 changes: 7 additions & 7 deletions src/eventsub/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ pub enum Event {

impl Event {
/// Parse string slice as an [`Event`]. Consider using [`Event::parse_http`] instead.
pub fn parse(source: &str) -> Result<Event, PayloadParseError> {
pub fn parse(source: &str) -> Result<Self, PayloadParseError> {
let (version, ty, message_type) =
get_version_event_type_and_message_type_from_text(source)?;
Self::parse_request(version, &ty, message_type, source.as_bytes().into())
Expand All @@ -523,21 +523,21 @@ impl Event {
/// Returns `true` if the message in the [`Payload`] is [`Notification`].
///
/// [`Notification`]: Message::Notification
pub fn is_notification(&self) -> bool { is_thing!(self, Notification) }
pub const fn is_notification(&self) -> bool { is_thing!(self, Notification) }

/// Returns `true` if the message in the [`Payload`] is [`Revocation`].
///
/// [`Revocation`]: Message::Revocation
pub fn is_revocation(&self) -> bool { is_thing!(self, Revocation) }
pub const fn is_revocation(&self) -> bool { is_thing!(self, Revocation) }

/// Returns `true` if the message in the [`Payload`] is [`VerificationRequest`].
///
/// [`VerificationRequest`]: Message::VerificationRequest
pub fn is_verification_request(&self) -> bool { is_thing!(self, VerificationRequest) }
pub const fn is_verification_request(&self) -> bool { is_thing!(self, VerificationRequest) }

/// If this event is a [`VerificationRequest`], return the [`VerificationRequest`] message, including the message.
#[rustfmt::skip]
pub fn get_verification_request(&self) -> Option<&VerificationRequest> {
pub const fn get_verification_request(&self) -> Option<&VerificationRequest> {
macro_rules! match_event {
($($(#[$meta:meta])* $module:ident::$event:ident);* $(;)?) => {{

Expand Down Expand Up @@ -735,7 +735,7 @@ impl Event {
/// Parse a http payload as an [`Event`]
///
/// Create the webhook via [`CreateEventSubSubscription`](crate::helix::eventsub::CreateEventSubSubscriptionRequest) according to the [Eventsub WebHooks guide](https://dev.twitch.tv/docs/eventsub/handling-webhook-events)
pub fn parse_http<B>(request: &http::Request<B>) -> Result<Event, PayloadParseError>
pub fn parse_http<B>(request: &http::Request<B>) -> Result<Self, PayloadParseError>
where B: AsRef<[u8]> {
let (version, ty, message_type) =
get_version_event_type_and_message_type_from_http(request)?;
Expand All @@ -750,7 +750,7 @@ impl Event {
event_type: &'a EventType,
message_type: Cow<'a, [u8]>,
source: Cow<'a, [u8]>,
) -> Result<Event, PayloadParseError> {
) -> Result<Self, PayloadParseError> {
/// Match on all defined eventsub types.
///
/// If this is not done, we'd get a much worse error message.
Expand Down
Loading

0 comments on commit 738679e

Please sign in to comment.