builder.rs 950 B

123456789101112131415161718192021222324252627282930313233343536
  1. use crate::core::{Attributes, Operation};
  2. pub struct Builder {
  3. ty: Operation,
  4. attrs: Attributes,
  5. }
  6. impl Builder {
  7. pub fn new(ty: Operation) -> Builder {
  8. Builder {
  9. ty,
  10. attrs: Attributes::default(),
  11. }
  12. }
  13. pub fn retain(n: usize) -> Builder { Builder::new(Operation::Retain(n.into())) }
  14. pub fn delete(n: usize) -> Builder { Builder::new(Operation::Delete(n)) }
  15. pub fn insert(s: &str) -> Builder { Builder::new(Operation::Insert(s.into())) }
  16. pub fn attributes(mut self, attrs: Attributes) -> Builder {
  17. self.attrs = attrs;
  18. self
  19. }
  20. pub fn build(self) -> Operation {
  21. let mut operation = self.ty;
  22. match &mut operation {
  23. Operation::Delete(_) => {},
  24. Operation::Retain(retain) => retain.attributes = self.attrs,
  25. Operation::Insert(insert) => insert.attributes = self.attrs,
  26. }
  27. operation
  28. }
  29. }