AppFlowy/frontend/rust-lib/flowy-grid/src/entities/grid_entities.rs

107 lines
2.3 KiB
Rust
Raw Normal View History

2022-07-17 05:38:53 +00:00
use crate::entities::{GridBlockPB, GridFieldPB};
use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
use flowy_error::ErrorCode;
use flowy_grid_data_model::parser::NotEmptyStr;
#[derive(Debug, Clone, Default, ProtoBuf)]
2022-07-17 05:38:53 +00:00
pub struct GridPB {
#[pb(index = 1)]
pub id: String,
#[pb(index = 2)]
2022-07-17 05:38:53 +00:00
pub fields: Vec<GridFieldPB>,
#[pb(index = 3)]
2022-07-17 05:38:53 +00:00
pub blocks: Vec<GridBlockPB>,
}
#[derive(ProtoBuf, Default)]
2022-07-17 05:38:53 +00:00
pub struct CreateGridPayloadPB {
#[pb(index = 1)]
pub name: String,
}
#[derive(Clone, ProtoBuf, Default, Debug)]
2022-07-17 05:38:53 +00:00
pub struct GridIdPB {
#[pb(index = 1)]
pub value: String,
}
2022-07-17 05:38:53 +00:00
impl AsRef<str> for GridIdPB {
fn as_ref(&self) -> &str {
&self.value
}
}
#[derive(Clone, ProtoBuf, Default, Debug)]
2022-07-17 05:38:53 +00:00
pub struct GridBlockIdPB {
#[pb(index = 1)]
pub value: String,
}
2022-07-17 05:38:53 +00:00
impl AsRef<str> for GridBlockIdPB {
fn as_ref(&self) -> &str {
&self.value
}
}
2022-07-17 05:38:53 +00:00
impl std::convert::From<&str> for GridBlockIdPB {
fn from(s: &str) -> Self {
2022-07-17 05:38:53 +00:00
GridBlockIdPB { value: s.to_owned() }
}
}
#[derive(Debug, Clone, ProtoBuf_Enum)]
2022-07-17 05:38:53 +00:00
pub enum MoveItemTypePB {
MoveField = 0,
MoveRow = 1,
}
2022-07-17 05:38:53 +00:00
impl std::default::Default for MoveItemTypePB {
fn default() -> Self {
2022-07-17 05:38:53 +00:00
MoveItemTypePB::MoveField
}
}
#[derive(Debug, Clone, Default, ProtoBuf)]
2022-07-17 05:38:53 +00:00
pub struct MoveItemPayloadPB {
#[pb(index = 1)]
pub grid_id: String,
#[pb(index = 2)]
pub item_id: String,
#[pb(index = 3)]
pub from_index: i32,
#[pb(index = 4)]
pub to_index: i32,
#[pb(index = 5)]
2022-07-17 05:38:53 +00:00
pub ty: MoveItemTypePB,
}
#[derive(Clone)]
pub struct MoveItemParams {
pub grid_id: String,
pub item_id: String,
pub from_index: i32,
pub to_index: i32,
2022-07-17 05:38:53 +00:00
pub ty: MoveItemTypePB,
}
2022-07-17 05:38:53 +00:00
impl TryInto<MoveItemParams> for MoveItemPayloadPB {
type Error = ErrorCode;
fn try_into(self) -> Result<MoveItemParams, Self::Error> {
let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
let item_id = NotEmptyStr::parse(self.item_id).map_err(|_| ErrorCode::InvalidData)?;
Ok(MoveItemParams {
grid_id: grid_id.0,
item_id: item_id.0,
from_index: self.from_index,
to_index: self.to_index,
ty: self.ty,
})
}
}