115 lines
3.0 KiB
Rust
Raw Normal View History

2022-08-11 10:08:42 +08:00
use crate::entities::FieldType;
2022-06-21 16:56:50 +08:00
use flowy_derive::ProtoBuf;
use flowy_error::ErrorCode;
use flowy_grid_data_model::parser::NotEmptyStr;
use flowy_grid_data_model::revision::GridGroupRevision;
2022-08-11 10:08:42 +08:00
use flowy_sync::entities::grid::{CreateGridGroupParams, DeleteGroupParams};
2022-06-21 16:56:50 +08:00
use std::convert::TryInto;
2022-07-10 17:06:36 +08:00
use std::sync::Arc;
2022-06-21 16:56:50 +08:00
#[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
2022-07-17 13:38:53 +08:00
pub struct GridGroupPB {
2022-06-21 16:56:50 +08:00
#[pb(index = 1)]
pub id: String,
2022-08-11 10:08:42 +08:00
#[pb(index = 2)]
pub group_field_id: String,
2022-06-21 16:56:50 +08:00
#[pb(index = 3, one_of)]
pub sub_group_field_id: Option<String>,
}
2022-07-17 13:38:53 +08:00
impl std::convert::From<&GridGroupRevision> for GridGroupPB {
2022-06-22 17:11:56 +08:00
fn from(rev: &GridGroupRevision) -> Self {
2022-07-17 13:38:53 +08:00
GridGroupPB {
2022-06-22 17:11:56 +08:00
id: rev.id.clone(),
group_field_id: rev.field_id.clone(),
sub_group_field_id: rev.sub_field_id.clone(),
2022-06-21 16:56:50 +08:00
}
}
}
#[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
2022-07-17 13:38:53 +08:00
pub struct RepeatedGridGroupPB {
2022-06-21 16:56:50 +08:00
#[pb(index = 1)]
2022-07-17 13:38:53 +08:00
pub items: Vec<GridGroupPB>,
2022-06-21 16:56:50 +08:00
}
2022-07-17 13:38:53 +08:00
impl std::convert::From<Vec<GridGroupPB>> for RepeatedGridGroupPB {
fn from(items: Vec<GridGroupPB>) -> Self {
2022-06-21 16:56:50 +08:00
Self { items }
}
}
2022-07-17 13:38:53 +08:00
impl std::convert::From<Vec<Arc<GridGroupRevision>>> for RepeatedGridGroupPB {
2022-07-10 17:06:36 +08:00
fn from(revs: Vec<Arc<GridGroupRevision>>) -> Self {
2022-07-17 13:38:53 +08:00
RepeatedGridGroupPB {
2022-07-10 17:06:36 +08:00
items: revs.iter().map(|rev| rev.as_ref().into()).collect(),
2022-06-21 16:56:50 +08:00
}
}
}
#[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
2022-07-17 13:38:53 +08:00
pub struct CreateGridGroupPayloadPB {
2022-08-11 10:08:42 +08:00
#[pb(index = 1)]
pub field_id: String,
2022-06-21 16:56:50 +08:00
#[pb(index = 2, one_of)]
pub sub_field_id: Option<String>,
2022-08-11 10:08:42 +08:00
#[pb(index = 3)]
pub field_type: FieldType,
2022-06-21 16:56:50 +08:00
}
2022-07-17 13:38:53 +08:00
impl TryInto<CreateGridGroupParams> for CreateGridGroupPayloadPB {
2022-06-21 16:56:50 +08:00
type Error = ErrorCode;
fn try_into(self) -> Result<CreateGridGroupParams, Self::Error> {
2022-08-11 10:08:42 +08:00
let field_id = NotEmptyStr::parse(self.field_id)
.map_err(|_| ErrorCode::FieldIdIsEmpty)?
.0;
2022-06-21 16:56:50 +08:00
let sub_field_id = match self.sub_field_id {
None => None,
Some(field_id) => Some(NotEmptyStr::parse(field_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?.0),
};
2022-08-11 10:08:42 +08:00
Ok(CreateGridGroupParams {
field_id,
sub_field_id,
field_type_rev: self.field_type.into(),
})
}
}
#[derive(ProtoBuf, Debug, Default, Clone)]
pub struct DeleteGroupPayloadPB {
#[pb(index = 1)]
pub field_id: String,
#[pb(index = 2)]
pub group_id: String,
#[pb(index = 3)]
pub field_type: FieldType,
}
impl TryInto<DeleteGroupParams> for DeleteGroupPayloadPB {
type Error = ErrorCode;
fn try_into(self) -> Result<DeleteGroupParams, Self::Error> {
let field_id = NotEmptyStr::parse(self.field_id)
.map_err(|_| ErrorCode::FieldIdIsEmpty)?
.0;
let group_id = NotEmptyStr::parse(self.group_id)
.map_err(|_| ErrorCode::FieldIdIsEmpty)?
.0;
Ok(DeleteGroupParams {
field_id,
field_type_rev: self.field_type.into(),
group_id,
})
2022-06-21 16:56:50 +08:00
}
}