document_pad.rs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. use crate::{client_document::InitialDocumentText, errors::CollaborateError, synchronizer::RevisionSyncObject};
  2. use lib_ot::{core::*, text_delta::TextDelta};
  3. pub struct ServerDocument {
  4. doc_id: String,
  5. delta: TextDelta,
  6. }
  7. impl ServerDocument {
  8. #[allow(dead_code)]
  9. pub fn new<C: InitialDocumentText>(doc_id: &str) -> Self {
  10. Self::from_delta(doc_id, C::initial_delta())
  11. }
  12. pub fn from_delta(doc_id: &str, delta: TextDelta) -> Self {
  13. let doc_id = doc_id.to_owned();
  14. ServerDocument { doc_id, delta }
  15. }
  16. }
  17. impl RevisionSyncObject<Attributes> for ServerDocument {
  18. fn id(&self) -> &str {
  19. &self.doc_id
  20. }
  21. fn compose(&mut self, other: &TextDelta) -> Result<(), CollaborateError> {
  22. // tracing::trace!("{} compose {}", &self.delta.to_json(), other.to_json());
  23. let new_delta = self.delta.compose(other)?;
  24. self.delta = new_delta;
  25. Ok(())
  26. }
  27. fn transform(&self, other: &TextDelta) -> Result<(TextDelta, TextDelta), CollaborateError> {
  28. let value = self.delta.transform(other)?;
  29. Ok(value)
  30. }
  31. fn to_json(&self) -> String {
  32. self.delta.json_str()
  33. }
  34. fn set_delta(&mut self, new_delta: Operations<Attributes>) {
  35. self.delta = new_delta;
  36. }
  37. }