Created
July 21, 2024 01:48
-
-
Save xentec/dfa58c28a0018ca93ff944cb5e380d89 to your computer and use it in GitHub Desktop.
Easily generate ICS calendars from an event list in CSV format.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::io::Write; | |
use icalendar::{self as ical, Component, DatePerhapsTime, EventLike}; | |
use csv; | |
use chrono::{DateTime, Utc}; | |
use serde::Deserialize; | |
#[derive(Deserialize)] | |
struct Event { | |
summary: String, | |
description: Option<String>, | |
start: DateTime<Utc>, | |
end: DateTime<Utc>, | |
location: Option<String>, | |
} | |
fn main() -> Result<(), Box<dyn std::error::Error>> { | |
std::io::stdout() | |
.write_all( | |
csv::ReaderBuilder::new() | |
.from_reader(std::io::stdin()) | |
.deserialize() | |
.filter_map(|e| | |
e.inspect_err(|err: &csv::Error| eprintln!("failed to parse line: {err}")) | |
.ok() | |
) | |
.map(|rec: Event| { | |
let mut ev = ical::Event::new(); | |
ev.summary(&rec.summary); | |
if let Some(sval) = rec.description { | |
ev.description(&sval); | |
} | |
ev.starts(DatePerhapsTime::from(rec.start)); | |
ev.ends(DatePerhapsTime::from(rec.end)); | |
if let Some(sval) = rec.location { | |
ev.location(&sval); | |
} | |
ev | |
}) | |
.collect::<ical::Calendar>() | |
.to_string() | |
.as_bytes() | |
) | |
.map_err(|err| format!("failed to convert to ics: {err}").into()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment