chore: add comments

This commit is contained in:
appflowy 2022-07-08 10:50:14 +08:00
parent 02cdd8ca4c
commit 41989cf186
3 changed files with 17 additions and 10 deletions

View File

@ -458,6 +458,8 @@ impl TryInto<FieldChangesetParams> for FieldChangesetPayload {
Serialize_repr,
Deserialize_repr,
)]
/// The order of the enum can't be changed. If you want to add a new type,
/// it would be better to append it to the end of the list.
#[repr(u8)]
pub enum FieldType {
RichText = 0,

View File

@ -2,7 +2,7 @@ use crate::entities::{FieldType, GridTextFilter};
use crate::impl_type_option;
use crate::services::field::{BoxTypeOptionBuilder, TextCellData, TypeOptionBuilder};
use crate::services::row::{
AnyCellData, CellContentChangeset, CellDataOperation, CellFilterOperation, DecodedCellData, EncodedCellData,
AnyCellData, CellContentChangeset, CellDataOperation, CellFilterOperation, DecodedCellData, Parser,
};
use bytes::Bytes;
use fancy_regex::Regex;
@ -46,7 +46,7 @@ impl CellFilterOperation<GridTextFilter> for URLTypeOption {
}
}
impl CellDataOperation<EncodedCellData<URLCellData>> for URLTypeOption {
impl CellDataOperation<Parser<URLCellData>> for URLTypeOption {
fn decode_cell_data<T>(
&self,
cell_data: T,
@ -54,7 +54,7 @@ impl CellDataOperation<EncodedCellData<URLCellData>> for URLTypeOption {
_field_rev: &FieldRevision,
) -> FlowyResult<DecodedCellData>
where
T: Into<EncodedCellData<URLCellData>>,
T: Into<Parser<URLCellData>>,
{
if !decoded_field_type.is_url() {
return Ok(DecodedCellData::default());
@ -152,7 +152,7 @@ mod tests {
use crate::entities::FieldType;
use crate::services::field::FieldBuilder;
use crate::services::field::{URLCellData, URLTypeOption};
use crate::services::row::{CellDataOperation, EncodedCellData};
use crate::services::row::{CellDataOperation, Parser};
use flowy_grid_data_model::revision::FieldRevision;
#[test]
@ -201,7 +201,7 @@ mod tests {
assert_eq!(expected_url.to_owned(), decode_cell_data.url);
}
fn decode_cell_data<T: Into<EncodedCellData<URLCellData>>>(
fn decode_cell_data<T: Into<Parser<URLCellData>>>(
encoded_data: T,
type_option: &URLTypeOption,
field_rev: &FieldRevision,

View File

@ -12,6 +12,8 @@ pub trait CellFilterOperation<T> {
}
pub trait CellDataOperation<D> {
/// The cell_data is able to parse into the specific data that was impl the From<String> trait.
/// D will be URLCellData, DateCellData. etc.
fn decode_cell_data<T>(
&self,
cell_data: T,
@ -52,6 +54,9 @@ impl std::ops::Deref for CellContentChangeset {
}
}
/// AnyCellData is a generic CellData, you can parse the cell_data according to the field_type.
/// When the type of field is changed, it's different from the field_type of AnyCellData.
/// So it will return an empty data. You could check the CellDataOperation trait for more information.
#[derive(Debug, Serialize, Deserialize)]
pub struct AnyCellData {
pub cell_data: String,
@ -230,9 +235,9 @@ pub fn try_decode_cell_data(
}
}
pub(crate) struct EncodedCellData<T>(pub Option<T>);
pub(crate) struct Parser<T>(pub Option<T>);
impl<T> EncodedCellData<T> {
impl<T> Parser<T> {
pub fn try_into_inner(self) -> FlowyResult<T> {
match self.0 {
None => Err(ErrorCode::InvalidData.into()),
@ -241,16 +246,16 @@ impl<T> EncodedCellData<T> {
}
}
impl<T> std::convert::From<String> for EncodedCellData<T>
impl<T> std::convert::From<String> for Parser<T>
where
T: FromStr<Err = FlowyError>,
{
fn from(s: String) -> Self {
match T::from_str(&s) {
Ok(inner) => EncodedCellData(Some(inner)),
Ok(inner) => Parser(Some(inner)),
Err(e) => {
tracing::error!("Deserialize Cell Data failed: {}", e);
EncodedCellData(None)
Parser(None)
}
}
}