operation.rs 6.1 KB

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