From 014a77a8503697cd907f0e89c62729847e02515c Mon Sep 17 00:00:00 2001 From: Sergio Ribera <56278796+SergioRibera@users.noreply.github.com> Date: Thu, 30 May 2024 00:47:13 -0400 Subject: [PATCH] fix: clippy suggestion to use unwrap_or_else --- src/cangrebot.rs | 2 +- src/lib.rs | 40 ++++++++++++++++++++++------------------ 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/cangrebot.rs b/src/cangrebot.rs index cb5e6fc..7764976 100644 --- a/src/cangrebot.rs +++ b/src/cangrebot.rs @@ -70,7 +70,7 @@ pub async fn build_message( ) { for (_, e) in three_days { let timestamp = OffsetDateTime::parse(&e.start.date_time, &Rfc3339) - .expect(&format!("Cannot parse date {}", e.start.date_time)) + .unwrap_or_else(|err| panic!("Cannot parse date {}: {err}", e.start.date_time)) .unix_timestamp(); let msg = THREE_DAYS .replace("@announce@", "đŸ“¢ ¡Atencion @anuncios !") diff --git a/src/lib.rs b/src/lib.rs index 6a12ccb..66632b2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,13 +1,13 @@ use reqwest::ClientBuilder; use time::format_description::well_known::Rfc3339; use time::{Duration, OffsetDateTime}; -use worker::{console_log, event, Env, ScheduleContext, ScheduledEvent}; +use worker::{event, Env, ScheduleContext, ScheduledEvent}; pub mod calendar; pub mod cangrebot; #[cfg(target_arch = "wasm32")] -use worker::{console_debug, console_error}; +use worker::{console_log, console_debug, console_error}; #[derive(Debug, PartialEq, Eq)] pub enum EventDateType { @@ -29,70 +29,74 @@ pub async fn main(_e: ScheduledEvent, env: Env, _ctx: ScheduleContext) { let calendar_api = env .secret("GOOGLE_APIKEY") .map(|e| e.to_string()) - .expect("Calendar Google API Secret not found"); + .unwrap_or_else(|_| panic!("Calendar Google API Secret not found")); let calendar_id = env .secret("GOOGLE_CALENDAR_ID") .map(|e| e.to_string()) - .expect("Calendar Google ID Secret not found"); + .unwrap_or_else(|_| panic!("Calendar Google ID Secret not found")); let endpoint = env .secret("ENDPOINT") .map(|e| e.to_string()) - .expect("Endpoint Secret not found"); + .unwrap_or_else(|_| panic!("Endpoint Secret not found")); let channel = env .secret("CHANNEL_ID") .map(|e| { e.to_string() .parse::() - .expect("Cannot parse CHANNEL_ID") + .unwrap_or_else(|_| panic!("Cannot parse CHANNEL_ID")) }) - .expect("Announce Channel ID Secret not found"); + .unwrap_or_else(|_| panic!("Announce Channel ID Secret not found")); let bot_key = env .secret("BOT_APIKEY") .map(|e| e.to_string()) - .expect("Bot APIKEY Secret not found"); + .unwrap_or_else(|_| panic!("Bot APIKEY Secret not found")); let bot_channel = env .secret("BOT_CHANNEL_ID") .map(|e| { e.to_string() .parse::() - .expect("Cannot parse CHANNEL_ID") + .unwrap_or_else(|_| panic!("Cannot parse CHANNEL_ID")) }) - .expect("Remember Channel ID Secret not found"); + .unwrap_or_else(|_| panic!("Remember Channel ID Secret not found")); let roles = env .secret("ROLES") .map(|e| { e.to_string() .split_terminator(';') - .map(|r| r.parse::().expect(&format!("Cannot parse role '{r}'"))) + .map(|r| { + r.parse::() + .unwrap_or_else(|e| panic!("Cannot parse role '{r}': {e}")) + }) .collect::>() }) - .expect("Roles to mention Secret not found"); + .unwrap_or_else(|e| panic!("Roles to mention Secret not found: {e}")); let client = ClientBuilder::default() .build() - .expect("Cannot build client reqwest"); + .unwrap_or_else(|e| panic!("Cannot build client reqwest: {e}")); let now = OffsetDateTime::now_utc(); // 3 days, but 00:00UTC need +1 let next_days = now .checked_add(Duration::days(4)) - .expect("Cannot get the next days"); + .unwrap_or_else(|| panic!("Cannot get the next days")); // Get events let get = calendar::get( &client, &calendar_id, &calendar_api, - &now.format(&Rfc3339).expect("Cannot format min_date (now)"), + &now.format(&Rfc3339) + .unwrap_or_else(|_| panic!("Cannot format min_date (now)")), &next_days .format(&Rfc3339) - .expect("Cannot format max_date (next 3 days)"), + .unwrap_or_else(|_| panic!("Cannot format max_date (next 3 days)")), ) .await; @@ -116,9 +120,9 @@ pub async fn main(_e: ScheduledEvent, env: Env, _ctx: ScheduleContext) { pub fn compare_dates(event_date: &str, now: &OffsetDateTime) -> Option { let event_date = OffsetDateTime::parse(event_date, &Rfc3339) - .expect(&format!("Cannot parse date {event_date}")) + .unwrap_or_else(|e| panic!("Cannot parse date {e}")) .replace_minute(0) - .expect(&format!("Cannot replace minutes from event date")); + .unwrap_or_else(|_| panic!("Cannot replace minutes from event date")); let days = event_date.checked_sub(Duration::days(3)).unwrap(); let hours = event_date.checked_sub(Duration::hours(1)).unwrap();