Skip to content

Commit 5b2b50d

Browse files
committed
refactor: cell data operation
1 parent 1ae0b18 commit 5b2b50d

File tree

6 files changed

+80
-64
lines changed

6 files changed

+80
-64
lines changed

frontend/rust-lib/flowy-grid/src/services/field/type_options/checkbox_type_option.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,15 @@ impl_type_option!(CheckboxTypeOption, FieldType::Checkbox);
4343
const YES: &str = "Yes";
4444
const NO: &str = "No";
4545

46-
impl CellDataOperation for CheckboxTypeOption {
46+
impl CellDataOperation<String> for CheckboxTypeOption {
4747
fn decode_cell_data<T: Into<TypeOptionCellData>>(
4848
&self,
4949
type_option_cell_data: T,
50+
decoded_field_type: &FieldType,
5051
_field_meta: &FieldMeta,
5152
) -> DecodedCellData {
5253
let type_option_cell_data = type_option_cell_data.into();
53-
if !type_option_cell_data.is_checkbox() {
54+
if !decoded_field_type.is_checkbox() {
5455
return DecodedCellData::default();
5556
}
5657
let cell_data = type_option_cell_data.data;
@@ -61,11 +62,10 @@ impl CellDataOperation for CheckboxTypeOption {
6162
DecodedCellData::default()
6263
}
6364

64-
fn apply_changeset<T: Into<CellContentChangeset>>(
65-
&self,
66-
changeset: T,
67-
_cell_meta: Option<CellMeta>,
68-
) -> Result<String, FlowyError> {
65+
fn apply_changeset<C>(&self, changeset: C, _cell_meta: Option<CellMeta>) -> Result<String, FlowyError>
66+
where
67+
C: Into<CellContentChangeset>,
68+
{
6969
let changeset = changeset.into();
7070
let s = match string_to_bool(&changeset) {
7171
true => YES,

frontend/rust-lib/flowy-grid/src/services/field/type_options/date_type_option.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,19 @@ impl DateTypeOption {
135135
}
136136
}
137137

138-
impl CellDataOperation for DateTypeOption {
138+
impl CellDataOperation<DateCellDataSerde> for DateTypeOption {
139139
fn decode_cell_data<T: Into<TypeOptionCellData>>(
140140
&self,
141141
type_option_cell_data: T,
142+
decoded_field_type: &FieldType,
142143
_field_meta: &FieldMeta,
143144
) -> DecodedCellData {
144145
let type_option_cell_data = type_option_cell_data.into();
145146
// Return default data if the type_option_cell_data is not FieldType::DateTime.
146147
// It happens when switching from one field to another.
147148
// For example:
148149
// FieldType::RichText -> FieldType::DateTime, it will display empty content on the screen.
149-
if !type_option_cell_data.is_date() {
150+
if !decoded_field_type.is_date() {
150151
return DecodedCellData::default();
151152
}
152153
match DateCellDataSerde::from_str(&type_option_cell_data.data) {
@@ -155,11 +156,10 @@ impl CellDataOperation for DateTypeOption {
155156
}
156157
}
157158

158-
fn apply_changeset<T: Into<CellContentChangeset>>(
159-
&self,
160-
changeset: T,
161-
_cell_meta: Option<CellMeta>,
162-
) -> Result<String, FlowyError> {
159+
fn apply_changeset<C>(&self, changeset: C, _cell_meta: Option<CellMeta>) -> Result<DateCellDataSerde, FlowyError>
160+
where
161+
C: Into<CellContentChangeset>,
162+
{
163163
let content_changeset: DateCellContentChangeset = serde_json::from_str(&changeset.into())?;
164164
let cell_data = match content_changeset.date_timestamp() {
165165
None => DateCellDataSerde::default(),
@@ -174,7 +174,7 @@ impl CellDataOperation for DateTypeOption {
174174
},
175175
};
176176

177-
Ok(cell_data.to_string())
177+
Ok(cell_data)
178178
}
179179
}
180180

@@ -316,15 +316,17 @@ impl DateCellDataSerde {
316316
Self { timestamp, time }
317317
}
318318

319-
fn to_string(self) -> String {
320-
serde_json::to_string(&self).unwrap_or("".to_string())
321-
}
322-
323319
fn from_str(s: &str) -> FlowyResult<Self> {
324320
serde_json::from_str::<DateCellDataSerde>(s).map_err(internal_error)
325321
}
326322
}
327323

324+
impl ToString for DateCellDataSerde {
325+
fn to_string(&self) -> String {
326+
serde_json::to_string(&self).unwrap_or("".to_string())
327+
}
328+
}
329+
328330
fn default_time_str(time_format: &TimeFormat) -> String {
329331
match time_format {
330332
TimeFormat::TwelveHour => "12:00 AM".to_string(),

frontend/rust-lib/flowy-grid/src/services/field/type_options/number_type_option.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,15 @@ pub struct NumberTypeOption {
7676
}
7777
impl_type_option!(NumberTypeOption, FieldType::Number);
7878

79-
impl CellDataOperation for NumberTypeOption {
79+
impl CellDataOperation<String> for NumberTypeOption {
8080
fn decode_cell_data<T: Into<TypeOptionCellData>>(
8181
&self,
8282
type_option_cell_data: T,
83+
decoded_field_type: &FieldType,
8384
_field_meta: &FieldMeta,
8485
) -> DecodedCellData {
8586
let type_option_cell_data = type_option_cell_data.into();
86-
if type_option_cell_data.is_date() {
87+
if decoded_field_type.is_date() {
8788
return DecodedCellData::default();
8889
}
8990

@@ -111,11 +112,10 @@ impl CellDataOperation for NumberTypeOption {
111112
}
112113
}
113114

114-
fn apply_changeset<T: Into<CellContentChangeset>>(
115-
&self,
116-
changeset: T,
117-
_cell_meta: Option<CellMeta>,
118-
) -> Result<String, FlowyError> {
115+
fn apply_changeset<C>(&self, changeset: C, _cell_meta: Option<CellMeta>) -> Result<String, FlowyError>
116+
where
117+
C: Into<CellContentChangeset>,
118+
{
119119
let changeset = changeset.into();
120120
let mut data = changeset.trim().to_string();
121121

frontend/rust-lib/flowy-grid/src/services/field/type_options/selection_type_option.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,15 @@ impl SelectOptionOperation for SingleSelectTypeOption {
9595
}
9696
}
9797

98-
impl CellDataOperation for SingleSelectTypeOption {
98+
impl CellDataOperation<String> for SingleSelectTypeOption {
9999
fn decode_cell_data<T: Into<TypeOptionCellData>>(
100100
&self,
101101
type_option_cell_data: T,
102+
decoded_field_type: &FieldType,
102103
_field_meta: &FieldMeta,
103104
) -> DecodedCellData {
104105
let type_option_cell_data = type_option_cell_data.into();
105-
if !type_option_cell_data.is_select_option() {
106+
if !decoded_field_type.is_select_option() {
106107
return DecodedCellData::default();
107108
}
108109

@@ -116,11 +117,10 @@ impl CellDataOperation for SingleSelectTypeOption {
116117
DecodedCellData::default()
117118
}
118119

119-
fn apply_changeset<T: Into<CellContentChangeset>>(
120-
&self,
121-
changeset: T,
122-
_cell_meta: Option<CellMeta>,
123-
) -> Result<String, FlowyError> {
120+
fn apply_changeset<C>(&self, changeset: C, _cell_meta: Option<CellMeta>) -> Result<String, FlowyError>
121+
where
122+
C: Into<CellContentChangeset>,
123+
{
124124
let changeset = changeset.into();
125125
let select_option_changeset: SelectOptionCellContentChangeset = serde_json::from_str(&changeset)?;
126126
let new_cell_data: String;
@@ -187,14 +187,15 @@ impl SelectOptionOperation for MultiSelectTypeOption {
187187
}
188188
}
189189

190-
impl CellDataOperation for MultiSelectTypeOption {
190+
impl CellDataOperation<String> for MultiSelectTypeOption {
191191
fn decode_cell_data<T: Into<TypeOptionCellData>>(
192192
&self,
193193
type_option_cell_data: T,
194+
decoded_field_type: &FieldType,
194195
_field_meta: &FieldMeta,
195196
) -> DecodedCellData {
196197
let type_option_cell_data = type_option_cell_data.into();
197-
if !type_option_cell_data.is_select_option() {
198+
if !decoded_field_type.is_select_option() {
198199
return DecodedCellData::default();
199200
}
200201

@@ -210,11 +211,10 @@ impl CellDataOperation for MultiSelectTypeOption {
210211
DecodedCellData::from_content(content)
211212
}
212213

213-
fn apply_changeset<T: Into<CellContentChangeset>>(
214-
&self,
215-
changeset: T,
216-
cell_meta: Option<CellMeta>,
217-
) -> Result<String, FlowyError> {
214+
fn apply_changeset<T>(&self, changeset: T, cell_meta: Option<CellMeta>) -> Result<String, FlowyError>
215+
where
216+
T: Into<CellContentChangeset>,
217+
{
218218
let content_changeset: SelectOptionCellContentChangeset = serde_json::from_str(&changeset.into())?;
219219
let new_cell_data: String;
220220
match cell_meta {

frontend/rust-lib/flowy-grid/src/services/field/type_options/text_type_option.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,18 @@ pub struct RichTextTypeOption {
3434
}
3535
impl_type_option!(RichTextTypeOption, FieldType::RichText);
3636

37-
impl CellDataOperation for RichTextTypeOption {
37+
impl CellDataOperation<String> for RichTextTypeOption {
3838
fn decode_cell_data<T: Into<TypeOptionCellData>>(
3939
&self,
4040
type_option_cell_data: T,
41+
decoded_field_type: &FieldType,
4142
field_meta: &FieldMeta,
4243
) -> DecodedCellData {
4344
let type_option_cell_data = type_option_cell_data.into();
44-
if type_option_cell_data.is_date()
45-
|| type_option_cell_data.is_single_select()
46-
|| type_option_cell_data.is_multi_select()
47-
|| type_option_cell_data.is_number()
45+
if decoded_field_type.is_date()
46+
|| decoded_field_type.is_single_select()
47+
|| decoded_field_type.is_multi_select()
48+
|| decoded_field_type.is_number()
4849
{
4950
let field_type = type_option_cell_data.field_type.clone();
5051
decode_cell_data(type_option_cell_data, field_meta, &field_type).unwrap_or_default()
@@ -53,16 +54,15 @@ impl CellDataOperation for RichTextTypeOption {
5354
}
5455
}
5556

56-
fn apply_changeset<T: Into<CellContentChangeset>>(
57-
&self,
58-
changeset: T,
59-
_cell_meta: Option<CellMeta>,
60-
) -> Result<String, FlowyError> {
57+
fn apply_changeset<C>(&self, changeset: C, _cell_meta: Option<CellMeta>) -> Result<String, FlowyError>
58+
where
59+
C: Into<CellContentChangeset>,
60+
{
6161
let data = changeset.into();
6262
if data.len() > 10000 {
6363
Err(FlowyError::text_too_long().context("The len of the text should not be more than 10000"))
6464
} else {
65-
Ok(data.to_string())
65+
Ok(data.0)
6666
}
6767
}
6868
}

frontend/rust-lib/flowy-grid/src/services/row/cell_data_operation.rs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,22 @@ use serde::{Deserialize, Serialize};
55
use std::fmt::Formatter;
66
use std::str::FromStr;
77

8-
pub trait CellDataOperation {
9-
fn decode_cell_data<T: Into<TypeOptionCellData>>(&self, data: T, field_meta: &FieldMeta) -> DecodedCellData;
10-
fn apply_changeset<T: Into<CellContentChangeset>>(
8+
pub trait CellDataOperation<D, CO: ToString> {
9+
fn decode_cell_data<T: Into<TypeOptionCellData>>(
1110
&self,
12-
changeset: T,
11+
data: T,
12+
decoded_field_type: &FieldType,
13+
field_meta: &FieldMeta,
14+
) -> DecodedCellData;
15+
fn apply_changeset<C: Into<CellContentChangeset>>(
16+
&self,
17+
changeset: C,
1318
cell_meta: Option<CellMeta>,
14-
) -> Result<String, FlowyError>;
19+
) -> Result<CO, FlowyError>;
1520
}
1621

1722
#[derive(Debug)]
18-
pub struct CellContentChangeset(String);
23+
pub struct CellContentChangeset(pub String);
1924

2025
impl std::fmt::Display for CellContentChangeset {
2126
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
@@ -44,6 +49,12 @@ pub struct TypeOptionCellData {
4449
pub field_type: FieldType,
4550
}
4651

52+
impl TypeOptionCellData {
53+
pub fn split(self) -> (String, FieldType) {
54+
(self.data, self.field_type)
55+
}
56+
}
57+
4758
impl std::str::FromStr for TypeOptionCellData {
4859
type Err = FlowyError;
4960

@@ -120,7 +131,9 @@ pub fn apply_cell_data_changeset<T: Into<CellContentChangeset>>(
120131
let s = match field_meta.field_type {
121132
FieldType::RichText => RichTextTypeOption::from(field_meta).apply_changeset(changeset, cell_meta),
122133
FieldType::Number => NumberTypeOption::from(field_meta).apply_changeset(changeset, cell_meta),
123-
FieldType::DateTime => DateTypeOption::from(field_meta).apply_changeset(changeset, cell_meta),
134+
FieldType::DateTime => DateTypeOption::from(field_meta)
135+
.apply_changeset(changeset, cell_meta)
136+
.map(|data| data.to_string()),
124137
FieldType::SingleSelect => SingleSelectTypeOption::from(field_meta).apply_changeset(changeset, cell_meta),
125138
FieldType::MultiSelect => MultiSelectTypeOption::from(field_meta).apply_changeset(changeset, cell_meta),
126139
FieldType::Checkbox => CheckboxTypeOption::from(field_meta).apply_changeset(changeset, cell_meta),
@@ -135,25 +148,26 @@ pub fn decode_cell_data<T: TryInto<TypeOptionCellData>>(
135148
field_type: &FieldType,
136149
) -> Option<DecodedCellData> {
137150
if let Ok(type_option_cell_data) = data.try_into() {
151+
let (decoded_field_type, data) = &type_option_cell_data.split();
138152
let s = match field_type {
139153
FieldType::RichText => field_meta
140154
.get_type_option_entry::<RichTextTypeOption>(field_type)?
141-
.decode_cell_data(type_option_cell_data, field_meta),
155+
.decode_cell_data(type_option_cell_data, decoded_field_type, field_meta),
142156
FieldType::Number => field_meta
143157
.get_type_option_entry::<NumberTypeOption>(field_type)?
144-
.decode_cell_data(type_option_cell_data, field_meta),
158+
.decode_cell_data(type_option_cell_data, decoded_field_type, field_meta),
145159
FieldType::DateTime => field_meta
146160
.get_type_option_entry::<DateTypeOption>(field_type)?
147-
.decode_cell_data(type_option_cell_data, field_meta),
161+
.decode_cell_data(type_option_cell_data, decoded_field_type, field_meta),
148162
FieldType::SingleSelect => field_meta
149163
.get_type_option_entry::<SingleSelectTypeOption>(field_type)?
150-
.decode_cell_data(type_option_cell_data, field_meta),
164+
.decode_cell_data(type_option_cell_data, decoded_field_type, field_meta),
151165
FieldType::MultiSelect => field_meta
152166
.get_type_option_entry::<MultiSelectTypeOption>(field_type)?
153-
.decode_cell_data(type_option_cell_data, field_meta),
167+
.decode_cell_data(type_option_cell_data, decoded_field_type, field_meta),
154168
FieldType::Checkbox => field_meta
155169
.get_type_option_entry::<CheckboxTypeOption>(field_type)?
156-
.decode_cell_data(type_option_cell_data, field_meta),
170+
.decode_cell_data(type_option_cell_data, decoded_field_type, field_meta),
157171
};
158172
tracing::Span::current().record(
159173
"content",

0 commit comments

Comments
 (0)