From 83e4b72307fab1af1e0a9915cb21492c86f11705 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Fri, 29 Dec 2023 20:55:01 -0500 Subject: [PATCH] Implement timestamp. --- src/types/object.rs | 10 ++-- src/wasm/timestamp.rs | 123 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 123 insertions(+), 10 deletions(-) diff --git a/src/types/object.rs b/src/types/object.rs index b2f5f33..f8afa55 100644 --- a/src/types/object.rs +++ b/src/types/object.rs @@ -332,19 +332,19 @@ pub type HourInner = u8; pub type MinuteInner = u8; #[derive(Debug, Clone)] -pub struct Year(YearInner); +pub struct Year(pub YearInner); #[derive(Debug, Clone)] -pub struct Month(MonthInner); +pub struct Month(pub MonthInner); #[derive(Debug, Clone)] -pub struct DayOfMonth(DayOfMonthInner); +pub struct DayOfMonth(pub DayOfMonthInner); #[derive(Debug, Clone)] -pub struct Hour(HourInner); +pub struct Hour(pub HourInner); #[derive(Debug, Clone)] -pub struct Minute(MinuteInner); +pub struct Minute(pub MinuteInner); impl Year { // TODO: Make a real error type instead of a boxed any error. diff --git a/src/wasm/timestamp.rs b/src/wasm/timestamp.rs index 554ad8a..a6c376b 100644 --- a/src/wasm/timestamp.rs +++ b/src/wasm/timestamp.rs @@ -4,15 +4,61 @@ use serde::Serialize; use super::ast_node::WasmAstNode; use super::macros::to_wasm; use super::to_wasm::ToWasm; -use super::AdditionalProperties; use crate::compare::ElispFact; +use crate::types::DayOfMonthInner; +use crate::types::HourInner; +use crate::types::MinuteInner; +use crate::types::MonthInner; +use crate::types::RepeaterType; +use crate::types::RepeaterWarningDelayValueType; +use crate::types::TimeUnit; use crate::types::Timestamp; +use crate::types::TimestampRangeType; +use crate::types::TimestampType; +use crate::types::WarningDelayType; +use crate::types::YearInner; use crate::wasm::to_wasm::ToWasmStandardProperties; #[derive(Debug, Serialize, Deserialize)] pub struct WasmTimestamp { - #[serde(flatten)] - pub(crate) additional_properties: AdditionalProperties, + #[serde(rename = "type")] + pub(crate) timestamp_type: String, + #[serde(rename = "range-type")] + pub(crate) range_type: Option, + #[serde(rename = "raw-value")] + pub(crate) raw_value: String, + #[serde(rename = "year-start")] + pub(crate) year_start: Option, + #[serde(rename = "month-start")] + pub(crate) month_start: Option, + #[serde(rename = "day-start")] + pub(crate) day_of_month_start: Option, + #[serde(rename = "hour-start")] + pub(crate) hour_start: Option, + #[serde(rename = "minute-start")] + pub(crate) minute_start: Option, + #[serde(rename = "year-end")] + pub(crate) year_end: Option, + #[serde(rename = "month-end")] + pub(crate) month_end: Option, + #[serde(rename = "day-end")] + pub(crate) day_of_month_end: Option, + #[serde(rename = "hour-end")] + pub(crate) hour_end: Option, + #[serde(rename = "minute-end")] + pub(crate) minute_end: Option, + #[serde(rename = "repeater-type")] + pub(crate) repeater_type: Option, + #[serde(rename = "repeater-unit")] + pub(crate) repeater_unit: Option, + #[serde(rename = "repeater-value")] + pub(crate) repeater_value: Option, + #[serde(rename = "warning-type")] + pub(crate) warning_type: Option, + #[serde(rename = "warning-unit")] + pub(crate) warning_unit: Option, + #[serde(rename = "warning-value")] + pub(crate) warning_value: Option, } to_wasm!( @@ -21,12 +67,79 @@ to_wasm!( original, wasm_context, { WasmAstNode::Timestamp(original) }, - { "TODO".into() }, + { "timestamp".into() }, { Ok(( Vec::new(), WasmTimestamp { - additional_properties: AdditionalProperties::default(), + timestamp_type: match original.timestamp_type { + TimestampType::Diary => "diary", + TimestampType::Active => "active", + TimestampType::Inactive => "inactive", + TimestampType::ActiveRange => "active-range", + TimestampType::InactiveRange => "inactive-range", + } + .to_owned(), + range_type: match original.range_type { + TimestampRangeType::DateRange => Some("daterange".to_owned()), + TimestampRangeType::TimeRange => Some("timerange".to_owned()), + TimestampRangeType::None => None, + }, + raw_value: original.get_raw_value().to_owned(), + year_start: original.start.as_ref().map(|date| date.get_year().0), + month_start: original.start.as_ref().map(|date| date.get_month().0), + day_of_month_start: original + .start + .as_ref() + .map(|date| date.get_day_of_month().0), + hour_start: original.start_time.as_ref().map(|time| time.get_hour().0), + minute_start: original.start_time.as_ref().map(|time| time.get_minute().0), + year_end: original.end.as_ref().map(|date| date.get_year().0), + month_end: original.end.as_ref().map(|date| date.get_month().0), + day_of_month_end: original.end.as_ref().map(|date| date.get_day_of_month().0), + hour_end: original.end_time.as_ref().map(|time| time.get_hour().0), + minute_end: original.end_time.as_ref().map(|time| time.get_minute().0), + repeater_type: original + .repeater + .as_ref() + .map(|repeater| match repeater.repeater_type { + RepeaterType::Cumulative => "cumulate", + RepeaterType::CatchUp => "catch-up", + RepeaterType::Restart => "restart", + }) + .map(|text| text.to_owned()), + repeater_unit: original + .repeater + .as_ref() + .map(|repeater| match repeater.unit { + TimeUnit::Hour => "hour", + TimeUnit::Day => "day", + TimeUnit::Week => "week", + TimeUnit::Month => "month", + TimeUnit::Year => "year", + }) + .map(|text| text.to_owned()), + repeater_value: original.repeater.as_ref().map(|repeater| repeater.value), + warning_type: original + .warning_delay + .as_ref() + .map(|warning| match warning.warning_delay_type { + WarningDelayType::All => "all", + WarningDelayType::First => "first", + }) + .map(|text| text.to_owned()), + warning_unit: original + .warning_delay + .as_ref() + .map(|warning| match warning.unit { + TimeUnit::Hour => "hour", + TimeUnit::Day => "day", + TimeUnit::Week => "week", + TimeUnit::Month => "month", + TimeUnit::Year => "year", + }) + .map(|text| text.to_owned()), + warning_value: original.warning_delay.as_ref().map(|warning| warning.value), }, )) }