136 lines
3.7 KiB
Rust
136 lines
3.7 KiB
Rust
use chrono::NaiveDate;
|
|
use std::vec::Vec;
|
|
use std::default::Default;
|
|
use std::string::String;
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct Event {
|
|
pub text: String,
|
|
pub begin: NaiveDate,
|
|
pub end: NaiveDate,
|
|
}
|
|
|
|
//impl Clone for Event {
|
|
// fn clone(&self) -> Self {
|
|
// Event {
|
|
// text: self.text.clone(),
|
|
// begin: self.begin,
|
|
// end: self.end,
|
|
// }
|
|
// }
|
|
//}
|
|
//
|
|
impl Event {
|
|
pub fn is_in_day(&self, day: NaiveDate) -> bool {
|
|
day >= self.begin && day <= self.end
|
|
}
|
|
|
|
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()
|
|
}
|
|
}
|
|
|
|
pub struct EventsCollection {
|
|
events: Vec<Event>,
|
|
}
|
|
|
|
impl EventsCollection {
|
|
pub fn new() -> Self {
|
|
EventsCollection {
|
|
// TODO: hard-coded events
|
|
events: vec![
|
|
Event {
|
|
text: String::from("pentecoste"),
|
|
begin: NaiveDate::from_ymd_opt(2023, 05, 30).unwrap(),
|
|
end: NaiveDate::from_ymd_opt(2023, 06, 09).unwrap(),
|
|
},
|
|
Event {
|
|
text: String::from("estate"),
|
|
begin: NaiveDate::from_ymd_opt(2023, 07, 31).unwrap(),
|
|
end: NaiveDate::from_ymd_opt(2023, 09, 11).unwrap(),
|
|
},
|
|
Event {
|
|
text: String::from("event one"),
|
|
begin: NaiveDate::from_ymd_opt(2023, 07, 31).unwrap(),
|
|
end: NaiveDate::from_ymd_opt(2023, 08, 03).unwrap(),
|
|
},
|
|
Event {
|
|
text: String::from("event two"),
|
|
begin: NaiveDate::from_ymd_opt(2023, 08, 02).unwrap(),
|
|
end: NaiveDate::from_ymd_opt(2023, 08, 04).unwrap(),
|
|
},
|
|
Event {
|
|
text: String::from("event three"),
|
|
begin: NaiveDate::from_ymd_opt(2023, 08, 05).unwrap(),
|
|
end: NaiveDate::from_ymd_opt(2023, 08, 06).unwrap(),
|
|
},
|
|
],
|
|
}
|
|
}
|
|
|
|
pub fn is_any_in_day(&self, day: NaiveDate) -> Option<Event> {
|
|
for ev_day in &self.events {
|
|
if ev_day.is_in_day(day) {
|
|
return Some(ev_day.clone());
|
|
}
|
|
}
|
|
None
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
impl Default for EventsCollection {
|
|
fn default() -> EventsCollection {
|
|
EventsCollection::new()
|
|
}
|
|
} |