grid_entities.rs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. use crate::entities::{BlockPB, FieldIdPB};
  2. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  3. use flowy_error::ErrorCode;
  4. use flowy_grid_data_model::parser::NotEmptyStr;
  5. /// [GridPB] describes how many fields and blocks the grid has
  6. #[derive(Debug, Clone, Default, ProtoBuf)]
  7. pub struct GridPB {
  8. #[pb(index = 1)]
  9. pub id: String,
  10. #[pb(index = 2)]
  11. pub fields: Vec<FieldIdPB>,
  12. #[pb(index = 3)]
  13. pub blocks: Vec<BlockPB>,
  14. }
  15. #[derive(ProtoBuf, Default)]
  16. pub struct CreateGridPayloadPB {
  17. #[pb(index = 1)]
  18. pub name: String,
  19. }
  20. #[derive(Clone, ProtoBuf, Default, Debug)]
  21. pub struct GridIdPB {
  22. #[pb(index = 1)]
  23. pub value: String,
  24. }
  25. impl AsRef<str> for GridIdPB {
  26. fn as_ref(&self) -> &str {
  27. &self.value
  28. }
  29. }
  30. #[derive(Clone, ProtoBuf, Default, Debug)]
  31. pub struct GridBlockIdPB {
  32. #[pb(index = 1)]
  33. pub value: String,
  34. }
  35. impl AsRef<str> for GridBlockIdPB {
  36. fn as_ref(&self) -> &str {
  37. &self.value
  38. }
  39. }
  40. impl std::convert::From<&str> for GridBlockIdPB {
  41. fn from(s: &str) -> Self {
  42. GridBlockIdPB { value: s.to_owned() }
  43. }
  44. }
  45. #[derive(Debug, Clone, ProtoBuf_Enum)]
  46. pub enum MoveItemTypePB {
  47. MoveField = 0,
  48. MoveRow = 1,
  49. }
  50. impl std::default::Default for MoveItemTypePB {
  51. fn default() -> Self {
  52. MoveItemTypePB::MoveField
  53. }
  54. }
  55. #[derive(Debug, Clone, Default, ProtoBuf)]
  56. pub struct MoveItemPayloadPB {
  57. #[pb(index = 1)]
  58. pub grid_id: String,
  59. #[pb(index = 2)]
  60. pub item_id: String,
  61. #[pb(index = 3)]
  62. pub from_index: i32,
  63. #[pb(index = 4)]
  64. pub to_index: i32,
  65. #[pb(index = 5)]
  66. pub ty: MoveItemTypePB,
  67. }
  68. #[derive(Clone)]
  69. pub struct MoveItemParams {
  70. pub grid_id: String,
  71. pub item_id: String,
  72. pub from_index: i32,
  73. pub to_index: i32,
  74. pub ty: MoveItemTypePB,
  75. }
  76. impl TryInto<MoveItemParams> for MoveItemPayloadPB {
  77. type Error = ErrorCode;
  78. fn try_into(self) -> Result<MoveItemParams, Self::Error> {
  79. let grid_id = NotEmptyStr::parse(self.grid_id).map_err(|_| ErrorCode::GridIdIsEmpty)?;
  80. let item_id = NotEmptyStr::parse(self.item_id).map_err(|_| ErrorCode::InvalidData)?;
  81. Ok(MoveItemParams {
  82. grid_id: grid_id.0,
  83. item_id: item_id.0,
  84. from_index: self.from_index,
  85. to_index: self.to_index,
  86. ty: self.ty,
  87. })
  88. }
  89. }