2023-01-15 15:38:41 +01:00
|
|
|
use chrono::NaiveDate;
|
|
|
|
use std::vec::Vec;
|
|
|
|
use std::default::Default;
|
2023-02-11 11:43:24 +01:00
|
|
|
use std::string::String;
|
2023-01-15 15:38:41 +01:00
|
|
|
|
2023-06-11 09:00:48 +02:00
|
|
|
#[derive(Clone, Debug)]
|
2023-01-15 15:38:41 +01:00
|
|
|
pub struct Event {
|
2023-02-11 11:43:24 +01:00
|
|
|
pub text: String,
|
|
|
|
pub begin: NaiveDate,
|
|
|
|
pub end: NaiveDate,
|
2023-01-15 15:38:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Event {
|
2023-06-11 09:00:48 +02:00
|
|
|
pub fn is_in_day(&self, day: NaiveDate) -> bool {
|
2023-01-15 15:38:41 +01:00
|
|
|
day >= self.begin && day <= self.end
|
|
|
|
}
|
2023-06-11 09:00:48 +02:00
|
|
|
|
|
|
|
pub fn is_in_days(&self, first_day: NaiveDate, last_day: NaiveDate) -> bool {
|
|
|
|
let mut day = first_day;
|
|
|
|
while day <= last_day {
|
|
|
|
if self.is_in_day(day) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
day = day.succ_opt().unwrap();
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn span_days(&self) -> i64 {
|
|
|
|
(self.end - self.begin).num_days()
|
|
|
|
}
|
2023-01-15 15:38:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct EventsCollection {
|
|
|
|
events: Vec<Event>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EventsCollection {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
EventsCollection {
|
|
|
|
// TODO: hard-coded events
|
2023-07-09 18:49:42 +02:00
|
|
|
events: Vec::new(),
|
2023-01-15 15:38:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-09 18:49:42 +02:00
|
|
|
pub fn create(self: &mut Self, begin: NaiveDate, end: NaiveDate, text: &str) {
|
|
|
|
self.events.push(Event {text: text.to_string(), begin, end});
|
|
|
|
}
|
|
|
|
|
2023-02-11 11:43:24 +01:00
|
|
|
pub fn is_any_in_day(&self, day: NaiveDate) -> Option<Event> {
|
2023-01-15 15:38:41 +01:00
|
|
|
for ev_day in &self.events {
|
|
|
|
if ev_day.is_in_day(day) {
|
2023-02-11 11:43:24 +01:00
|
|
|
return Some(ev_day.clone());
|
2023-01-15 15:38:41 +01:00
|
|
|
}
|
|
|
|
}
|
2023-02-11 11:43:24 +01:00
|
|
|
None
|
2023-01-15 15:38:41 +01:00
|
|
|
}
|
2023-05-21 13:41:16 +02:00
|
|
|
|
|
|
|
pub fn for_day(&self, day: NaiveDate) -> Vec<Event> {
|
|
|
|
let mut events_in_day = Vec::new();
|
|
|
|
for ev_day in &self.events {
|
|
|
|
if ev_day.is_in_day(day) {
|
|
|
|
events_in_day.push(ev_day.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
events_in_day
|
|
|
|
}
|
2023-06-11 09:00:48 +02:00
|
|
|
|
|
|
|
pub fn starting_at(&self, day: NaiveDate) -> Vec<Event> {
|
|
|
|
let mut events_in_day = Vec::new();
|
|
|
|
for ev_day in &self.events {
|
|
|
|
if ev_day.begin == day {
|
|
|
|
events_in_day.push(ev_day.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
events_in_day
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn starting_before(&self, day: NaiveDate) -> Vec<Event> {
|
|
|
|
let mut events_in_day = Vec::new();
|
|
|
|
for ev_day in &self.events {
|
|
|
|
if ev_day.begin < day {
|
|
|
|
events_in_day.push(ev_day.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
events_in_day
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn within(&self, first_day: NaiveDate, last_day: NaiveDate) -> Vec<Event> {
|
|
|
|
let mut events_in_day = Vec::new();
|
|
|
|
for ev_day in &self.events {
|
|
|
|
if ev_day.is_in_days(first_day, last_day) {
|
|
|
|
events_in_day.push(ev_day.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
events_in_day
|
|
|
|
}
|
2023-07-09 20:31:18 +02:00
|
|
|
|
|
|
|
pub fn sort(&mut self) {
|
|
|
|
self.events.sort_by_key(|event| event.begin);
|
|
|
|
}
|
2023-01-15 15:38:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for EventsCollection {
|
|
|
|
fn default() -> EventsCollection {
|
|
|
|
EventsCollection::new()
|
|
|
|
}
|
|
|
|
}
|