rs-calendar/src/model/events.rs

66 lines
1.5 KiB
Rust

use chrono::NaiveDate;
use std::vec::Vec;
use std::default::Default;
use std::string::String;
#[derive(Clone)]
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 {
fn is_in_day(&self, day: NaiveDate) -> bool {
day >= self.begin && day <= self.end
}
}
pub struct EventsCollection {
events: Vec<Event>,
}
impl EventsCollection {
pub fn new() -> Self {
EventsCollection {
// TODO: hard-coded events
events: vec![
Event {
text: String::from("ev_1"),
begin: NaiveDate::from_ymd_opt(2023, 01, 15).unwrap(),
end: NaiveDate::from_ymd_opt(2023, 01, 16).unwrap(),
},
Event {
text: String::from("ev_2"),
begin: NaiveDate::from_ymd_opt(2023, 01, 16).unwrap(),
end: NaiveDate::from_ymd_opt(2023, 01, 17).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
}
}
impl Default for EventsCollection {
fn default() -> EventsCollection {
EventsCollection::new()
}
}