mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
feat: reminder (#3374)
This commit is contained in:
25
frontend/rust-lib/flowy-date/Cargo.toml
Normal file
25
frontend/rust-lib/flowy-date/Cargo.toml
Normal 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" }
|
3
frontend/rust-lib/flowy-date/Flowy.toml
Normal file
3
frontend/rust-lib/flowy-date/Flowy.toml
Normal 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"]
|
10
frontend/rust-lib/flowy-date/build.rs
Normal file
10
frontend/rust-lib/flowy-date/build.rs
Normal 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);
|
||||
}
|
13
frontend/rust-lib/flowy-date/src/entities.rs
Normal file
13
frontend/rust-lib/flowy-date/src/entities.rs
Normal 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,
|
||||
}
|
36
frontend/rust-lib/flowy-date/src/event_handler.rs
Normal file
36
frontend/rust-lib/flowy-date/src/event_handler.rs
Normal 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")),
|
||||
}
|
||||
}
|
19
frontend/rust-lib/flowy-date/src/event_map.rs
Normal file
19
frontend/rust-lib/flowy-date/src/event_map.rs
Normal 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,
|
||||
}
|
4
frontend/rust-lib/flowy-date/src/lib.rs
Normal file
4
frontend/rust-lib/flowy-date/src/lib.rs
Normal file
@ -0,0 +1,4 @@
|
||||
pub mod entities;
|
||||
pub mod event_handler;
|
||||
pub mod event_map;
|
||||
pub mod protobuf;
|
Reference in New Issue
Block a user