feat: reminder (#3374)

This commit is contained in:
Mathias Mogensen
2023-10-02 09:12:24 +02:00
committed by GitHub
parent f7749bdccc
commit 4a433a3176
99 changed files with 4599 additions and 998 deletions

View File

@ -0,0 +1,25 @@
[package]
name = "flowy-date"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
lib-dispatch = { path = "../lib-dispatch" }
flowy-error = { path = "../flowy-error" }
flowy-derive = { path = "../../../shared-lib/flowy-derive" }
protobuf = { version = "2.28.0" }
bytes = { version = "1.4" }
strum_macros = "0.21"
tracing = { version = "0.1" }
date_time_parser = { version = "0.2.0" }
chrono = { version = "0.4.26" }
fancy-regex = { version = "0.11.0" }
[features]
dart = ["flowy-codegen/dart"]
ts = ["flowy-codegen/ts"]
[build-dependencies]
flowy-codegen = { path = "../../../shared-lib/flowy-codegen" }

View File

@ -0,0 +1,3 @@
# Check out the FlowyConfig (located in flowy_toml.rs) for more details.
proto_input = ["src/event_map.rs", "src/entities.rs"]
event_files = ["src/event_map.rs"]

View File

@ -0,0 +1,10 @@
fn main() {
let crate_name = env!("CARGO_PKG_NAME");
flowy_codegen::protobuf_file::gen(crate_name);
#[cfg(feature = "dart")]
flowy_codegen::dart_event::gen(crate_name);
#[cfg(feature = "ts")]
flowy_codegen::ts_event::gen(crate_name);
}

View File

@ -0,0 +1,13 @@
use flowy_derive::ProtoBuf;
#[derive(ProtoBuf, Debug, Default, Clone)]
pub struct DateQueryPB {
#[pb(index = 1)]
pub query: String,
}
#[derive(ProtoBuf, Debug, Default, Clone)]
pub struct DateResultPB {
#[pb(index = 1)]
pub date: String,
}

View File

@ -0,0 +1,36 @@
use chrono::{Datelike, NaiveDate};
use date_time_parser::DateParser;
use fancy_regex::Regex;
use flowy_error::FlowyError;
use lib_dispatch::prelude::{data_result_ok, AFPluginData, DataResult};
use std::sync::OnceLock;
use crate::entities::*;
static YEAR_REGEX: OnceLock<Regex> = OnceLock::new();
fn year_regex() -> &'static Regex {
YEAR_REGEX.get_or_init(|| Regex::new(r"\b\d{4}\b").unwrap())
}
#[tracing::instrument(level = "debug", skip_all, err)]
pub(crate) async fn query_date_handler(
data: AFPluginData<DateQueryPB>,
) -> DataResult<DateResultPB, FlowyError> {
let query: String = data.into_inner().query;
let date = DateParser::parse(&query);
match date {
Some(naive_date) => {
let year_match = year_regex().find(&query).unwrap();
let formatted = year_match
.and_then(|capture| capture.as_str().parse::<i32>().ok())
.and_then(|year| NaiveDate::from_ymd_opt(year, naive_date.month0(), naive_date.day0()))
.map(|date| date.to_string())
.unwrap_or_else(|| naive_date.to_string());
data_result_ok(DateResultPB { date: formatted })
},
None => Err(FlowyError::internal().with_context("Failed to parse date from")),
}
}

View File

@ -0,0 +1,19 @@
use strum_macros::Display;
use flowy_derive::{Flowy_Event, ProtoBuf_Enum};
use lib_dispatch::prelude::AFPlugin;
use crate::event_handler::query_date_handler;
pub fn init() -> AFPlugin {
AFPlugin::new()
.name(env!("CARGO_PKG_NAME"))
.event(DateEvent::QueryDate, query_date_handler)
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Display, ProtoBuf_Enum, Flowy_Event)]
#[event_err = "FlowyError"]
pub enum DateEvent {
#[event(input = "DateQueryPB", output = "DateResultPB")]
QueryDate = 0,
}

View File

@ -0,0 +1,4 @@
pub mod entities;
pub mod event_handler;
pub mod event_map;
pub mod protobuf;