operation.rs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. use crate::core::attributes::Attributes;
  2. use crate::core::document::path::Path;
  3. use crate::core::{NodeBodyChangeset, NodeData};
  4. use crate::errors::OTError;
  5. use serde::{Deserialize, Serialize};
  6. #[derive(Clone, Serialize, Deserialize)]
  7. #[serde(tag = "op")]
  8. pub enum NodeOperation {
  9. #[serde(rename = "insert")]
  10. Insert { path: Path, nodes: Vec<NodeData> },
  11. #[serde(rename = "update")]
  12. UpdateAttributes {
  13. path: Path,
  14. attributes: Attributes,
  15. #[serde(rename = "oldAttributes")]
  16. old_attributes: Attributes,
  17. },
  18. #[serde(rename = "update-body")]
  19. // #[serde(serialize_with = "serialize_edit_body")]
  20. // #[serde(deserialize_with = "deserialize_edit_body")]
  21. UpdateBody { path: Path, changeset: NodeBodyChangeset },
  22. #[serde(rename = "delete")]
  23. Delete { path: Path, nodes: Vec<NodeData> },
  24. }
  25. impl NodeOperation {
  26. pub fn path(&self) -> &Path {
  27. match self {
  28. NodeOperation::Insert { path, .. } => path,
  29. NodeOperation::UpdateAttributes { path, .. } => path,
  30. NodeOperation::Delete { path, .. } => path,
  31. NodeOperation::UpdateBody { path, .. } => path,
  32. }
  33. }
  34. pub fn invert(&self) -> NodeOperation {
  35. match self {
  36. NodeOperation::Insert { path, nodes } => NodeOperation::Delete {
  37. path: path.clone(),
  38. nodes: nodes.clone(),
  39. },
  40. NodeOperation::UpdateAttributes {
  41. path,
  42. attributes,
  43. old_attributes,
  44. } => NodeOperation::UpdateAttributes {
  45. path: path.clone(),
  46. attributes: old_attributes.clone(),
  47. old_attributes: attributes.clone(),
  48. },
  49. NodeOperation::Delete { path, nodes } => NodeOperation::Insert {
  50. path: path.clone(),
  51. nodes: nodes.clone(),
  52. },
  53. NodeOperation::UpdateBody { path, changeset: body } => NodeOperation::UpdateBody {
  54. path: path.clone(),
  55. changeset: body.inverted(),
  56. },
  57. }
  58. }
  59. pub fn clone_with_new_path(&self, path: Path) -> NodeOperation {
  60. match self {
  61. NodeOperation::Insert { nodes, .. } => NodeOperation::Insert {
  62. path,
  63. nodes: nodes.clone(),
  64. },
  65. NodeOperation::UpdateAttributes {
  66. attributes,
  67. old_attributes,
  68. ..
  69. } => NodeOperation::UpdateAttributes {
  70. path,
  71. attributes: attributes.clone(),
  72. old_attributes: old_attributes.clone(),
  73. },
  74. NodeOperation::Delete { nodes, .. } => NodeOperation::Delete {
  75. path,
  76. nodes: nodes.clone(),
  77. },
  78. NodeOperation::UpdateBody { path, changeset } => NodeOperation::UpdateBody {
  79. path: path.clone(),
  80. changeset: changeset.clone(),
  81. },
  82. }
  83. }
  84. pub fn transform(a: &NodeOperation, b: &NodeOperation) -> NodeOperation {
  85. match a {
  86. NodeOperation::Insert { path: a_path, nodes } => {
  87. let new_path = Path::transform(a_path, b.path(), nodes.len() as i64);
  88. b.clone_with_new_path(new_path)
  89. }
  90. NodeOperation::Delete { path: a_path, nodes } => {
  91. let new_path = Path::transform(a_path, b.path(), nodes.len() as i64);
  92. b.clone_with_new_path(new_path)
  93. }
  94. _ => b.clone(),
  95. }
  96. }
  97. }
  98. #[derive(Serialize, Deserialize, Default)]
  99. pub struct NodeOperationList {
  100. operations: Vec<NodeOperation>,
  101. }
  102. impl NodeOperationList {
  103. pub fn into_inner(self) -> Vec<NodeOperation> {
  104. self.operations
  105. }
  106. }
  107. impl std::ops::Deref for NodeOperationList {
  108. type Target = Vec<NodeOperation>;
  109. fn deref(&self) -> &Self::Target {
  110. &self.operations
  111. }
  112. }
  113. impl std::ops::DerefMut for NodeOperationList {
  114. fn deref_mut(&mut self) -> &mut Self::Target {
  115. &mut self.operations
  116. }
  117. }
  118. impl NodeOperationList {
  119. pub fn new(operations: Vec<NodeOperation>) -> Self {
  120. Self { operations }
  121. }
  122. pub fn from_bytes(bytes: Vec<u8>) -> Result<Self, OTError> {
  123. let operation_list = serde_json::from_slice(&bytes).map_err(|err| OTError::serde().context(err))?;
  124. Ok(operation_list)
  125. }
  126. pub fn to_bytes(&self) -> Result<Vec<u8>, OTError> {
  127. let bytes = serde_json::to_vec(self).map_err(|err| OTError::serde().context(err))?;
  128. Ok(bytes)
  129. }
  130. }