162 lines
4.0 KiB
Rust
Raw Normal View History

use crate::entities::FieldType;
2022-04-07 08:33:10 +08:00
use flowy_derive::ProtoBuf;
use flowy_error::ErrorCode;
2022-04-11 15:27:03 +08:00
use flowy_grid_data_model::parser::NotEmptyStr;
use flowy_grid_data_model::revision::{CellRevision, RowChangeset};
use std::collections::HashMap;
2022-04-07 08:33:10 +08:00
#[derive(ProtoBuf, Default)]
2022-07-17 14:13:12 +08:00
pub struct CreateSelectOptionPayloadPB {
2022-04-07 08:33:10 +08:00
#[pb(index = 1)]
2022-07-25 13:15:11 +08:00
pub field_id: String,
2022-04-07 08:33:10 +08:00
#[pb(index = 2)]
2022-07-25 13:15:11 +08:00
pub grid_id: String,
#[pb(index = 3)]
2022-04-07 08:33:10 +08:00
pub option_name: String,
}
pub struct CreateSelectOptionParams {
2022-07-25 13:15:11 +08:00
pub field_id: String,
pub grid_id: String,
2022-04-07 08:33:10 +08:00
pub option_name: String,
}
2022-07-17 14:13:12 +08:00
impl TryInto<CreateSelectOptionParams> for CreateSelectOptionPayloadPB {
2022-04-07 08:33:10 +08:00
type Error = ErrorCode;
fn try_into(self) -> Result<CreateSelectOptionParams, Self::Error> {
let option_name = NotEmptyStr::parse(self.option_name).map_err(|_| ErrorCode::SelectOptionNameIsEmpty)?;
2022-07-25 13:15:11 +08:00
let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
let field_id = NotEmptyStr::parse(self.field_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
2022-04-07 08:33:10 +08:00
Ok(CreateSelectOptionParams {
2022-07-25 13:15:11 +08:00
field_id: field_id.0,
2022-04-07 08:33:10 +08:00
option_name: option_name.0,
2022-07-25 13:15:11 +08:00
grid_id: grid_id.0,
2022-04-07 08:33:10 +08:00
})
}
}
#[derive(Debug, Clone, Default, ProtoBuf)]
2022-07-25 13:15:11 +08:00
pub struct GridCellIdPB {
2022-04-07 08:33:10 +08:00
#[pb(index = 1)]
pub grid_id: String,
#[pb(index = 2)]
pub field_id: String,
#[pb(index = 3)]
pub row_id: String,
}
2022-07-25 13:15:11 +08:00
pub struct GridCellIdParams {
2022-04-07 08:33:10 +08:00
pub grid_id: String,
pub field_id: String,
pub row_id: String,
}
2022-07-25 13:15:11 +08:00
impl TryInto<GridCellIdParams> for GridCellIdPB {
2022-04-07 08:33:10 +08:00
type Error = ErrorCode;
2022-07-25 13:15:11 +08:00
fn try_into(self) -> Result<GridCellIdParams, Self::Error> {
2022-04-11 15:27:03 +08:00
let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
let field_id = NotEmptyStr::parse(self.field_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?;
let row_id = NotEmptyStr::parse(self.row_id).map_err(|_| ErrorCode::RowIdIsEmpty)?;
2022-07-25 13:15:11 +08:00
Ok(GridCellIdParams {
2022-04-07 08:33:10 +08:00
grid_id: grid_id.0,
field_id: field_id.0,
row_id: row_id.0,
})
}
}
#[derive(Debug, Default, ProtoBuf)]
2022-07-17 14:13:12 +08:00
pub struct GridCellPB {
#[pb(index = 1)]
pub field_id: String,
// The data was encoded in field_type's data type
#[pb(index = 2)]
pub data: Vec<u8>,
#[pb(index = 3, one_of)]
pub field_type: Option<FieldType>,
}
2022-07-17 14:13:12 +08:00
impl GridCellPB {
pub fn new(field_id: &str, field_type: FieldType, data: Vec<u8>) -> Self {
Self {
field_id: field_id.to_owned(),
data,
field_type: Some(field_type),
}
}
pub fn empty(field_id: &str) -> Self {
Self {
field_id: field_id.to_owned(),
data: vec![],
field_type: None,
}
}
}
#[derive(Debug, Default, ProtoBuf)]
2022-07-17 14:13:12 +08:00
pub struct RepeatedCellPB {
#[pb(index = 1)]
2022-07-17 14:13:12 +08:00
pub items: Vec<GridCellPB>,
}
2022-07-17 14:13:12 +08:00
impl std::ops::Deref for RepeatedCellPB {
type Target = Vec<GridCellPB>;
fn deref(&self) -> &Self::Target {
&self.items
}
}
2022-07-17 14:13:12 +08:00
impl std::ops::DerefMut for RepeatedCellPB {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.items
}
}
2022-07-17 14:13:12 +08:00
impl std::convert::From<Vec<GridCellPB>> for RepeatedCellPB {
fn from(items: Vec<GridCellPB>) -> Self {
Self { items }
}
}
///
#[derive(Debug, Clone, Default, ProtoBuf)]
2022-07-17 14:13:12 +08:00
pub struct CellChangesetPB {
#[pb(index = 1)]
pub grid_id: String,
#[pb(index = 2)]
pub row_id: String,
#[pb(index = 3)]
pub field_id: String,
#[pb(index = 4)]
pub content: String,
}
impl std::convert::From<CellChangesetPB> for RowChangeset {
2022-07-17 14:13:12 +08:00
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 {
data: changeset.content,
};
cell_by_field_id.insert(field_id, cell_rev);
RowChangeset {
row_id: changeset.row_id,
height: None,
visibility: None,
cell_by_field_id,
}
}
}