use chrono::{DateTime, Local};
#[derive(Debug)]
pub struct CalTempsDateRange {
pub start: Option<DateTime<Local>>,
pub end: Option<DateTime<Local>>,
}
impl CalTempsDateRange {
pub fn new(range_str: String) -> Result<Self, Box<dyn std::error::Error>> {
let range_parts: Vec<&str> = range_str.splitn(2, "..").collect();
let start_str = range_parts[0];
let end_str = if range_parts.len() > 1 {
range_parts[1]
} else {
""
};
Ok(CalTempsDateRange {
start: if start_str.is_empty() {
None
} else {
Some(Self::read_in_date(start_str, false)?)
},
end: if range_parts.len() < 2 {
Some(Local::now())
} else if end_str.is_empty() {
None
} else {
Some(Self::read_in_date(end_str, true)?)
},
})
}
fn read_in_date(
dstr: &str,
_is_end: bool,
) -> Result<DateTime<Local>, Box<dyn std::error::Error>> {
match DateTime::parse_from_rfc3339(dstr) {
Ok(dt) => Ok(dt.into()),
Err(error) => Err(Box::new(error)),
}
}
}