add stuff

This commit is contained in:
Adam 2024-01-12 07:34:06 -05:00
parent 0194185cdc
commit 96714af9cb
5 changed files with 2357 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

1042
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

11
Cargo.toml Normal file
View file

@ -0,0 +1,11 @@
[package]
name = "calendar_db"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
icalendar = "0.15.8"
icu = "1.3.2"
sqlite = "0.31.1"

1193
holidays.ics Normal file

File diff suppressed because it is too large Load diff

110
src/main.rs Normal file
View file

@ -0,0 +1,110 @@
use std::collections::HashMap;
use std::fs;
use icu::calendar::types::IsoWeekday;
use icu::calendar::Date;
#[derive(Copy, Clone)]
struct DateStruct {
year: i32,
month: u8,
day: u8,
}
struct DayStruct {
datestring: String,
date: DateStruct,
day_of_week: IsoWeekday,
day_of_year: u16,
days_in_month: u8,
days_in_year: u16,
week_of_month: u32,
}
struct EventStruct {
datestring: String,
date: DateStruct,
summary: String,
}
fn holidaze() -> HashMap<String, String> {
let input = fs::read_to_string("holidays.ics").unwrap();
let unfolded = icalendar::parser::unfold(input.as_str());
let calendar = icalendar::parser::read_calendar(unfolded.as_str());
let mut holiday_map = HashMap::<String, String>::new();
for holiday in calendar.unwrap().components {
let date_data = holiday.properties[2].val.as_str().split_at(4);
let event_data = holiday.properties[4].val.as_str();
let date_struct = DateStruct {
year: date_data.0.parse::<i32>().unwrap(),
month: date_data.1.split_at(2).0.parse::<u8>().unwrap(),
day: date_data.1.split_at(2).1.parse::<u8>().unwrap(),
};
let date_iso = Date::try_new_iso_date(
date_struct.year.clone(),
date_struct.month.clone(),
date_struct.day.clone(),
)
.unwrap();
let day_struct = DayStruct {
date: date_struct.clone(),
datestring: format!(
"{:?}{:02}{:02}",
&date_struct.year, &date_struct.month, &date_struct.day,
),
day_of_week: date_iso.day_of_week(),
day_of_year: date_iso.day_of_year_info().day_of_year,
days_in_month: date_iso.days_in_month(),
days_in_year: date_iso.days_in_year(),
week_of_month: date_iso.week_of_month(IsoWeekday::Sunday).0,
};
let event_struct = EventStruct {
date: date_struct,
datestring: day_struct.datestring,
summary: event_data.replace("\\", ""),
};
holiday_map.insert(
event_struct.datestring.to_string(),
event_struct.summary.to_string(),
);
}
return holiday_map;
}
fn main() {
let holidays_map = holidaze();
let connection = sqlite::open(":memory:").unwrap();
connection.execute("CREATE TABLE calendar (datestring TEXT, summary TEXT)").unwrap();
for key in holidays_map.keys() {
let query = format!(
"
INSERT INTO calendar VALUES ({}, {:?});
",
key, holidays_map.get(key).unwrap(),
);
connection.execute(query).unwrap();
}
let query = "SELECT * FROM calendar where summary like '%birthday%'";
connection
.iterate(query, |pairs| {
for &(name, value) in pairs.iter() {
println!("{} = {}", name, value.unwrap());
}
true
})
.unwrap();
}