builder.rs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. use crate::core::delta::operation::{EmptyAttributes, Operation, OperationAttributes};
  2. // pub type RichTextOpBuilder = OperationsBuilder<TextAttributes>;
  3. pub type PlainTextOpBuilder = OperationsBuilder<EmptyAttributes>;
  4. #[derive(Default)]
  5. pub struct OperationsBuilder<T: OperationAttributes> {
  6. operations: Vec<Operation<T>>,
  7. }
  8. impl<T> OperationsBuilder<T>
  9. where
  10. T: OperationAttributes,
  11. {
  12. pub fn new() -> OperationsBuilder<T> {
  13. OperationsBuilder::default()
  14. }
  15. pub fn retain_with_attributes(mut self, n: usize, attributes: T) -> OperationsBuilder<T> {
  16. let retain = Operation::retain_with_attributes(n, attributes);
  17. self.operations.push(retain);
  18. self
  19. }
  20. pub fn retain(mut self, n: usize) -> OperationsBuilder<T> {
  21. let retain = Operation::retain(n);
  22. self.operations.push(retain);
  23. self
  24. }
  25. pub fn delete(mut self, n: usize) -> OperationsBuilder<T> {
  26. self.operations.push(Operation::Delete(n));
  27. self
  28. }
  29. pub fn insert_with_attributes(mut self, s: &str, attributes: T) -> OperationsBuilder<T> {
  30. let insert = Operation::insert_with_attributes(s, attributes);
  31. self.operations.push(insert);
  32. self
  33. }
  34. pub fn insert(mut self, s: &str) -> OperationsBuilder<T> {
  35. let insert = Operation::insert(s);
  36. self.operations.push(insert);
  37. self
  38. }
  39. pub fn build(self) -> Vec<Operation<T>> {
  40. self.operations
  41. }
  42. }