Skip to content

Commit

Permalink
fix: clippy suggestion to use unwrap_or_else
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioRibera committed May 30, 2024
1 parent 5d74eb7 commit 0c062c7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/cangrebot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 !")
Expand Down
38 changes: 21 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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;
Expand Down Expand Up @@ -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::<i64>()
.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::<i64>()
.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::<i64>().expect(&format!("Cannot parse role '{r}'")))
.map(|r| {
r.parse::<i64>()
.unwrap_or_else(|e| panic!("Cannot parse role '{r}': {e}"))
})
.collect::<Vec<_>>()
})
.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;

Expand All @@ -116,9 +120,9 @@ pub async fn main(_e: ScheduledEvent, env: Env, _ctx: ScheduleContext) {

pub fn compare_dates(event_date: &str, now: &OffsetDateTime) -> Option<EventDateType> {
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();

Expand Down

0 comments on commit 0c062c7

Please sign in to comment.