diff --git a/src/bots/leek.rs b/src/bots/leek.rs index 3d81459..11f479c 100644 --- a/src/bots/leek.rs +++ b/src/bots/leek.rs @@ -1,4 +1,4 @@ -use arrayvec::{ArrayString, CapacityError}; +use arrayvec::{ArrayString}; use rand::Rng; use std::{ error::Error, @@ -6,25 +6,25 @@ use std::{ }; #[derive(Debug)] -pub struct LeekCapacityError(CapacityError); +pub struct CapacityError(arrayvec::CapacityError); -impl Display for LeekCapacityError { +impl Display for CapacityError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { Display::fmt(&self.0, f) } } -impl Error for LeekCapacityError {} +impl Error for CapacityError {} -impl From> for LeekCapacityError { - fn from(e: CapacityError) -> Self { - Self { 0: e.simplify() } +impl From> for CapacityError { + fn from(e: arrayvec::CapacityError) -> Self { + Self(e.simplify()) } } -type LeekResult = Result, LeekCapacityError>; +type LeekResult = Result, CapacityError>; -fn mock(input: &str) -> LeekResult { +fn mock(input: &str) -> ArrayString<512> { let mut builder = ArrayString::<512>::new(); for ch in input.chars() { @@ -35,10 +35,10 @@ fn mock(input: &str) -> LeekResult { } } - Ok(builder) + builder } -fn leetify(input: &str) -> LeekResult { +fn leetify(input: &str) -> ArrayString<512> { let mut builder = ArrayString::<512>::new(); for ch in input.chars() { @@ -55,7 +55,7 @@ fn leetify(input: &str) -> LeekResult { }); } - Ok(builder) + builder } fn owoify(input: &str) -> LeekResult { @@ -102,16 +102,16 @@ fn owoify(input: &str) -> LeekResult { Ok(builder) } -#[derive(Debug)] -pub enum LeekCommand { +#[derive(Debug, Clone, Copy)] +pub enum Command { Owo, Leet, Mock, } -pub fn execute_leek( +pub fn execute( state: &mut crate::AppState, - cmd: LeekCommand, + cmd: Command, target: &str, nick: &str, ) -> anyhow::Result<()> { @@ -119,9 +119,9 @@ pub fn execute_leek( Some(msg) => { tracing::debug!("Executing {:?} on {:?}", cmd, msg); let output = match cmd { - LeekCommand::Owo => super::leek::owoify(msg)?, - LeekCommand::Leet => super::leek::leetify(msg)?, - LeekCommand::Mock => super::leek::mock(msg)?, + Command::Owo => super::leek::owoify(msg)?, + Command::Leet => super::leek::leetify(msg), + Command::Mock => super::leek::mock(msg), }; state.client.send_privmsg(target, &output)?; } diff --git a/src/bots/misc.rs b/src/bots/misc.rs index e5c88e0..893ed7d 100644 --- a/src/bots/misc.rs +++ b/src/bots/misc.rs @@ -10,8 +10,8 @@ pub async fn get_waifu_pic(category: &str) -> anyhow::Result> { .text() .await?; let api_resp = api_resp.trim(); - let value: Value = serde_json::from_str(&api_resp)?; - let url = value["url"].as_str().map(|v| v.to_string()); + let value: Value = serde_json::from_str(api_resp)?; + let url = value["url"].as_str().map(ToString::to_string); Ok(url) } diff --git a/src/bots/sed.rs b/src/bots/sed.rs index 4489c9c..afbb63f 100644 --- a/src/bots/sed.rs +++ b/src/bots/sed.rs @@ -5,6 +5,7 @@ use fancy_regex::Regex; use lazy_static::lazy_static; use sedregex::find_and_replace; +#[allow(clippy::module_name_repetitions)] #[derive(Debug)] pub enum SedError { Capacity(CapacityError), @@ -52,7 +53,7 @@ pub fn resolve(prev_msg: &str, cmd: &str) -> SedResult { if RE.is_match(cmd)? { return if let Some(mat) = RE.find(cmd)? { let slice = &cmd[mat.start()..mat.end()]; - let formatted = find_and_replace(&prev_msg, [slice])?; + let formatted = find_and_replace(prev_msg, [slice])?; Ok(Some(ArrayString::from(&formatted)?)) } else { Ok(None) diff --git a/src/bots/title.rs b/src/bots/title.rs index 45543c7..9bb8f28 100644 --- a/src/bots/title.rs +++ b/src/bots/title.rs @@ -101,7 +101,7 @@ impl Titlebot { } pub async fn resolve(&mut self, message: &str) -> anyhow::Result> { - if let Some(m) = self.spotify_regex.captures(&message)? { + if let Some(m) = self.spotify_regex.captures(message)? { tracing::debug!("{}", message); let tp_group = m.get(1).unwrap(); let id_group = m.get(2).unwrap(); @@ -114,7 +114,7 @@ impl Titlebot { ) .await?, )); - } else if let Some(m) = self.url_regex.find(&message)? { + } else if let Some(m) = self.url_regex.find(message)? { let url = &message[m.start()..m.end()]; tracing::debug!("url: {}", url); diff --git a/src/main.rs b/src/main.rs index 1005f2a..4433035 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +#![allow(clippy::match_wildcard_for_single_variants)] + use std::fmt::Write; use std::fs::File; use std::io::Read; @@ -154,7 +156,7 @@ async fn main() -> anyhow::Result<()> { }; let message_loop_task = tokio::spawn(async move { if let Err(e) = message_loop(state).await { - let _ = etx.send(e); + let _err = etx.send(e); } }); @@ -219,6 +221,7 @@ fn separate_to_space(str: &str, prefix_len: usize) -> (&str, Option<&str>) { } } +#[allow(clippy::too_many_lines)] async fn handle_privmsg( state: &mut AppState, author: &str, @@ -255,32 +258,30 @@ async fn handle_privmsg( "waifu" => { let category = remainder.unwrap_or("waifu"); let url = misc::get_waifu_pic(category).await?; - let response = url - .as_ref() - .map(|v| v.as_str()) + let response = url.as_deref() .unwrap_or("Invalid category. Valid categories: https://waifu.pics/docs"); state.client.send_privmsg(origin, response)?; } "mock" => { - leek::execute_leek( + leek::execute( state, - leek::LeekCommand::Mock, + leek::Command::Mock, origin, remainder.unwrap_or(author), )?; } "leet" => { - leek::execute_leek( + leek::execute( state, - leek::LeekCommand::Leet, + leek::Command::Leet, origin, remainder.unwrap_or(author), )?; } "owo" => { - leek::execute_leek( + leek::execute( state, - leek::LeekCommand::Owo, + leek::Command::Owo, origin, remainder.unwrap_or(author), )?; @@ -315,7 +316,7 @@ async fn handle_privmsg( } } "quot" => { - if let Some(quote) = state.db.get_quote(remainder.map(|v| v.to_string())).await { + if let Some(quote) = state.db.get_quote(remainder.map(ToString::to_string)).await { let mut resp = ArrayString::<512>::new(); write!(resp, "\"{}\" ~{}", quote.0, quote.1)?; state.client.send_privmsg(origin, &resp)?; diff --git a/src/web_service.rs b/src/web_service.rs index d557335..9e7bc04 100644 --- a/src/web_service.rs +++ b/src/web_service.rs @@ -17,31 +17,48 @@ pub async fn run( let quote_get = warp::path("quotes") .and(warp::get()) .and(warp::any().map(move || db.clone())) - .then(handle_get_quote); + .map(handle_get_quote); let webhook_post = warp::path("webhook") .and(warp::post()) .and(warp::body::json()) .and(warp::any().map(move || wh_irc.clone())) .and(warp::any().map(move || wh_channel.clone())) - .then(handle_webhook); + .map(handle_webhook); let filter = quote_get.or(webhook_post); warp::serve(filter).bind_with_graceful_shutdown(listen, async move { let _ = cancel.recv().await; }).1.await; - tracing::info!("Web service finished") + tracing::info!("Web service finished"); } -async fn handle_get_quote(_: ExecutorConnection) -> impl Reply { +fn handle_get_quote(_: ExecutorConnection) -> impl Reply { reply::html(include_str!("res/quote_tmpl.html")) } -async fn handle_webhook(json: serde_json::Value, irc: Arc, channel: String) -> impl Reply { +#[allow(clippy::needless_pass_by_value)] +fn handle_webhook(json: serde_json::Value, irc: Arc, channel: String) -> impl Reply { if json["commits"] != Null { let commits = json["commits"].as_array().unwrap(); let repo = &json["repository"]["full_name"].as_str().unwrap().trim(); - if commits.len() != 1 { + if commits.len() == 1 { + let author = &json["commits"][0]["author"]["name"] + .as_str() + .unwrap() + .trim(); + let message = &json["commits"][0]["message"].as_str().unwrap().trim(); + if let Err(e) = irc.send_privmsg( + channel, + format!("New commit on {}: {} - {}", repo, message, author), + ) { + return reply::with_status( + format!("An error has occurred: {}", e), + StatusCode::INTERNAL_SERVER_ERROR, + ) + .into_response(); + } + } else { if let Err(e) = irc.send_privmsg( channel.clone(), format!("{} new commits on {}:", commits.len(), repo), @@ -65,22 +82,6 @@ async fn handle_webhook(json: serde_json::Value, irc: Arc, channel: Stri .into_response(); } } - } else { - let author = &json["commits"][0]["author"]["name"] - .as_str() - .unwrap() - .trim(); - let message = &json["commits"][0]["message"].as_str().unwrap().trim(); - if let Err(e) = irc.send_privmsg( - channel, - format!("New commit on {}: {} - {}", repo, message, author), - ) { - return reply::with_status( - format!("An error has occurred: {}", e), - StatusCode::INTERNAL_SERVER_ERROR, - ) - .into_response(); - } } } StatusCode::CREATED.into_response()