improve dates & borrowing strings
This commit is contained in:
parent
45f5eeee57
commit
de57e2c316
|
@ -102,7 +102,7 @@ pub fn interactive_check(config: KV) -> Result<(), Box<dyn Error>> {
|
||||||
if !todo.is_empty() { all_valid = false; }
|
if !todo.is_empty() { all_valid = false; }
|
||||||
break;
|
break;
|
||||||
},
|
},
|
||||||
_ => { return Err("Wat.".into()); }, // shouldn't be able to happen (prompt already checks)
|
_ => { unreachable!(); }, // (prompt already checks)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
87
src/main.rs
87
src/main.rs
|
@ -36,7 +36,7 @@ use std::io::prelude::*;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
// use std::future::Future;
|
// use std::future::Future;
|
||||||
|
|
||||||
use chrono::{Datelike, Local, NaiveDate, Weekday};
|
use chrono::{Datelike, Local, Months, NaiveDate, Weekday};
|
||||||
use clap::{Arg, Command};
|
use clap::{Arg, Command};
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use reqwest::Client;
|
use reqwest::Client;
|
||||||
|
@ -113,43 +113,42 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
// config
|
// config
|
||||||
let hedgedoc_server = &config["hedgedoc-server-url"];
|
let hedgedoc_server = &config["hedgedoc-server-url"];
|
||||||
println!("[START]\nAktueller Zustand der DB:");
|
println!("[START]\nAktueller Zustand der DB:");
|
||||||
config.dump_redacting(&["email-pass","matrix-pass"]).ok();
|
config.dump_redacting(&["email-password","wiki-password","matrix-password"]).ok();
|
||||||
|
|
||||||
// Dienstage diesen Monat
|
// Dienstage diesen Monat
|
||||||
let all_tuesdays: Vec<Option<NaiveDate>> = get_tuesdays(0);
|
let all_tuesdays: Vec<NaiveDate> = get_tuesdays(0);
|
||||||
let zweiter_dienstag: String = all_tuesdays[1].unwrap().to_string(); // z.B. 2024-07-09
|
let zweiter_dienstag: String = all_tuesdays[1].to_string(); // z.B. 2024-07-09
|
||||||
let vierter_dienstag: String = all_tuesdays[3].unwrap().to_string(); // z.B. 2024-07-23
|
let vierter_dienstag: String = all_tuesdays[3].to_string(); // z.B. 2024-07-23
|
||||||
|
|
||||||
//Dienstage des nächsten Monats definieren
|
//Dienstage des nächsten Monats definieren
|
||||||
let all_tuesdays_next_month: Vec<Option<NaiveDate>> = get_tuesdays(1);
|
let all_tuesdays_next_month: Vec<NaiveDate> = get_tuesdays(1);
|
||||||
let zweiter_dienstag_nächster_monat: String = all_tuesdays_next_month[1].unwrap().to_string();
|
let zweiter_dienstag_nächster_monat: String = all_tuesdays_next_month[1].to_string();
|
||||||
let vierter_dienstag_nächster_monat: String = all_tuesdays_next_month[3].unwrap().to_string();
|
let vierter_dienstag_nächster_monat: String = all_tuesdays_next_month[3].to_string();
|
||||||
|
|
||||||
// Daten, die später benutzt werden, definieren
|
// Daten, die später benutzt werden, definieren
|
||||||
let today= Local::now();
|
let today= Local::now();
|
||||||
let today_simple = NaiveDate::from_ymd_opt(today.year(), today.month(), today.day()).unwrap();
|
let today_simple = today.date_naive();
|
||||||
let in_1_day: NaiveDate = NaiveDate::from_ymd_opt(today.year(), today.month(), today.day()).unwrap().succ_opt().unwrap();
|
let yesterday = today_simple.pred_opt().unwrap();
|
||||||
let in_2_days: NaiveDate = in_1_day.succ_opt().unwrap();
|
let in_1_day = today_simple.succ_opt().unwrap();
|
||||||
let in_3_days: NaiveDate = in_2_days.succ_opt().unwrap();
|
let in_2_days = in_1_day.succ_opt().unwrap();
|
||||||
let yesterday: NaiveDate = NaiveDate::from_ymd_opt(today.year(), today.month(), today.day()).unwrap().pred_opt().unwrap();
|
let in_3_days = in_2_days.succ_opt().unwrap();
|
||||||
|
|
||||||
|
|
||||||
// Nächste Plena nachschauen:
|
// Nächste Plena nachschauen:
|
||||||
|
|
||||||
let nächster_plenumtermin: String = if all_tuesdays[1].unwrap() >= today_simple.pred_opt().unwrap() { // Für das Pad rumschicken am nächsten Tag wird das Datum einen Tag nach Hinten gesetzt,
|
let nächster_plenumtermin: &String = if all_tuesdays[1] >= yesterday { // Für das Pad rumschicken am nächsten Tag wird das Datum einen Tag nach Hinten gesetzt,
|
||||||
zweiter_dienstag.clone()
|
&zweiter_dienstag
|
||||||
} else {
|
} else {
|
||||||
vierter_dienstag.clone()
|
&vierter_dienstag
|
||||||
};
|
};
|
||||||
let übernächster_plenumtermin = if all_tuesdays[1].unwrap() >= today_simple.pred_opt().unwrap() { // hier das Gleiche.
|
let übernächster_plenumtermin = if all_tuesdays[1] >= yesterday { // hier das Gleiche.
|
||||||
vierter_dienstag.clone()
|
&vierter_dienstag
|
||||||
} else {
|
} else {
|
||||||
zweiter_dienstag_nächster_monat.clone()
|
&zweiter_dienstag_nächster_monat
|
||||||
};
|
};
|
||||||
let überübernächster_plenumtermin = if all_tuesdays[1].unwrap() >= today_simple.pred_opt().unwrap() { // hier das Gleiche.
|
let überübernächster_plenumtermin = if all_tuesdays[1] >= yesterday { // hier das Gleiche.
|
||||||
zweiter_dienstag_nächster_monat.clone()
|
&zweiter_dienstag_nächster_monat
|
||||||
} else {
|
} else {
|
||||||
vierter_dienstag_nächster_monat
|
&vierter_dienstag_nächster_monat
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -244,29 +243,25 @@ async fn download_and_return_pad(pad_link: String) -> Result<String, Box<dyn std
|
||||||
Ok(pad_content)
|
Ok(pad_content)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_tuesdays(month_offset: u32) -> Vec<Option<NaiveDate>>{
|
fn get_tuesdays(month_offset: i32) -> Vec<NaiveDate>{
|
||||||
// Jeden Donnerstag im Monat finden:
|
let date = Local::now();
|
||||||
let today = Local::now(); // jetzige Zeit abrufen (z.B. 2024-07-17 15:36:31.660997700 +02:00)
|
let date = match month_offset.signum() {
|
||||||
let current_month = today.month(); // diesen Monat als Zahl (z.B. 7)
|
0 => Some(date),
|
||||||
let current_year = today.year(); // dieses Jahr als Zahl (z.B. 2024)
|
1 => date.checked_add_months(Months::new(month_offset as u32)),
|
||||||
let first_day: Option<NaiveDate> = NaiveDate::from_ymd_opt(current_year, current_month+month_offset, 1); // Erster Tag des Monats (z.B. 2024-07-01)
|
-1 => date.checked_sub_months(Months::new((-month_offset) as u32)),
|
||||||
let last_day: Option<NaiveDate> = NaiveDate::from_ymd_opt(current_year, current_month+1+month_offset, 1).unwrap().pred_opt(); //Letzter Tag des Monats(z.B. 2024-07-31)
|
_ => unreachable!(),
|
||||||
// - Vektor mit allen Tagen im Monat erstellen
|
}.expect("(very) invalid month offset");
|
||||||
let mut days_in_month_vec: Vec<Option<NaiveDate>> = Vec::new();
|
let year = date.year();
|
||||||
let mut all_tuesdays: Vec<Option<NaiveDate>> = Vec::new();
|
let month = date.month();
|
||||||
let mut current_day: Option<NaiveDate> = first_day;
|
let mut current_date = NaiveDate::from_ymd_opt(year, month, 1);
|
||||||
while current_day <= last_day {
|
let mut dates = Vec::new();
|
||||||
days_in_month_vec.push(current_day);
|
while let Some(date) = current_date {
|
||||||
current_day = current_day.unwrap().succ_opt()
|
if date.month() == month && date.weekday() == Weekday::Tue {
|
||||||
|
dates.push(date);
|
||||||
}
|
}
|
||||||
// - Alle bis auf Donnerstage raus
|
current_date = date.succ_opt();
|
||||||
for element in days_in_month_vec {
|
|
||||||
match element.unwrap().weekday() {
|
|
||||||
Weekday::Tue => all_tuesdays.push(element),
|
|
||||||
_ => {} // Kein Dienstag,
|
|
||||||
}
|
}
|
||||||
}
|
dates
|
||||||
all_tuesdays
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_if_plenum(infrage_kommendes_plenum: String, date_to_check: NaiveDate) -> bool {
|
fn check_if_plenum(infrage_kommendes_plenum: String, date_to_check: NaiveDate) -> bool {
|
||||||
|
@ -329,7 +324,7 @@ fn mail_versenden(inhalt: String, betreff: String) -> std::result::Result<String
|
||||||
Ok(message_id)
|
Ok(message_id)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn generate_new_pad_for_following_date(übernächster_plenumtermin: String, überübernächster_plenumtermin: String, kv: &KV) -> Result<(), Box<dyn Error>> {
|
async fn generate_new_pad_for_following_date(übernächster_plenumtermin: &String, überübernächster_plenumtermin: &String, kv: &KV) -> Result<(), Box<dyn Error>> {
|
||||||
let client = Client::new();
|
let client = Client::new();
|
||||||
|
|
||||||
match create_new_pads::create_pad(&client, HEDGEDOC_SERVER_URL).await {
|
match create_new_pads::create_pad(&client, HEDGEDOC_SERVER_URL).await {
|
||||||
|
@ -355,7 +350,7 @@ async fn generate_new_pad_for_following_date(übernächster_plenumtermin: String
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn replace_placeholders(template: &str, übernächster_plenumtermin: String, überübernächster_plenumtermin: String) -> Result<String, Box<dyn Error>> {
|
fn replace_placeholders(template: &str, übernächster_plenumtermin: &str, überübernächster_plenumtermin: &str) -> Result<String, Box<dyn Error>> {
|
||||||
let re_datum = Regex::new(r"\{\{Datum\}\}")?;
|
let re_datum = Regex::new(r"\{\{Datum\}\}")?;
|
||||||
let result = re_datum.replace_all(template, übernächster_plenumtermin);
|
let result = re_datum.replace_all(template, übernächster_plenumtermin);
|
||||||
let re_naechstes_plenum = Regex::new(r"\{\{naechstes-plenum\}\}")?;
|
let re_naechstes_plenum = Regex::new(r"\{\{naechstes-plenum\}\}")?;
|
||||||
|
|
Loading…
Reference in a new issue