use crate::entities::parser::NotEmptyStr; use crate::entities::FieldType; use flowy_derive::ProtoBuf; use flowy_error::ErrorCode; use grid_model::{CellRevision, RowChangeset}; use std::collections::HashMap; #[derive(ProtoBuf, Default)] pub struct CreateSelectOptionPayloadPB { #[pb(index = 1)] pub field_id: String, #[pb(index = 2)] pub database_id: String, #[pb(index = 3)] pub option_name: String, } pub struct CreateSelectOptionParams { pub field_id: String, pub database_id: String, pub option_name: String, } impl TryInto for CreateSelectOptionPayloadPB { type Error = ErrorCode; fn try_into(self) -> Result { let option_name = NotEmptyStr::parse(self.option_name).map_err(|_| ErrorCode::SelectOptionNameIsEmpty)?; let database_id = NotEmptyStr::parse(self.database_id).map_err(|_| ErrorCode::DatabaseIdIsEmpty)?; let field_id = NotEmptyStr::parse(self.field_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?; Ok(CreateSelectOptionParams { field_id: field_id.0, option_name: option_name.0, database_id: database_id.0, }) } } #[derive(Debug, Clone, Default, ProtoBuf)] pub struct CellIdPB { #[pb(index = 1)] pub database_id: String, #[pb(index = 2)] pub field_id: String, #[pb(index = 3)] pub row_id: String, } /// Represents as the cell identifier. It's used to locate the cell in corresponding /// view's row with the field id. pub struct CellIdParams { pub database_id: String, pub field_id: String, pub row_id: String, } impl TryInto for CellIdPB { type Error = ErrorCode; fn try_into(self) -> Result { let database_id = NotEmptyStr::parse(self.database_id).map_err(|_| ErrorCode::DatabaseIdIsEmpty)?; let field_id = NotEmptyStr::parse(self.field_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?; let row_id = NotEmptyStr::parse(self.row_id).map_err(|_| ErrorCode::RowIdIsEmpty)?; Ok(CellIdParams { database_id: database_id.0, field_id: field_id.0, row_id: row_id.0, }) } } /// Represents as the data of the cell. #[derive(Debug, Default, ProtoBuf)] pub struct CellPB { #[pb(index = 1)] pub field_id: String, #[pb(index = 2)] pub row_id: String, /// Encoded the data using the helper struct `CellProtobufBlob`. /// Check out the `CellProtobufBlob` for more information. #[pb(index = 3)] pub data: Vec, /// the field_type will be None if the field with field_id is not found #[pb(index = 4, one_of)] pub field_type: Option, } impl CellPB { pub fn new(field_id: &str, row_id: &str, field_type: FieldType, data: Vec) -> Self { Self { field_id: field_id.to_owned(), row_id: row_id.to_string(), data, field_type: Some(field_type), } } pub fn empty(field_id: &str, row_id: &str) -> Self { Self { field_id: field_id.to_owned(), row_id: row_id.to_owned(), data: vec![], field_type: None, } } } #[derive(Debug, Default, ProtoBuf)] pub struct RepeatedCellPB { #[pb(index = 1)] pub items: Vec, } impl std::ops::Deref for RepeatedCellPB { type Target = Vec; fn deref(&self) -> &Self::Target { &self.items } } impl std::ops::DerefMut for RepeatedCellPB { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.items } } impl std::convert::From> for RepeatedCellPB { fn from(items: Vec) -> Self { Self { items } } } /// #[derive(Debug, Clone, Default, ProtoBuf)] pub struct CellChangesetPB { #[pb(index = 1)] pub database_id: String, #[pb(index = 2)] pub row_id: String, #[pb(index = 3)] pub field_id: String, #[pb(index = 4)] pub type_cell_data: String, } impl std::convert::From for RowChangeset { fn from(changeset: CellChangesetPB) -> Self { let mut cell_by_field_id = HashMap::with_capacity(1); let field_id = changeset.field_id; let cell_rev = CellRevision { type_cell_data: changeset.type_cell_data, }; cell_by_field_id.insert(field_id, cell_rev); RowChangeset { row_id: changeset.row_id, height: None, visibility: None, cell_by_field_id, } } }