operation.rs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. use crate::attributes::Attributes;
  2. use bytecount::num_chars;
  3. use std::{
  4. cmp::Ordering,
  5. collections::{hash_map::RandomState, HashMap},
  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 attrs(&self) -> Option<Attributes> {
  29. match self {
  30. Operation::Delete(_) => None,
  31. Operation::Retain(retain) => retain.attrs.clone(),
  32. Operation::Insert(insert) => insert.attrs.clone(),
  33. }
  34. }
  35. pub fn is_plain(&self) -> bool { self.attrs().is_none() }
  36. }
  37. pub struct OpBuilder {
  38. ty: Operation,
  39. attrs: Option<Attributes>,
  40. }
  41. impl OpBuilder {
  42. pub fn new(ty: Operation) -> OpBuilder { OpBuilder { ty, attrs: None } }
  43. pub fn retain(n: u64) -> OpBuilder { OpBuilder::new(Operation::Retain(n.into())) }
  44. pub fn delete(n: u64) -> OpBuilder { OpBuilder::new(Operation::Delete(n)) }
  45. pub fn insert(s: String) -> OpBuilder { OpBuilder::new(Operation::Insert(s.into())) }
  46. pub fn with_attrs(mut self, attrs: Option<Attributes>) -> OpBuilder {
  47. self.attrs = attrs;
  48. self
  49. }
  50. pub fn build(self) -> Operation {
  51. let mut operation = self.ty;
  52. match &mut operation {
  53. Operation::Delete(_) => {},
  54. Operation::Retain(retain) => retain.attrs = self.attrs,
  55. Operation::Insert(insert) => insert.attrs = self.attrs,
  56. }
  57. operation
  58. }
  59. }
  60. #[derive(Clone, Debug, PartialEq)]
  61. pub struct Retain {
  62. pub n: u64,
  63. pub(crate) attrs: Option<Attributes>,
  64. }
  65. impl std::convert::From<u64> for Retain {
  66. fn from(n: u64) -> Self { Retain { n, attrs: None } }
  67. }
  68. impl Deref for Retain {
  69. type Target = u64;
  70. fn deref(&self) -> &Self::Target { &self.n }
  71. }
  72. impl DerefMut for Retain {
  73. fn deref_mut(&mut self) -> &mut Self::Target { &mut self.n }
  74. }
  75. #[derive(Clone, Debug, PartialEq)]
  76. pub struct Insert {
  77. pub s: String,
  78. pub attrs: Option<Attributes>,
  79. }
  80. impl Insert {
  81. pub fn as_bytes(&self) -> &[u8] { self.s.as_bytes() }
  82. pub fn chars(&self) -> Chars<'_> { self.s.chars() }
  83. pub fn num_chars(&self) -> u64 { num_chars(self.s.as_bytes()) as _ }
  84. }
  85. impl std::convert::From<String> for Insert {
  86. fn from(s: String) -> Self { Insert { s, attrs: None } }
  87. }
  88. impl std::convert::From<&str> for Insert {
  89. fn from(s: &str) -> Self {
  90. Insert {
  91. s: s.to_owned(),
  92. attrs: None,
  93. }
  94. }
  95. }