builder.rs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. use crate::core::{trim, Attributes, Delta};
  2. pub struct DeltaBuilder<T: Attributes> {
  3. delta: Delta<T>,
  4. }
  5. impl<T> std::default::Default for DeltaBuilder<T>
  6. where
  7. T: Attributes,
  8. {
  9. fn default() -> Self { Self { delta: Delta::new() } }
  10. }
  11. impl<T> DeltaBuilder<T>
  12. where
  13. T: Attributes,
  14. {
  15. pub fn new() -> Self { DeltaBuilder::default() }
  16. pub fn retain_with_attributes(mut self, n: usize, attrs: T) -> Self {
  17. self.delta.retain(n, attrs);
  18. self
  19. }
  20. pub fn retain(mut self, n: usize) -> Self {
  21. self.delta.retain(n, T::default());
  22. self
  23. }
  24. pub fn delete(mut self, n: usize) -> Self {
  25. self.delta.delete(n);
  26. self
  27. }
  28. pub fn insert_with_attributes(mut self, s: &str, attrs: T) -> Self {
  29. self.delta.insert(s, attrs);
  30. self
  31. }
  32. pub fn insert(mut self, s: &str) -> Self {
  33. self.delta.insert(s, T::default());
  34. self
  35. }
  36. pub fn trim(mut self) -> Self {
  37. trim(&mut self.delta);
  38. self
  39. }
  40. pub fn build(self) -> Delta<T> { self.delta }
  41. }