operation.rs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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(usize),
  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) -> usize {
  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(n) => {
  66. //
  67. OpBuilder::delete(min(*n, interval.size())).build()
  68. },
  69. Operation::Retain(retain) => {
  70. //
  71. OpBuilder::retain(min(retain.n, interval.size()))
  72. .attributes(retain.attributes.clone())
  73. .build()
  74. },
  75. Operation::Insert(insert) => {
  76. // debug_assert!(insert.s.len() <= interval.size());
  77. if interval.start > insert.s.len() {
  78. return OpBuilder::insert("").build();
  79. }
  80. let end = min(interval.end, insert.s.len());
  81. let s = &insert.s[interval.start..end];
  82. OpBuilder::insert(s)
  83. .attributes(insert.attributes.clone())
  84. .build()
  85. },
  86. }
  87. }
  88. }
  89. impl fmt::Display for Operation {
  90. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  91. match self {
  92. Operation::Delete(n) => {
  93. f.write_fmt(format_args!("delete: {}", n))?;
  94. },
  95. Operation::Retain(r) => {
  96. f.write_fmt(format_args!(
  97. "retain: {}, attributes: {}",
  98. r.n, r.attributes
  99. ))?;
  100. },
  101. Operation::Insert(i) => {
  102. f.write_fmt(format_args!(
  103. "insert: {}, attributes: {}",
  104. i.s, i.attributes
  105. ))?;
  106. },
  107. }
  108. Ok(())
  109. }
  110. }
  111. #[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
  112. pub struct Retain {
  113. #[serde(rename(serialize = "retain", deserialize = "retain"))]
  114. pub n: usize,
  115. #[serde(skip_serializing_if = "is_empty")]
  116. pub attributes: Attributes,
  117. }
  118. impl Retain {
  119. pub fn merge_or_new_op(&mut self, n: usize, attributes: Attributes) -> Option<Operation> {
  120. log::debug!(
  121. "merge_retain_or_new_op: {:?}, {:?}",
  122. self.attributes,
  123. attributes
  124. );
  125. match &attributes {
  126. Attributes::Follow => {
  127. log::debug!("Follow attribute: {:?}", self.attributes);
  128. self.n += n;
  129. None
  130. },
  131. Attributes::Custom(_) | Attributes::Empty => {
  132. if self.attributes == attributes {
  133. log::debug!("Attribute equal");
  134. self.n += n;
  135. None
  136. } else {
  137. log::debug!("New retain op");
  138. Some(OpBuilder::retain(n).attributes(attributes).build())
  139. }
  140. },
  141. }
  142. }
  143. }
  144. impl std::convert::From<usize> for Retain {
  145. fn from(n: usize) -> Self {
  146. Retain {
  147. n,
  148. attributes: Attributes::default(),
  149. }
  150. }
  151. }
  152. impl Deref for Retain {
  153. type Target = usize;
  154. fn deref(&self) -> &Self::Target { &self.n }
  155. }
  156. impl DerefMut for Retain {
  157. fn deref_mut(&mut self) -> &mut Self::Target { &mut self.n }
  158. }
  159. #[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize)]
  160. pub struct Insert {
  161. #[serde(rename(serialize = "insert", deserialize = "insert"))]
  162. pub s: String,
  163. #[serde(skip_serializing_if = "is_empty")]
  164. pub attributes: Attributes,
  165. }
  166. impl Insert {
  167. pub fn as_bytes(&self) -> &[u8] { self.s.as_bytes() }
  168. pub fn chars(&self) -> Chars<'_> { self.s.chars() }
  169. pub fn num_chars(&self) -> usize { num_chars(self.s.as_bytes()) as _ }
  170. pub fn merge_or_new_op(&mut self, s: &str, attributes: Attributes) -> Option<Operation> {
  171. match &attributes {
  172. Attributes::Follow => {
  173. self.s += s;
  174. return None;
  175. },
  176. Attributes::Custom(_) | Attributes::Empty => {
  177. if self.attributes == attributes {
  178. self.s += s;
  179. None
  180. } else {
  181. Some(OpBuilder::insert(s).attributes(attributes).build())
  182. }
  183. },
  184. }
  185. }
  186. }
  187. impl std::convert::From<String> for Insert {
  188. fn from(s: String) -> Self {
  189. Insert {
  190. s,
  191. attributes: Attributes::default(),
  192. }
  193. }
  194. }
  195. impl std::convert::From<&str> for Insert {
  196. fn from(s: &str) -> Self { Insert::from(s.to_owned()) }
  197. }
  198. fn is_empty(attributes: &Attributes) -> bool { attributes.is_empty() }