sort_entities.rs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. use flowy_derive::ProtoBuf;
  2. use flowy_error::ErrorCode;
  3. use flowy_grid_data_model::parser::NotEmptyStr;
  4. use flowy_grid_data_model::revision::GridSortRevision;
  5. use flowy_sync::entities::grid::CreateGridSortParams;
  6. use std::convert::TryInto;
  7. use std::sync::Arc;
  8. #[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
  9. pub struct GridSort {
  10. #[pb(index = 1)]
  11. pub id: String,
  12. #[pb(index = 2, one_of)]
  13. pub field_id: Option<String>,
  14. }
  15. impl std::convert::From<&GridSortRevision> for GridSort {
  16. fn from(rev: &GridSortRevision) -> Self {
  17. GridSort {
  18. id: rev.id.clone(),
  19. field_id: rev.field_id.clone(),
  20. }
  21. }
  22. }
  23. #[derive(Eq, PartialEq, ProtoBuf, Debug, Default, Clone)]
  24. pub struct RepeatedGridSortPB {
  25. #[pb(index = 1)]
  26. pub items: Vec<GridSort>,
  27. }
  28. impl std::convert::From<Vec<Arc<GridSortRevision>>> for RepeatedGridSortPB {
  29. fn from(revs: Vec<Arc<GridSortRevision>>) -> Self {
  30. RepeatedGridSortPB {
  31. items: revs.into_iter().map(|rev| rev.as_ref().into()).collect(),
  32. }
  33. }
  34. }
  35. impl std::convert::From<Vec<GridSort>> for RepeatedGridSortPB {
  36. fn from(items: Vec<GridSort>) -> Self {
  37. Self { items }
  38. }
  39. }
  40. #[derive(ProtoBuf, Debug, Default, Clone)]
  41. pub struct CreateGridSortPayloadPB {
  42. #[pb(index = 1, one_of)]
  43. pub field_id: Option<String>,
  44. }
  45. impl TryInto<CreateGridSortParams> for CreateGridSortPayloadPB {
  46. type Error = ErrorCode;
  47. fn try_into(self) -> Result<CreateGridSortParams, Self::Error> {
  48. let field_id = match self.field_id {
  49. None => None,
  50. Some(field_id) => Some(NotEmptyStr::parse(field_id).map_err(|_| ErrorCode::FieldIdIsEmpty)?.0),
  51. };
  52. Ok(CreateGridSortParams { field_id })
  53. }
  54. }