1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
use {Command, Error, Merger}; /// A stack of commands. /// /// The `Stack` is the simplest data structure and works by pushing and /// popping off `Command`s that modifies the `receiver`. /// Unlike the `Record`, it does not have a special state that can be used for callbacks. /// /// # Examples /// ``` /// use std::error::Error; /// use undo::{Command, Stack}; /// /// struct Add(char); /// /// impl Command<String> for Add { /// fn redo(&mut self, s: &mut String) -> Result<(), Box<Error>> { /// s.push(self.0); /// Ok(()) /// } /// /// fn undo(&mut self, s: &mut String) -> Result<(), Box<Error>> { /// self.0 = s.pop().expect("`String` is unexpectedly empty"); /// Ok(()) /// } /// } /// /// fn foo() -> Result<(), Box<Error>> { /// let mut stack = Stack::default(); /// /// stack.push(Add('a'))?; /// stack.push(Add('b'))?; /// stack.push(Add('c'))?; /// /// assert_eq!(stack.as_receiver(), "abc"); /// /// let c = stack.pop().unwrap()?; /// let b = stack.pop().unwrap()?; /// let a = stack.pop().unwrap()?; /// /// assert_eq!(stack.as_receiver(), ""); /// /// stack.push(a)?; /// stack.push(b)?; /// stack.push(c)?; /// /// assert_eq!(stack.into_receiver(), "abc"); /// /// Ok(()) /// } /// # foo().unwrap(); /// ``` #[derive(Debug, Default)] pub struct Stack<R> { commands: Vec<Box<Command<R>>>, receiver: R, } impl<R> Stack<R> { /// Creates a new `Stack`. #[inline] pub fn new<T: Into<R>>(receiver: T) -> Stack<R> { Stack { commands: Vec::new(), receiver: receiver.into(), } } /// Creates a new `Stack` with the given `capacity`. #[inline] pub fn with_capacity<T: Into<R>>(receiver: T, capacity: usize) -> Stack<R> { Stack { commands: Vec::with_capacity(capacity), receiver: receiver.into(), } } /// Returns the capacity of the `Stack`. #[inline] pub fn capacity(&self) -> usize { self.commands.capacity() } /// Returns the number of `Command`s in the `Stack`. #[inline] pub fn len(&self) -> usize { self.commands.len() } /// Returns `true` if the `Stack` is empty. #[inline] pub fn is_empty(&self) -> bool { self.commands.is_empty() } /// Returns a reference to the `receiver`. #[inline] pub fn as_receiver(&self) -> &R { &self.receiver } /// Consumes the `Stack`, returning the `receiver`. #[inline] pub fn into_receiver(self) -> R { self.receiver } /// Pushes `cmd` on the stack and executes its [`redo`] method. The command is merged with /// the previous top `Command` if their `id` is equal. /// /// # Errors /// If an error occur when executing `redo`, the error is returned together with the `Command`. /// /// [`redo`]: ../trait.Command.html#tymethod.redo #[inline] pub fn push<C>(&mut self, mut cmd: C) -> Result<(), Error<R>> where C: Command<R> + 'static, R: 'static, { if let Err(e) = cmd.redo(&mut self.receiver) { return Err(Error(Box::new(cmd), e)); } match (cmd.id(), self.commands.last().and_then(|last| last.id())) { (Some(id1), Some(id2)) if id1 == id2 => { // Merge the command with the one on the top of the stack. let cmd = Merger { cmd1: self.commands.pop().unwrap(), cmd2: Box::new(cmd), }; self.commands.push(Box::new(cmd)); } _ => self.commands.push(Box::new(cmd)), } Ok(()) } /// Calls the top commands [`undo`] method and pops it off the stack. /// Returns `None` if the stack is empty. /// /// # Errors /// If an error occur when executing `undo` the error is returned together with the `Command`. /// /// [`undo`]: ../trait.Command.html#tymethod.undo #[inline] pub fn pop(&mut self) -> Option<Result<Box<Command<R>>, Error<R>>> { let mut cmd = match self.commands.pop() { Some(cmd) => cmd, None => return None, }; match cmd.undo(&mut self.receiver) { Ok(_) => Some(Ok(cmd)), Err(e) => Some(Err(Error(cmd, e))), } } } impl<R> AsRef<R> for Stack<R> { #[inline] fn as_ref(&self) -> &R { self.as_receiver() } }