Skip to content

Commit

Permalink
refactor: make clippy pedantic
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanccn committed Nov 12, 2023
1 parent 0df526d commit a1f786f
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/commands/presence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub enum PresenceChoice {
}

impl PresenceChoice {
fn make_activity(&self, content: &str) -> serenity::ActivityData {
fn make_activity(self, content: &str) -> serenity::ActivityData {
match self {
Self::Custom => serenity::ActivityData::custom(content),
Self::Playing => serenity::ActivityData::playing(content),
Expand Down
56 changes: 30 additions & 26 deletions src/commands/shiggy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,42 @@ pub async fn shiggy(

let resp = crate::reqwest_client::HTTP.get(url).send().await?;

if resp.status().is_success() {
let data: SafebooruResponse = resp.json().await?;
if !raw {
ctx.send(
CreateReply::new().embed(
serenity::CreateEmbed::new()
.title(data.id.to_string())
.field("Tags", data.tag_string.replace('_', "\\_"), false)
.field("Source", data.source, false)
.url(format!("https://safebooru.donmai.us/posts/{}", data.id))
.image(data.file_url)
.color(0xfef9c3),
),
)
.await?;
} else {
ctx.say(data.file_url).await?;
match resp.error_for_status() {
Ok(resp) => {
let data: SafebooruResponse = resp.json().await?;

if raw {
ctx.say(data.file_url).await?;
} else {
ctx.send(
CreateReply::new().embed(
serenity::CreateEmbed::new()
.title(data.id.to_string())
.field("Tags", data.tag_string.replace('_', "\\_"), false)
.field("Source", data.source, false)
.url(format!("https://safebooru.donmai.us/posts/{}", data.id))
.image(data.file_url)
.color(0xfef9c3),
),
)
.await?;
}
}
} else {
let mut embed = serenity::CreateEmbed::new()
.title("Could not fetch shiggy!")
.description("An error occurred while fetching from the API.")
.color(0xef4444);

if let Err(err) = resp.error_for_status_ref().map_err(anyhow::Error::from) {
Err(err) => {
let err = anyhow::Error::from(err);
let valfisk_err = ValfiskError::new(&err, &ctx);
valfisk_err.handle_log();
valfisk_err.handle_report().await;
embed = embed.footer(serenity::CreateEmbedFooter::new(valfisk_err.error_id))
};

ctx.send(CreateReply::new().embed(embed)).await?;
let embed = serenity::CreateEmbed::new()
.title("Could not fetch shiggy!")
.description("An error occurred while fetching from the API.")
.color(0xef4444)
.footer(serenity::CreateEmbedFooter::new(valfisk_err.error_id));

ctx.send(CreateReply::new().embed(embed)).await?;
}
}

Ok(())
Expand Down
3 changes: 1 addition & 2 deletions src/commands/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ pub async fn translate(ctx: Context<'_>, message: serenity::Message) -> Result<(
.sentences
.into_iter()
.filter_map(|s| s.trans)
.collect::<Vec<String>>()
.join("");
.collect::<String>();

ctx.send(
CreateReply::new().embed(
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/github_expansion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub async fn handle(message: &serenity::Message, ctx: &serenity::Context) -> Res
.text()
.await?
.split('\n')
.map(|s| s.to_owned())
.map(std::borrow::ToOwned::to_owned)
.collect();

let idx_start = start - 1;
Expand Down
9 changes: 8 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
#![warn(clippy::all, clippy::pedantic)]
#![allow(
clippy::unreadable_literal,
clippy::module_name_repetitions,
clippy::unused_async
)]
#![deny(unsafe_code)]

use anyhow::{Context as AnyhowContext, Error, Result};
use owo_colors::OwoColorize;

Expand All @@ -19,7 +27,6 @@ mod presence_api;
mod reqwest_client;
mod utils;

#[deny(unsafe_code)]
#[tokio::main]
async fn main() -> Result<()> {
#[cfg(debug_assertions)]
Expand Down
7 changes: 4 additions & 3 deletions src/presence_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ async fn route_get_presence(path: web::Path<(u64,)>) -> Result<impl Responder, A
}

pub async fn serve() -> anyhow::Result<()> {
let host = std::env::var("HOST").unwrap_or(match cfg!(debug_assertions) {
true => "127.0.0.1".to_owned(),
false => "0.0.0.0".to_owned(),
let host = std::env::var("HOST").unwrap_or(if cfg!(debug_assertions) {
"127.0.0.1".to_owned()
} else {
"0.0.0.0".to_owned()
});
let port = std::env::var("PORT").map_or(Ok(8080), |v| v.parse::<u16>())?;

Expand Down
4 changes: 2 additions & 2 deletions src/utils/pluralize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ pub trait Pluralize {

impl Pluralize for String {
fn pluralize<T: Integer>(&self, count: T) -> String {
self.pluralize_alternate(count, &(self.to_owned() + "s"))
self.pluralize_alternate(count, &(self.clone() + "s"))
}
fn pluralize_alternate<T: Integer>(&self, count: T, alternate: &str) -> String {
if count.is_one() {
self.to_owned()
self.clone()
} else {
alternate.to_owned()
}
Expand Down

0 comments on commit a1f786f

Please sign in to comment.