operation.rs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. use crate::core::{Attributes, OpBuilder};
  2. use bytecount::num_chars;
  3. use std::{
  4. fmt,
  5. ops::{Deref, DerefMut},
  6. str::Chars,
  7. };
  8. #[derive(Debug, Clone, PartialEq)]
  9. pub enum Operation {
  10. Delete(u64),
  11. Retain(Retain),
  12. Insert(Insert),
  13. }
  14. impl Operation {
  15. pub fn is_delete(&self) -> bool {
  16. match self {
  17. Operation::Delete(_) => true,
  18. _ => false,
  19. }
  20. }
  21. pub fn is_noop(&self) -> bool {
  22. match self {
  23. Operation::Retain(_) => true,
  24. _ => false,
  25. }
  26. }
  27. pub fn get_attributes(&self) -> Attributes {
  28. match self {
  29. Operation::Delete(_) => Attributes::Empty,
  30. Operation::Retain(retain) => retain.attributes.clone(),
  31. Operation::Insert(insert) => insert.attributes.clone(),
  32. }
  33. }
  34. pub fn set_attributes(&mut self, attributes: Attributes) {
  35. match self {
  36. Operation::Delete(_) => {
  37. log::error!("Delete should not contains attributes");
  38. },
  39. Operation::Retain(retain) => {
  40. retain.attributes = attributes;
  41. },
  42. Operation::Insert(insert) => {
  43. insert.attributes = attributes;
  44. },
  45. }
  46. }
  47. pub fn has_attribute(&self) -> bool {
  48. match self.get_attributes() {
  49. Attributes::Follow => true,
  50. Attributes::Custom(_) => false,
  51. Attributes::Empty => true,
  52. }
  53. }
  54. pub fn length(&self) -> u64 {
  55. match self {
  56. Operation::Delete(n) => *n,
  57. Operation::Retain(r) => r.n,
  58. Operation::Insert(i) => i.num_chars(),
  59. }
  60. }
  61. pub fn is_empty(&self) -> bool { self.length() == 0 }
  62. }
  63. impl fmt::Display for Operation {
  64. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  65. match self {
  66. Operation::Delete(n) => {
  67. f.write_fmt(format_args!("delete: {}", n))?;
  68. },
  69. Operation::Retain(r) => {
  70. f.write_fmt(format_args!(
  71. "retain: {}, attributes: {}",
  72. r.n, r.attributes
  73. ))?;
  74. },
  75. Operation::Insert(i) => {
  76. f.write_fmt(format_args!(
  77. "insert: {}, attributes: {}",
  78. i.s, i.attributes
  79. ))?;
  80. },
  81. }
  82. Ok(())
  83. }
  84. }
  85. #[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
  86. pub struct Retain {
  87. #[serde(rename(serialize = "retain", deserialize = "retain"))]
  88. pub n: u64,
  89. #[serde(skip_serializing_if = "is_empty")]
  90. pub attributes: Attributes,
  91. }
  92. impl Retain {
  93. pub fn merge_or_new_op(&mut self, n: u64, attributes: Attributes) -> Option<Operation> {
  94. log::debug!(
  95. "merge_retain_or_new_op: {:?}, {:?}",
  96. self.attributes,
  97. attributes
  98. );
  99. match &attributes {
  100. Attributes::Follow => {
  101. log::debug!("Follow attribute: {:?}", self.attributes);
  102. self.n += n;
  103. None
  104. },
  105. Attributes::Custom(_) | Attributes::Empty => {
  106. if self.attributes == attributes {
  107. log::debug!("Attribute equal");
  108. self.n += n;
  109. None
  110. } else {
  111. log::debug!("New retain op");
  112. Some(OpBuilder::retain(n).attributes(attributes).build())
  113. }
  114. },
  115. }
  116. }
  117. }
  118. impl std::convert::From<u64> for Retain {
  119. fn from(n: u64) -> Self {
  120. Retain {
  121. n,
  122. attributes: Attributes::default(),
  123. }
  124. }
  125. }
  126. impl Deref for Retain {
  127. type Target = u64;
  128. fn deref(&self) -> &Self::Target { &self.n }
  129. }
  130. impl DerefMut for Retain {
  131. fn deref_mut(&mut self) -> &mut Self::Target { &mut self.n }
  132. }
  133. #[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
  134. pub struct Insert {
  135. #[serde(rename(serialize = "insert", deserialize = "insert"))]
  136. pub s: String,
  137. #[serde(skip_serializing_if = "is_empty")]
  138. pub attributes: Attributes,
  139. }
  140. impl Insert {
  141. pub fn as_bytes(&self) -> &[u8] { self.s.as_bytes() }
  142. pub fn chars(&self) -> Chars<'_> { self.s.chars() }
  143. pub fn num_chars(&self) -> u64 { num_chars(self.s.as_bytes()) as _ }
  144. pub fn merge_or_new_op(&mut self, s: &str, attributes: Attributes) -> Option<Operation> {
  145. match &attributes {
  146. Attributes::Follow => {
  147. self.s += s;
  148. return None;
  149. },
  150. Attributes::Custom(_) | Attributes::Empty => {
  151. if self.attributes == attributes {
  152. self.s += s;
  153. None
  154. } else {
  155. Some(OpBuilder::insert(s).attributes(attributes).build())
  156. }
  157. },
  158. }
  159. }
  160. }
  161. impl std::convert::From<String> for Insert {
  162. fn from(s: String) -> Self {
  163. Insert {
  164. s,
  165. attributes: Attributes::default(),
  166. }
  167. }
  168. }
  169. impl std::convert::From<&str> for Insert {
  170. fn from(s: &str) -> Self { Insert::from(s.to_owned()) }
  171. }
  172. fn is_empty(attributes: &Attributes) -> bool { attributes.is_empty() }