2019-10-06 23:28:39 +02:00
|
|
|
use std::collections::HashMap;
|
2021-05-24 23:16:44 +02:00
|
|
|
use std::str::FromStr;
|
2019-10-11 21:24:43 +02:00
|
|
|
use chrono::{NaiveDate, NaiveDateTime};
|
2019-10-06 23:28:39 +02:00
|
|
|
|
|
|
|
mod tokenizer;
|
|
|
|
mod parser;
|
|
|
|
pub use parser::Parser;
|
|
|
|
|
2019-10-10 17:39:34 +02:00
|
|
|
pub trait GetValue<'a, R: 'a> {
|
|
|
|
fn get(&'a self, key: &'_ str) -> Option<R>;
|
|
|
|
}
|
|
|
|
|
2019-10-07 00:12:31 +02:00
|
|
|
pub type Props = Vec<(String, String)>;
|
|
|
|
|
2019-10-06 23:28:39 +02:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub struct Object {
|
|
|
|
pub name: String,
|
2021-05-31 19:04:16 +02:00
|
|
|
pub content: HashMap<String, Vec<(Props, String)>>,
|
2019-10-10 18:27:39 +02:00
|
|
|
pub children: Vec<Box<Object>>,
|
2019-10-07 00:12:31 +02:00
|
|
|
}
|
|
|
|
|
2019-10-10 17:39:34 +02:00
|
|
|
impl<'a> GetValue<'a, &'a str> for Object {
|
|
|
|
fn get(&'a self, key: &'_ str) -> Option<&'a str> {
|
2019-10-07 00:12:31 +02:00
|
|
|
self.content.get(key)
|
2021-05-31 19:04:16 +02:00
|
|
|
.and_then(|pairs| pairs.first().map(|(_props, ref value)| value.as_str()))
|
2019-10-07 00:12:31 +02:00
|
|
|
}
|
2019-10-06 23:28:39 +02:00
|
|
|
}
|
2019-10-10 17:39:34 +02:00
|
|
|
|
|
|
|
impl<'a> GetValue<'a, String> for Object {
|
|
|
|
fn get(&self, key: &'_ str) -> Option<String> {
|
|
|
|
self.content.get(key)
|
2021-05-31 19:04:16 +02:00
|
|
|
.and_then(|pairs| pairs.first().map(|(_props, value)| value.clone()))
|
2019-10-10 17:39:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-10 18:27:39 +02:00
|
|
|
// TODO: TZID, offset
|
2019-10-11 21:43:24 +02:00
|
|
|
impl<'a> GetValue<'a, Timestamp> for Object {
|
|
|
|
fn get(&self, key: &'_ str) -> Option<Timestamp> {
|
2019-10-10 17:39:34 +02:00
|
|
|
let s = self.get(key)?;
|
2021-05-24 23:16:44 +02:00
|
|
|
Timestamp::from_str(s)
|
2019-10-11 21:24:43 +02:00
|
|
|
.map_err(|e| println!("Cannot parse date: {:?}", e))
|
2019-10-10 17:39:34 +02:00
|
|
|
.ok()
|
|
|
|
}
|
|
|
|
}
|
2019-10-11 21:43:24 +02:00
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum Timestamp {
|
|
|
|
Date(NaiveDate),
|
|
|
|
DateTime(NaiveDateTime),
|
|
|
|
}
|
|
|
|
|
2021-05-24 23:16:44 +02:00
|
|
|
impl FromStr for Timestamp {
|
|
|
|
type Err = String;
|
|
|
|
|
|
|
|
fn from_str(s: &'_ str) -> Result<Self, Self::Err> {
|
|
|
|
NaiveDateTime::parse_from_str(s, "%Y%m%dT%H%M%SZ")
|
|
|
|
.or_else(|_| NaiveDateTime::parse_from_str(s, "%Y%m%dT%H%M%S"))
|
|
|
|
.map(Timestamp::DateTime)
|
|
|
|
.or_else(|_|
|
|
|
|
NaiveDate::parse_from_str(s, "%Y%m%d")
|
|
|
|
.map(Timestamp::Date)
|
|
|
|
)
|
|
|
|
.map_err(|e| format!("Cannot parse date: {:?}", e))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 21:43:24 +02:00
|
|
|
impl Timestamp {
|
|
|
|
pub fn or_hms(self, hour: u32, min: u32, sec: u32) -> NaiveDateTime {
|
|
|
|
match self {
|
|
|
|
Timestamp::Date(date) => date.and_hms(hour, min, sec),
|
|
|
|
Timestamp::DateTime(date_time) => date_time,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|