Skip to content

Commit

Permalink
Extract TwitchApi wrapper traits
Browse files Browse the repository at this point in the history
  • Loading branch information
fusillicode committed Sep 24, 2023
1 parent fad9c31 commit 17e23c7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
12 changes: 10 additions & 2 deletions src/xddmod/src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ pub mod rip_bozo;
pub mod sniffa;
pub mod the_grind;

pub trait TwitchApiClient: twitch_api::HttpClient + twitch_api::twitch_oauth2::client::Client {}

impl<T: twitch_api::HttpClient + twitch_api::twitch_oauth2::client::Client> TwitchApiClient for T {}

pub trait TwitchApiError: std::error::Error + Send + Sync + 'static {}

impl<T: std::error::Error + Send + Sync + 'static> TwitchApiError for T {}

#[derive(thiserror::Error, Debug)]
pub enum HandlerError<T: Transport, L: LoginCredentials, RE: std::error::Error + Send + Sync + 'static> {
pub enum HandlerError<T: Transport, L: LoginCredentials, RE: TwitchApiError> {
#[error(transparent)]
Persistence(#[from] persistence::PersistenceError),
#[error(transparent)]
Expand All @@ -23,7 +31,7 @@ pub enum HandlerError<T: Transport, L: LoginCredentials, RE: std::error::Error +
}

#[derive(thiserror::Error, Debug)]
pub enum TwitchError<T: Transport, L: LoginCredentials, RE: std::error::Error + Send + Sync + 'static> {
pub enum TwitchError<T: Transport, L: LoginCredentials, RE: TwitchApiError> {
#[error(transparent)]
Irc(#[from] twitch_irc::Error<T, L>),
#[error(transparent)]
Expand Down
3 changes: 2 additions & 1 deletion src/xddmod/src/handlers/npc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use twitch_irc::TwitchIRCClient;
use crate::handlers::persistence::Handler;
use crate::handlers::persistence::Reply;
use crate::handlers::HandlerError;
use crate::handlers::TwitchApiError;
use crate::handlers::TwitchError;
use crate::poor_man_throttling;

Expand All @@ -26,7 +27,7 @@ impl<'a, T: Transport, L: LoginCredentials> Npc<'a, T, L> {
}

impl<'a, T: Transport, L: LoginCredentials> Npc<'a, T, L> {
pub async fn handle<RE: std::error::Error + Send + Sync + 'static>(
pub async fn handle<RE: TwitchApiError>(
&self,
server_message: &ServerMessage,
) -> Result<(), HandlerError<T, L, RE>> {
Expand Down
9 changes: 4 additions & 5 deletions src/xddmod/src/handlers/rip_bozo/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,37 @@ use lazy_static::lazy_static;
use regex::Captures;
use regex::Regex;
use sqlx::SqlitePool;
use twitch_api::twitch_oauth2::client::Client as TwitchOauth2Client;
use twitch_api::twitch_oauth2::TwitchToken;
use twitch_api::twitch_oauth2::UserToken;
use twitch_api::HelixClient;
use twitch_api::HttpClient as TwitchHttpClient;
use twitch_irc::message::PrivmsgMessage;
use twitch_irc::message::ServerMessage;
use twitch_types::UserId;
use unicode_segmentation::UnicodeSegmentation;

use crate::apis::twitch;
use crate::handlers::persistence::Handler;
use crate::handlers::TwitchApiClient;

lazy_static! {
static ref EMOJI_REGEX: Regex = Regex::new(r"\p{Emoji}").unwrap();
static ref MENTION_REGEX: Regex = Regex::new(r"(@(\w+))").unwrap();
}

pub struct RipBozo<'a, C: TwitchHttpClient + TwitchOauth2Client> {
pub struct RipBozo<'a, C: TwitchApiClient> {
pub broadcaster_id: UserId,
pub token: UserToken,
pub helix_client: HelixClient<'a, C>,
pub db_pool: SqlitePool,
}

impl<'a, C: TwitchHttpClient + TwitchOauth2Client> RipBozo<'a, C> {
impl<'a, C: TwitchApiClient> RipBozo<'a, C> {
pub fn handler(&self) -> Handler {
Handler::RipBozo
}
}

impl<'a, C: TwitchHttpClient + TwitchOauth2Client> RipBozo<'a, C> {
impl<'a, C: TwitchApiClient> RipBozo<'a, C> {
pub async fn handle(&mut self, server_message: &ServerMessage) -> anyhow::Result<bool> {
if let ServerMessage::Privmsg(message @ PrivmsgMessage { is_action: false, .. }) = server_message {
if twitch::helpers::is_from_streamer_or_mod(message) {
Expand Down

0 comments on commit 17e23c7

Please sign in to comment.