250 lines
7.5 KiB
Rust
Raw Normal View History

use crate::services::field::*;
2022-05-24 14:56:55 +08:00
use flowy_error::{ErrorCode, FlowyError, FlowyResult};
2022-04-05 21:25:59 +08:00
use flowy_grid_data_model::entities::{CellMeta, FieldMeta, FieldType};
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
2022-05-23 22:53:13 +08:00
pub trait CellDataOperation<D, CO: ToString> {
2022-05-24 14:56:55 +08:00
fn decode_cell_data<T>(
2022-04-05 21:25:59 +08:00
&self,
2022-05-24 14:56:55 +08:00
encoded_data: T,
2022-05-23 22:53:13 +08:00
decoded_field_type: &FieldType,
field_meta: &FieldMeta,
2022-05-24 14:56:55 +08:00
) -> FlowyResult<DecodedCellData>
where
T: Into<D>;
//
2022-05-23 22:53:13 +08:00
fn apply_changeset<C: Into<CellContentChangeset>>(
&self,
changeset: C,
2022-04-05 21:25:59 +08:00
cell_meta: Option<CellMeta>,
2022-05-24 14:56:55 +08:00
) -> FlowyResult<CO>;
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-04-02 21:17:20 +08:00
pub struct TypeOptionCellData {
pub data: String,
pub field_type: FieldType,
}
2022-05-23 22:53:13 +08:00
impl TypeOptionCellData {
pub fn split(self) -> (String, FieldType) {
(self.data, self.field_type)
}
}
2022-04-02 21:17:20 +08:00
impl std::str::FromStr for TypeOptionCellData {
type Err = FlowyError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let type_option_cell_data: TypeOptionCellData = serde_json::from_str(s)?;
Ok(type_option_cell_data)
}
}
2022-05-22 23:33:08 +08:00
impl std::convert::TryInto<TypeOptionCellData> for String {
type Error = FlowyError;
fn try_into(self) -> Result<TypeOptionCellData, Self::Error> {
TypeOptionCellData::from_str(&self)
}
}
2022-04-02 21:17:20 +08:00
impl TypeOptionCellData {
2022-04-05 21:25:59 +08:00
pub fn new<T: ToString>(data: T, field_type: FieldType) -> Self {
2022-04-02 21:17:20 +08:00
TypeOptionCellData {
2022-04-05 21:25:59 +08:00
data: data.to_string(),
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_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-05-11 11:34:13 +08:00
pub fn apply_cell_data_changeset<T: Into<CellContentChangeset>>(
2022-04-05 21:25:59 +08:00
changeset: T,
cell_meta: Option<CellMeta>,
field_meta: &FieldMeta,
) -> Result<String, FlowyError> {
2022-05-22 23:33:08 +08:00
let s = match field_meta.field_type {
2022-04-05 21:25:59 +08:00
FieldType::RichText => RichTextTypeOption::from(field_meta).apply_changeset(changeset, cell_meta),
FieldType::Number => NumberTypeOption::from(field_meta).apply_changeset(changeset, cell_meta),
2022-05-23 22:53:13 +08:00
FieldType::DateTime => DateTypeOption::from(field_meta)
.apply_changeset(changeset, cell_meta)
.map(|data| data.to_string()),
2022-04-05 21:25:59 +08:00
FieldType::SingleSelect => SingleSelectTypeOption::from(field_meta).apply_changeset(changeset, cell_meta),
FieldType::MultiSelect => MultiSelectTypeOption::from(field_meta).apply_changeset(changeset, cell_meta),
FieldType::Checkbox => CheckboxTypeOption::from(field_meta).apply_changeset(changeset, cell_meta),
2022-05-22 23:33:08 +08:00
}?;
Ok(TypeOptionCellData::new(s, field_meta.field_type.clone()).json())
2022-03-15 11:07:18 +08:00
}
2022-05-09 16:08:27 +08:00
2022-05-24 14:56:55 +08:00
pub fn decode_cell_data_from_type_option_cell_data<T: TryInto<TypeOptionCellData>>(
2022-05-22 23:33:08 +08:00
data: T,
field_meta: &FieldMeta,
field_type: &FieldType,
2022-05-24 14:56:55 +08:00
) -> DecodedCellData {
2022-05-22 23:33:08 +08:00
if let Ok(type_option_cell_data) = data.try_into() {
2022-05-24 14:56:55 +08:00
let (encoded_data, s_field_type) = type_option_cell_data.split();
decode_cell_data(encoded_data, &s_field_type, field_type, field_meta).unwrap_or_default()
} else {
DecodedCellData::default()
}
}
pub fn decode_cell_data<T: Into<String>>(
encoded_data: T,
s_field_type: &FieldType,
t_field_type: &FieldType,
field_meta: &FieldMeta,
) -> FlowyResult<DecodedCellData> {
let encoded_data = encoded_data.into();
let get_cell_data = || {
let data = match t_field_type {
2022-05-22 23:33:08 +08:00
FieldType::RichText => field_meta
2022-05-24 14:56:55 +08:00
.get_type_option_entry::<RichTextTypeOption>(t_field_type)?
.decode_cell_data(encoded_data, s_field_type, field_meta),
2022-05-22 23:33:08 +08:00
FieldType::Number => field_meta
2022-05-24 14:56:55 +08:00
.get_type_option_entry::<NumberTypeOption>(t_field_type)?
.decode_cell_data(encoded_data, s_field_type, field_meta),
2022-05-22 23:33:08 +08:00
FieldType::DateTime => field_meta
2022-05-24 14:56:55 +08:00
.get_type_option_entry::<DateTypeOption>(t_field_type)?
.decode_cell_data(encoded_data, s_field_type, field_meta),
2022-05-22 23:33:08 +08:00
FieldType::SingleSelect => field_meta
2022-05-24 14:56:55 +08:00
.get_type_option_entry::<SingleSelectTypeOption>(t_field_type)?
.decode_cell_data(encoded_data, s_field_type, field_meta),
2022-05-22 23:33:08 +08:00
FieldType::MultiSelect => field_meta
2022-05-24 14:56:55 +08:00
.get_type_option_entry::<MultiSelectTypeOption>(t_field_type)?
.decode_cell_data(encoded_data, s_field_type, field_meta),
2022-05-22 23:33:08 +08:00
FieldType::Checkbox => field_meta
2022-05-24 14:56:55 +08:00
.get_type_option_entry::<CheckboxTypeOption>(t_field_type)?
.decode_cell_data(encoded_data, s_field_type, field_meta),
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)) => {
tracing::Span::current().record(
"content",
&format!("{:?}: {}", field_meta.field_type, data.content).as_str(),
);
Ok(data)
}
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
#[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
pub content: String,
}
impl DecodedCellData {
pub fn from_content(content: String) -> Self {
Self {
2022-05-28 08:35:22 +08:00
data: content.as_bytes().to_vec(),
2022-05-11 11:34:13 +08:00
content,
}
}
2022-05-28 08:35:22 +08:00
pub fn new<T: AsRef<[u8]>>(data: T, content: String) -> Self {
let data = data.as_ref().to_vec();
Self { data, content }
2022-05-11 11:34:13 +08:00
}
2022-05-28 08:35:22 +08:00
pub fn split(self) -> (Vec<u8>, String) {
(self.data, self.content)
2022-05-11 11:34:13 +08:00
}
}