287 lines
9.0 KiB
Rust
Raw Normal View History

use crate::entities::FieldType;
use crate::services::field::*;
use bytes::Bytes;
use flowy_error::{internal_error, ErrorCode, FlowyError, FlowyResult};
2022-07-06 10:38:54 +08:00
use flowy_grid_data_model::revision::{CellRevision, FieldRevision, FieldTypeRevision};
2022-04-02 21:17:20 +08:00
use serde::{Deserialize, Serialize};
2022-05-11 11:34:13 +08:00
use std::fmt::Formatter;
2022-05-22 23:33:08 +08:00
use std::str::FromStr;
2022-03-15 11:07:18 +08:00
pub trait CellFilterOperation<T> {
fn apply_filter(&self, any_cell_data: AnyCellData, filter: &T) -> FlowyResult<bool>;
2022-07-06 10:38:54 +08:00
}
pub trait CellDataOperation<D> {
2022-05-24 14:56:55 +08:00
fn decode_cell_data<T>(
2022-04-05 21:25:59 +08:00
&self,
2022-07-06 10:38:54 +08:00
cell_data: T,
2022-05-23 22:53:13 +08:00
decoded_field_type: &FieldType,
field_rev: &FieldRevision,
2022-05-24 14:56:55 +08:00
) -> FlowyResult<DecodedCellData>
where
2022-06-30 23:00:03 +08:00
T: Into<D>;
2022-05-23 22:53:13 +08:00
fn apply_changeset<C: Into<CellContentChangeset>>(
&self,
changeset: C,
cell_rev: Option<CellRevision>,
2022-06-02 15:06:15 +08:00
) -> FlowyResult<String>;
2022-04-05 21:25:59 +08:00
}
#[derive(Debug)]
2022-05-23 22:53:13 +08:00
pub struct CellContentChangeset(pub String);
2022-04-05 21:25:59 +08:00
2022-05-11 11:34:13 +08:00
impl std::fmt::Display for CellContentChangeset {
2022-04-05 21:25:59 +08:00
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", &self.0)
}
}
2022-05-11 11:34:13 +08:00
impl<T: AsRef<str>> std::convert::From<T> for CellContentChangeset {
2022-04-05 21:25:59 +08:00
fn from(s: T) -> Self {
let s = s.as_ref().to_owned();
2022-05-11 11:34:13 +08:00
CellContentChangeset(s)
2022-04-05 21:25:59 +08:00
}
}
2022-05-11 11:34:13 +08:00
impl std::ops::Deref for CellContentChangeset {
2022-04-05 21:25:59 +08:00
type Target = str;
fn deref(&self) -> &Self::Target {
&self.0
}
2022-03-15 11:07:18 +08:00
}
2022-04-05 21:25:59 +08:00
#[derive(Debug, Serialize, Deserialize)]
2022-07-06 10:38:54 +08:00
pub struct AnyCellData {
pub cell_data: String,
2022-04-02 21:17:20 +08:00
pub field_type: FieldType,
}
2022-07-06 10:38:54 +08:00
impl std::str::FromStr for AnyCellData {
2022-04-02 21:17:20 +08:00
type Err = FlowyError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
2022-07-06 10:38:54 +08:00
let type_option_cell_data: AnyCellData = serde_json::from_str(s)?;
2022-04-02 21:17:20 +08:00
Ok(type_option_cell_data)
}
}
2022-07-06 10:38:54 +08:00
impl std::convert::TryInto<AnyCellData> for String {
2022-05-22 23:33:08 +08:00
type Error = FlowyError;
2022-07-06 10:38:54 +08:00
fn try_into(self) -> Result<AnyCellData, Self::Error> {
AnyCellData::from_str(&self)
2022-05-22 23:33:08 +08:00
}
}
2022-07-06 10:38:54 +08:00
impl std::convert::TryFrom<&CellRevision> for AnyCellData {
2022-07-05 17:30:17 +08:00
type Error = FlowyError;
fn try_from(value: &CellRevision) -> Result<Self, Self::Error> {
Self::from_str(&value.data)
}
}
2022-07-06 10:38:54 +08:00
impl AnyCellData {
pub fn new(content: String, field_type: FieldType) -> Self {
AnyCellData {
cell_data: content,
2022-04-02 21:17:20 +08:00
field_type,
}
}
pub fn json(&self) -> String {
serde_json::to_string(self).unwrap_or_else(|_| "".to_owned())
}
pub fn is_number(&self) -> bool {
self.field_type == FieldType::Number
}
pub fn is_text(&self) -> bool {
self.field_type == FieldType::RichText
}
pub fn is_checkbox(&self) -> bool {
self.field_type == FieldType::Checkbox
}
pub fn is_date(&self) -> bool {
self.field_type == FieldType::DateTime
}
pub fn is_single_select(&self) -> bool {
self.field_type == FieldType::SingleSelect
}
pub fn is_multi_select(&self) -> bool {
self.field_type == FieldType::MultiSelect
}
2022-05-22 23:33:08 +08:00
pub fn is_url(&self) -> bool {
self.field_type == FieldType::URL
}
2022-05-22 23:33:08 +08:00
pub fn is_select_option(&self) -> bool {
self.field_type == FieldType::MultiSelect || self.field_type == FieldType::SingleSelect
}
2022-04-02 21:17:20 +08:00
}
2022-04-07 08:33:10 +08:00
/// The changeset will be deserialized into specific data base on the FieldType.
/// For example, it's String on FieldType::RichText, and SelectOptionChangeset on FieldType::SingleSelect
2022-07-01 10:36:07 +08:00
pub fn apply_cell_data_changeset<C: Into<CellContentChangeset>, T: AsRef<FieldRevision>>(
changeset: C,
cell_rev: Option<CellRevision>,
2022-07-01 10:36:07 +08:00
field_rev: T,
2022-04-05 21:25:59 +08:00
) -> Result<String, FlowyError> {
2022-07-01 10:36:07 +08:00
let field_rev = field_rev.as_ref();
let field_type = field_rev.field_type_rev.into();
let s = match field_type {
FieldType::RichText => RichTextTypeOption::from(field_rev).apply_changeset(changeset, cell_rev),
FieldType::Number => NumberTypeOption::from(field_rev).apply_changeset(changeset, cell_rev),
FieldType::DateTime => DateTypeOption::from(field_rev).apply_changeset(changeset, cell_rev),
FieldType::SingleSelect => SingleSelectTypeOption::from(field_rev).apply_changeset(changeset, cell_rev),
FieldType::MultiSelect => MultiSelectTypeOption::from(field_rev).apply_changeset(changeset, cell_rev),
FieldType::Checkbox => CheckboxTypeOption::from(field_rev).apply_changeset(changeset, cell_rev),
FieldType::URL => URLTypeOption::from(field_rev).apply_changeset(changeset, cell_rev),
2022-05-22 23:33:08 +08:00
}?;
2022-07-06 10:38:54 +08:00
Ok(AnyCellData::new(s, field_type).json())
2022-03-15 11:07:18 +08:00
}
2022-05-09 16:08:27 +08:00
2022-07-06 10:38:54 +08:00
pub fn decode_any_cell_data<T: TryInto<AnyCellData>>(data: T, field_rev: &FieldRevision) -> DecodedCellData {
if let Ok(any_cell_data) = data.try_into() {
let AnyCellData { cell_data, field_type } = any_cell_data;
let to_field_type = field_rev.field_type_rev.into();
2022-07-06 10:38:54 +08:00
match try_decode_cell_data(cell_data, field_rev, &field_type, &to_field_type) {
Ok(cell_data) => cell_data,
Err(e) => {
tracing::error!("Decode cell data failed, {:?}", e);
DecodedCellData::default()
}
}
2022-05-24 14:56:55 +08:00
} else {
tracing::error!("Decode type option data failed");
2022-05-24 14:56:55 +08:00
DecodedCellData::default()
}
}
2022-06-30 23:00:03 +08:00
pub fn try_decode_cell_data(
2022-07-06 10:38:54 +08:00
cell_data: String,
2022-06-24 18:13:40 +08:00
field_rev: &FieldRevision,
2022-05-24 14:56:55 +08:00
s_field_type: &FieldType,
t_field_type: &FieldType,
) -> FlowyResult<DecodedCellData> {
let get_cell_data = || {
2022-07-06 10:38:54 +08:00
let field_type: FieldTypeRevision = t_field_type.into();
2022-05-24 14:56:55 +08:00
let data = match t_field_type {
FieldType::RichText => field_rev
2022-07-06 10:38:54 +08:00
.get_type_option_entry::<RichTextTypeOption>(field_type)?
.decode_cell_data(cell_data, s_field_type, field_rev),
FieldType::Number => field_rev
2022-07-06 10:38:54 +08:00
.get_type_option_entry::<NumberTypeOption>(field_type)?
.decode_cell_data(cell_data, s_field_type, field_rev),
FieldType::DateTime => field_rev
2022-07-06 10:38:54 +08:00
.get_type_option_entry::<DateTypeOption>(field_type)?
.decode_cell_data(cell_data, s_field_type, field_rev),
FieldType::SingleSelect => field_rev
2022-07-06 10:38:54 +08:00
.get_type_option_entry::<SingleSelectTypeOption>(field_type)?
.decode_cell_data(cell_data, s_field_type, field_rev),
FieldType::MultiSelect => field_rev
2022-07-06 10:38:54 +08:00
.get_type_option_entry::<MultiSelectTypeOption>(field_type)?
.decode_cell_data(cell_data, s_field_type, field_rev),
FieldType::Checkbox => field_rev
2022-07-06 10:38:54 +08:00
.get_type_option_entry::<CheckboxTypeOption>(field_type)?
.decode_cell_data(cell_data, s_field_type, field_rev),
FieldType::URL => field_rev
2022-07-06 10:38:54 +08:00
.get_type_option_entry::<URLTypeOption>(field_type)?
.decode_cell_data(cell_data, s_field_type, field_rev),
2022-05-22 23:33:08 +08:00
};
2022-05-24 14:56:55 +08:00
Some(data)
};
match get_cell_data() {
Some(Ok(data)) => Ok(data),
2022-05-24 14:56:55 +08:00
Some(Err(err)) => {
tracing::error!("{:?}", err);
Ok(DecodedCellData::default())
}
None => Ok(DecodedCellData::default()),
}
}
pub(crate) struct EncodedCellData<T>(pub Option<T>);
impl<T> EncodedCellData<T> {
pub fn try_into_inner(self) -> FlowyResult<T> {
match self.0 {
None => Err(ErrorCode::InvalidData.into()),
Some(data) => Ok(data),
}
}
}
impl<T> std::convert::From<String> for EncodedCellData<T>
where
T: FromStr<Err = FlowyError>,
{
fn from(s: String) -> Self {
match T::from_str(&s) {
Ok(inner) => EncodedCellData(Some(inner)),
Err(e) => {
tracing::error!("Deserialize Cell Data failed: {}", e);
EncodedCellData(None)
}
}
2022-05-22 23:33:08 +08:00
}
2022-03-15 11:07:18 +08:00
}
2022-05-11 11:34:13 +08:00
2022-06-26 15:14:24 +08:00
/// The data is encoded by protobuf or utf8. You should choose the corresponding decode struct to parse it.
///
/// For example:
///
/// * Use DateCellData to parse the data when the FieldType is Date.
/// * Use URLCellData to parse the data when the FieldType is URL.
/// * Use String to parse the data when the FieldType is RichText, Number, or Checkbox.
/// * Check out the implementation of CellDataOperation trait for more information.
2022-05-11 11:34:13 +08:00
#[derive(Default)]
pub struct DecodedCellData {
2022-05-28 08:35:22 +08:00
pub data: Vec<u8>,
2022-05-11 11:34:13 +08:00
}
impl DecodedCellData {
pub fn new<T: AsRef<[u8]>>(data: T) -> Self {
2022-05-11 11:34:13 +08:00
Self {
data: data.as_ref().to_vec(),
2022-05-11 11:34:13 +08:00
}
}
pub fn try_from_bytes<T: TryInto<Bytes>>(bytes: T) -> FlowyResult<Self>
where
<T as TryInto<Bytes>>::Error: std::fmt::Debug,
{
let bytes = bytes.try_into().map_err(internal_error)?;
Ok(Self { data: bytes.to_vec() })
}
pub fn parse<'a, T: TryFrom<&'a [u8]>>(&'a self) -> FlowyResult<T>
where
<T as TryFrom<&'a [u8]>>::Error: std::fmt::Debug,
{
T::try_from(self.data.as_ref()).map_err(internal_error)
2022-05-11 11:34:13 +08:00
}
}
2022-05-11 11:34:13 +08:00
impl ToString for DecodedCellData {
fn to_string(&self) -> String {
match String::from_utf8(self.data.clone()) {
Ok(s) => s,
Err(e) => {
tracing::error!("DecodedCellData to string failed: {:?}", e);
"".to_string()
}
}
2022-05-11 11:34:13 +08:00
}
}