2022-03-23 22:10:31 +08:00
|
|
|
use crate::services::field::*;
|
2022-05-09 16:08:27 +08:00
|
|
|
use std::borrow::Cow;
|
2022-04-05 21:25:59 +08:00
|
|
|
use std::fmt::Formatter;
|
2022-05-09 16:08:27 +08:00
|
|
|
use std::sync::Arc;
|
2022-04-05 21:25:59 +08:00
|
|
|
|
2022-03-15 11:07:18 +08:00
|
|
|
use flowy_error::FlowyError;
|
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-03-15 11:07:18 +08:00
|
|
|
|
2022-04-05 14:25:07 +08:00
|
|
|
pub trait CellDataOperation {
|
2022-05-09 16:08:27 +08:00
|
|
|
fn decode_cell_data(&self, data: String, field_meta: &FieldMeta) -> DecodedCellData;
|
2022-04-05 21:25:59 +08:00
|
|
|
fn apply_changeset<T: Into<CellDataChangeset>>(
|
|
|
|
&self,
|
|
|
|
changeset: T,
|
|
|
|
cell_meta: Option<CellMeta>,
|
|
|
|
) -> Result<String, FlowyError>;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct CellDataChangeset(String);
|
|
|
|
|
|
|
|
impl std::fmt::Display for CellDataChangeset {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
|
|
|
write!(f, "{}", &self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: AsRef<str>> std::convert::From<T> for CellDataChangeset {
|
|
|
|
fn from(s: T) -> Self {
|
|
|
|
let s = s.as_ref().to_owned();
|
|
|
|
CellDataChangeset(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::ops::Deref for CellDataChangeset {
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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-04-05 21:25:59 +08:00
|
|
|
pub fn apply_cell_data_changeset<T: Into<CellDataChangeset>>(
|
|
|
|
changeset: T,
|
|
|
|
cell_meta: Option<CellMeta>,
|
|
|
|
field_meta: &FieldMeta,
|
|
|
|
) -> Result<String, FlowyError> {
|
2022-04-01 16:38:51 +08:00
|
|
|
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),
|
|
|
|
FieldType::DateTime => DateTypeOption::from(field_meta).apply_changeset(changeset, cell_meta),
|
|
|
|
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-03-15 11:07:18 +08:00
|
|
|
}
|
|
|
|
}
|
2022-05-09 16:08:27 +08:00
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct DecodedCellData {
|
|
|
|
raw: String,
|
|
|
|
content: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DecodedCellData {
|
|
|
|
pub fn from_content(content: String) -> Self {
|
|
|
|
Self {
|
|
|
|
raw: content.clone(),
|
|
|
|
content,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new(raw: String, content: String) -> Self {
|
|
|
|
Self { raw, content }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn split(self) -> (String, String) {
|
|
|
|
(self.raw, self.content)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn decode_cell_data(data: String, field_meta: &FieldMeta, field_type: &FieldType) -> Option<DecodedCellData> {
|
2022-04-10 20:21:28 +08:00
|
|
|
let s = match field_type {
|
|
|
|
FieldType::RichText => field_meta
|
|
|
|
.get_type_option_entry::<RichTextTypeOption>(field_type)?
|
|
|
|
.decode_cell_data(data, field_meta),
|
|
|
|
FieldType::Number => field_meta
|
|
|
|
.get_type_option_entry::<NumberTypeOption>(field_type)?
|
|
|
|
.decode_cell_data(data, field_meta),
|
|
|
|
FieldType::DateTime => field_meta
|
|
|
|
.get_type_option_entry::<DateTypeOption>(field_type)?
|
|
|
|
.decode_cell_data(data, field_meta),
|
|
|
|
FieldType::SingleSelect => field_meta
|
|
|
|
.get_type_option_entry::<SingleSelectTypeOption>(field_type)?
|
|
|
|
.decode_cell_data(data, field_meta),
|
|
|
|
FieldType::MultiSelect => field_meta
|
|
|
|
.get_type_option_entry::<MultiSelectTypeOption>(field_type)?
|
|
|
|
.decode_cell_data(data, field_meta),
|
|
|
|
FieldType::Checkbox => field_meta
|
|
|
|
.get_type_option_entry::<CheckboxTypeOption>(field_type)?
|
|
|
|
.decode_cell_data(data, field_meta),
|
2022-03-15 11:07:18 +08:00
|
|
|
};
|
2022-05-09 16:08:27 +08:00
|
|
|
tracing::Span::current().record(
|
|
|
|
"content",
|
|
|
|
&format!("{:?}: {}", field_meta.field_type, s.content).as_str(),
|
|
|
|
);
|
2022-04-10 20:21:28 +08:00
|
|
|
Some(s)
|
2022-03-15 11:07:18 +08:00
|
|
|
}
|