Batman
This commit is contained in:
@@ -0,0 +1 @@
|
||||
/target
|
||||
Generated
+1607
File diff suppressed because it is too large
Load Diff
+10
@@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "days-to-bh"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
reqwest = { version = "0.11", features = ["json", "blocking"] }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
chrono = { version = "0.4", features = ["serde"] }
|
||||
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
use reqwest;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use chrono::{Datelike, Local, NaiveDate};
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
struct Event {
|
||||
title: String,
|
||||
date: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
struct HolidaysData {
|
||||
events: Vec<Event>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
struct EnglandAndWalesData {
|
||||
#[serde(rename = "england-and-wales")]
|
||||
england_and_wales: HolidaysData,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct NextHoliday {
|
||||
title: String,
|
||||
date: NaiveDate,
|
||||
days_until: i32,
|
||||
}
|
||||
|
||||
impl NextHoliday {
|
||||
fn new(title: String, date: NaiveDate, days_until: i32) -> Self {
|
||||
NextHoliday {
|
||||
title,
|
||||
date,
|
||||
days_until,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_uk_bank_holidays() -> Result<EnglandAndWalesData, Box<dyn std::error::Error>> {
|
||||
let url = "https://www.gov.uk/bank-holidays.json";
|
||||
|
||||
let client = reqwest::blocking::Client::new();
|
||||
let response = client.get(url).send()?;
|
||||
let data: EnglandAndWalesData = response.json()?;
|
||||
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
fn get_next_bank_holiday(holidays_data: &EnglandAndWalesData) -> Option<NextHoliday> {
|
||||
let today = Local::today().naive_local();
|
||||
let mut next_holiday: Option<NextHoliday> = None;
|
||||
let mut min_diff: Option<i32> = None;
|
||||
|
||||
if let Some(_events) = holidays_data.england_and_wales.events.get(0) {
|
||||
// Process events to find the next holiday
|
||||
for event in &holidays_data.england_and_wales.events {
|
||||
let holiday_date = match NaiveDate::parse_from_str(&event.date, "%Y-%m-%d") {
|
||||
Ok(date) => date,
|
||||
Err(_) => continue,
|
||||
};
|
||||
|
||||
if holiday_date < today {
|
||||
continue;
|
||||
}
|
||||
|
||||
let diff = (holiday_date - today).num_days() as i32;
|
||||
|
||||
if min_diff.is_none() || diff < min_diff.unwrap() {
|
||||
min_diff = Some(diff);
|
||||
next_holiday = Some(NextHoliday::new(
|
||||
event.title.clone(),
|
||||
holiday_date,
|
||||
diff,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
next_holiday
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
println!("Fetching UK Bank Holidays...");
|
||||
|
||||
let holidays_data = get_uk_bank_holidays()?;
|
||||
|
||||
if let Some(next_holiday) = get_next_bank_holiday(&holidays_data) {
|
||||
println!("\nNext Bank Holiday:");
|
||||
println!("Title: {}", next_holiday.title);
|
||||
println!("Date: {}/{}/{}",
|
||||
next_holiday.date.day(),
|
||||
next_holiday.date.month(),
|
||||
next_holiday.date.year());
|
||||
println!("Days until: {}", next_holiday.days_until);
|
||||
|
||||
if next_holiday.days_until == 0 {
|
||||
println!("Today is a bank holiday! 🎉");
|
||||
} else if next_holiday.days_until == 1 {
|
||||
println!("Tomorrow is a bank holiday!");
|
||||
}
|
||||
} else {
|
||||
println!("No upcoming bank holidays found");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user