cal7tor/src/main.rs

67 lines
1.7 KiB
Rust
Raw Normal View History

2022-08-17 14:56:36 +02:00
use clap::Parser;
2022-08-17 12:21:56 +02:00
use regex::Regex;
2022-08-16 23:40:42 +02:00
2022-08-16 10:32:46 +02:00
mod ics;
mod info;
2022-08-15 14:52:57 +02:00
mod timetable;
2022-08-16 15:48:13 +02:00
mod utils;
2022-08-12 21:20:39 +02:00
2022-08-16 23:40:42 +02:00
#[derive(Parser)]
#[clap(version, about, long_about = None)]
struct Args {
2022-08-17 14:56:36 +02:00
/// The class you want to get the timetable, i.e.: L2-A
2022-08-17 12:21:56 +02:00
#[clap(value_parser)]
class: String,
2022-08-16 23:40:42 +02:00
2022-08-17 14:56:36 +02:00
/// The semester you want (useful only in 3rd year, 1-2 use letter in class)
#[clap(short, long, value_parser, value_name = "SEMESTER NUMBER")]
2022-08-17 14:09:08 +02:00
semester: Option<i8>,
2022-08-17 12:21:56 +02:00
/// Export to iCalendar format (.ics)
2022-08-17 14:56:36 +02:00
#[clap(short, long, value_name = "FILE NAME")]
2022-08-17 14:38:31 +02:00
export: Option<String>,
2022-08-16 23:40:42 +02:00
}
2022-08-12 19:55:28 +02:00
#[tokio::main]
2022-08-14 12:44:36 +02:00
async fn main() {
2022-08-16 23:40:42 +02:00
let args = Args::parse();
2022-08-17 16:58:49 +02:00
let matches = Regex::new(r"[Ll](?P<year>\d)[-–•·]?(?P<letter>.)?")
2022-08-17 12:21:56 +02:00
.unwrap()
.captures(&args.class)
.unwrap();
let year = matches
.name("year")
.unwrap()
.as_str()
.parse::<i8>()
.unwrap();
let letter = matches
.name("letter")
.map(|c| c.as_str().chars().next().expect("Error in letter"));
println!(
"Fetch the timetable for L{}{}...",
year,
letter.unwrap_or_default()
);
2022-08-17 14:09:08 +02:00
let timetable = timetable::timetable(year, args.semester, letter).await;
2022-08-12 21:20:39 +02:00
2022-08-16 15:48:13 +02:00
println!("Fetch informations about the year...");
2022-08-15 19:20:10 +02:00
let info = info::info().await;
2022-08-17 14:38:31 +02:00
if args.export.is_some() {
// Export the calendar
2022-08-17 14:56:36 +02:00
let filename = args.export.unwrap();
println!("Build the ICS file at {}...", filename);
let builded_timetable = timetable::build(timetable, info);
ics::export(builded_timetable, filename);
2022-08-17 14:38:31 +02:00
} else {
// Show the calendar
println!("Displaying...");
timetable::display();
2022-08-17 14:38:31 +02:00
}
2022-08-14 12:44:36 +02:00
}