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

229 lines
4.9 KiB
Rust
Raw Normal View History

2022-07-01 16:10:13 +00:00
use crate::entities::GridRowId;
use flowy_derive::ProtoBuf;
use flowy_error::ErrorCode;
use flowy_grid_data_model::parser::NotEmptyStr;
use flowy_grid_data_model::revision::RowRevision;
use std::sync::Arc;
#[derive(Debug, Clone, Default, ProtoBuf)]
pub struct GridBlock {
#[pb(index = 1)]
pub id: String,
#[pb(index = 2)]
2022-07-01 16:10:13 +00:00
pub row_infos: Vec<BlockRowInfo>,
}
impl GridBlock {
2022-07-01 16:10:13 +00:00
pub fn new(block_id: &str, row_orders: Vec<BlockRowInfo>) -> Self {
Self {
id: block_id.to_owned(),
2022-07-01 16:10:13 +00:00
row_infos: row_orders,
}
}
}
#[derive(Debug, Default, Clone, ProtoBuf)]
2022-07-01 16:10:13 +00:00
pub struct BlockRowInfo {
#[pb(index = 1)]
2022-07-01 16:10:13 +00:00
pub block_id: String,
#[pb(index = 2)]
2022-07-01 16:10:13 +00:00
pub row_id: String,
#[pb(index = 3)]
pub height: i32,
}
2022-07-01 16:10:13 +00:00
impl BlockRowInfo {
pub fn row_id(&self) -> &str {
&self.row_id
}
pub fn block_id(&self) -> &str {
&self.block_id
}
}
impl std::convert::From<&RowRevision> for BlockRowInfo {
fn from(rev: &RowRevision) -> Self {
Self {
2022-07-01 16:10:13 +00:00
block_id: rev.block_id.clone(),
row_id: rev.id.clone(),
height: rev.height,
}
}
}
2022-07-01 16:10:13 +00:00
impl std::convert::From<&Arc<RowRevision>> for BlockRowInfo {
fn from(rev: &Arc<RowRevision>) -> Self {
Self {
2022-07-01 16:10:13 +00:00
block_id: rev.block_id.clone(),
row_id: rev.id.clone(),
height: rev.height,
}
}
}
#[derive(Debug, Default, ProtoBuf)]
pub struct Row {
#[pb(index = 1)]
pub id: String,
#[pb(index = 2)]
pub height: i32,
}
#[derive(Debug, Default, ProtoBuf)]
pub struct OptionalRow {
#[pb(index = 1, one_of)]
pub row: Option<Row>,
}
#[derive(Debug, Default, ProtoBuf)]
pub struct RepeatedRow {
#[pb(index = 1)]
pub items: Vec<Row>,
}
impl std::convert::From<Vec<Row>> for RepeatedRow {
fn from(items: Vec<Row>) -> Self {
Self { items }
}
}
#[derive(Debug, Default, ProtoBuf)]
pub struct RepeatedGridBlock {
#[pb(index = 1)]
pub items: Vec<GridBlock>,
}
impl std::convert::From<Vec<GridBlock>> for RepeatedGridBlock {
fn from(items: Vec<GridBlock>) -> Self {
Self { items }
}
}
#[derive(Debug, Clone, Default, ProtoBuf)]
2022-07-04 07:24:40 +00:00
pub struct InsertedRow {
#[pb(index = 1)]
2022-07-04 07:24:40 +00:00
pub block_id: String,
#[pb(index = 2)]
pub row_id: String,
#[pb(index = 3)]
pub height: i32,
2022-07-04 07:24:40 +00:00
#[pb(index = 4, one_of)]
pub index: Option<i32>,
}
#[derive(Debug, Default, ProtoBuf)]
2022-07-04 07:24:40 +00:00
pub struct UpdatedRow {
#[pb(index = 1)]
2022-07-04 07:24:40 +00:00
pub block_id: String,
#[pb(index = 2)]
2022-07-04 07:24:40 +00:00
pub row_id: String,
#[pb(index = 3)]
pub row: Row,
}
2022-07-04 07:24:40 +00:00
impl UpdatedRow {
pub fn new(row_rev: &RowRevision, row: Row) -> Self {
Self {
2022-07-04 07:24:40 +00:00
row_id: row_rev.id.clone(),
block_id: row_rev.block_id.clone(),
row,
}
}
}
2022-07-04 07:24:40 +00:00
impl std::convert::From<BlockRowInfo> for InsertedRow {
2022-07-01 16:10:13 +00:00
fn from(row_info: BlockRowInfo) -> Self {
2022-07-04 07:24:40 +00:00
Self {
row_id: row_info.row_id,
block_id: row_info.block_id,
height: row_info.height,
index: None,
}
}
}
2022-07-04 07:24:40 +00:00
impl std::convert::From<&RowRevision> for InsertedRow {
fn from(row: &RowRevision) -> Self {
2022-07-01 16:10:13 +00:00
let row_order = BlockRowInfo::from(row);
Self::from(row_order)
}
}
#[derive(Debug, Default, ProtoBuf)]
2022-07-04 07:24:40 +00:00
pub struct GridBlockChangeset {
#[pb(index = 1)]
pub block_id: String,
#[pb(index = 2)]
2022-07-04 07:24:40 +00:00
pub inserted_rows: Vec<InsertedRow>,
#[pb(index = 3)]
2022-07-01 16:10:13 +00:00
pub deleted_rows: Vec<GridRowId>,
#[pb(index = 4)]
2022-07-04 07:24:40 +00:00
pub updated_rows: Vec<UpdatedRow>,
}
2022-07-04 07:24:40 +00:00
impl GridBlockChangeset {
pub fn insert(block_id: &str, inserted_rows: Vec<InsertedRow>) -> Self {
Self {
block_id: block_id.to_owned(),
inserted_rows,
deleted_rows: vec![],
updated_rows: vec![],
}
}
2022-07-01 16:10:13 +00:00
pub fn delete(block_id: &str, deleted_rows: Vec<GridRowId>) -> Self {
Self {
block_id: block_id.to_owned(),
inserted_rows: vec![],
deleted_rows,
updated_rows: vec![],
}
}
2022-07-04 07:24:40 +00:00
pub fn update(block_id: &str, updated_rows: Vec<UpdatedRow>) -> Self {
Self {
block_id: block_id.to_owned(),
inserted_rows: vec![],
deleted_rows: vec![],
updated_rows,
}
}
}
#[derive(ProtoBuf, Default)]
pub struct QueryGridBlocksPayload {
#[pb(index = 1)]
pub grid_id: String,
#[pb(index = 2)]
pub block_ids: Vec<String>,
}
pub struct QueryGridBlocksParams {
pub grid_id: String,
pub block_ids: Vec<String>,
}
impl TryInto<QueryGridBlocksParams> for QueryGridBlocksPayload {
type Error = ErrorCode;
fn try_into(self) -> Result<QueryGridBlocksParams, Self::Error> {
let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
Ok(QueryGridBlocksParams {
grid_id: grid_id.0,
block_ids: self.block_ids,
})
}
}