From c2c83ff66f3ccfff37177a81a2e126a3ef951321 Mon Sep 17 00:00:00 2001 From: Ash Vollmer Date: Wed, 5 Aug 2026 21:16:16 +0200 Subject: [PATCH 1/2] refactored into impl and added get validity function --- src/models.rs | 61 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 47 insertions(+), 14 deletions(-) diff --git a/src/models.rs b/src/models.rs index 389cfb8..a80fcf1 100644 --- a/src/models.rs +++ b/src/models.rs @@ -1,29 +1,62 @@ +use chrono::NaiveDateTime; +use regex::Regex; + pub struct Notice { pub affected_lines: Vec, pub dest_1: String, pub dest_2: String, pub headline: String, pub detail: String, - pub from: u32, - pub to: u32, + pub from: Option, + pub to: Option, pub last_modified: String, } -pub fn get_line_color(line_number: &str) -> (u8, u8, u8) { - match line_number { - // Rot: Linien 1, 2, 8 - "1" | "2" | "8" => (238, 66, 36), +impl Notice { + pub fn get_line_color(&self) -> (u8, u8, u8) { + match self + .affected_lines + .first() + .expect("Does not affect any line") + .as_str() + { + // Rot: Linien 1, 2, 8 + "1" | "2" | "8" => (238, 66, 36), - // Gelb (Die besten Linien): Linien 4, 5, 6, 11 - "4" | "5" | "6" | "11" => (248, 166, 28), + // Gelb (Die besten Linien): Linien 4, 5, 6, 11 + "4" | "5" | "6" | "11" => (248, 166, 28), - // Grün: Linien 9, 10, 12, 17 - "10" | "12" | "17" => (110, 191, 69), + // Grün: Linien 9, 10, 12, 17 + "10" | "12" | "17" => (110, 191, 69), - // Blau: Linien 3, 7, 9, 13 - "3" | "7" | "9" | "13" => (0, 114, 188), + // Blau: Linien 3, 7, 9, 13 + "3" | "7" | "9" | "13" => (0, 114, 188), - // Fallback (z.B. für das Standard-Blau aus deiner API) - _ => (55, 101, 174), + // Fallback (z.B. für das Standard-Blau aus deiner API) + _ => (55, 101, 174), + } + } + + pub fn get_validity_period(&mut self) { + let re = Regex::new( + r"Gültig ab (\d{2}\.\d{2}\.\d{4} \d{2}:\d{2}) bis (\d{2}\.\d{2}\.\d{4} \d{2}:\d{2})", + ) + .unwrap(); + + if let Some(caps) = re.captures(&self.detail) { + let valid_from = caps.get(1).unwrap().as_str(); + let valid_to = caps.get(1).unwrap().as_str(); + let format = "%d.%m.%Y %H:%M"; + + if let (Ok(start_dt), Ok(end_dt)) = ( + NaiveDateTime::parse_from_str(valid_from, format), + NaiveDateTime::parse_from_str(valid_to, format), + ) { + self.from = Some(start_dt); + self.to = Some(end_dt); + } + } else { + println!("No validity period found in the text."); + } } } From bb745c9595f502c5ecc86a2faeee26a4864056d2 Mon Sep 17 00:00:00 2001 From: Ash Vollmer Date: Wed, 5 Aug 2026 21:16:36 +0200 Subject: [PATCH 2/2] adjusted for changes in model --- src/main.rs | 61 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/src/main.rs b/src/main.rs index 259098b..3e0868a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,27 +6,25 @@ use dotenv::dotenv; use models::Notice; use serenity::{ all::{ - Colour, Context, CreateAttachment, CreateEmbed, CreateEmbedFooter, CreateMessage, - EventHandler, GatewayIntents, Message, Ready, Timestamp, + Colour, Context, CreateEmbed, CreateEmbedFooter, CreateMessage, EventHandler, + GatewayIntents, Message, Ready, Timestamp, }, async_trait, Client, }; -use crate::models::get_line_color; - struct Handler; #[async_trait] impl EventHandler for Handler { async fn message(&self, ctx: Context, msg: Message) { - let new = Notice { + let mut new = Notice { affected_lines: vec!(String::from("10")), dest_1: String::from("Hannover/Wettbergen"), dest_2: String::from("Hannover/Misburg"), headline: String::from("Linie 7 fährt erst bzw. bis Haltestelle In den Sieben Stücken"), detail: String::from("Die Stadtbahnlinie 7 fährt während der Baumaßnahme erst ab beziehungsweise bis zur Haltestelle In den Sieben Stücken. Dort besteht die Umsteigemöglichkeit vom bzw. in den Schienenersatzverkehr (SEV) und die Stadtbahnlinien 3 und 13. An der Haltestelle Buchholz/Betriebshof ist kein Zustieg bzw. Ausstieg möglich."), - from: 12, - to: 30, + from: None, + to: None, last_modified: String::from("2026-07-24T13:37:00Z"), }; if msg.content == "!hello" { @@ -42,24 +40,37 @@ impl EventHandler for Handler { )); let timestamp: Timestamp = new.last_modified.parse().expect("Invalid timestamp!"); - let color = - get_line_color(new.affected_lines.first().expect("No single line affected")); - - let embed = CreateEmbed::new() - .title(new.headline) - .description(new.detail) - .color(Colour::from_rgb(color.0, color.1, color.2)) - .timestamp(timestamp) - .fields(vec![ - ("Start", new.from.to_string(), true), - ("Ende", new.to.to_string(), true), - ]) - .field( - "Fahrtrichtungen", - format!("{} & {}", new.dest_1, new.dest_2), - false, - ) - .footer(footer); + let color = new.get_line_color(); + new.get_validity_period(); + let embed = if new.from.is_none() || new.to.is_none() { + CreateEmbed::new() + .title(new.headline) + .description(new.detail) + .color(Colour::from_rgb(color.0, color.1, color.2)) + .timestamp(timestamp) + .field( + "Fahrtrichtungen", + format!("{} & {}", &new.dest_1, &new.dest_2), + false, + ) + .footer(footer) + } else { + CreateEmbed::new() + .title(new.headline) + .description(new.detail) + .color(Colour::from_rgb(color.0, color.1, color.2)) + .timestamp(timestamp) + .fields(vec![ + ("Start", new.from.unwrap().to_string(), true), + ("Ende", new.to.unwrap().to_string(), true), + ]) + .field( + "Fahrtrichtungen", + format!("{} & {}", new.dest_1, new.dest_2), + false, + ) + .footer(footer) + }; // let embed = CreateEmbed::new() // .title("This is a title")