pub struct UndoStack { /* private fields */ }Expand description
A stack of FileSnapshots that supports restoring files to earlier states.
The stack has a configurable maximum depth. When the depth is exceeded, the oldest snapshot is discarded.
§Atomic writes
restore_last and
restore_all write files atomically by first
writing to a temporary file in the same directory, then renaming it.
Implementations§
Source§impl UndoStack
impl UndoStack
Sourcepub async fn snapshot<P>(&mut self, path: P) -> Result<(), UndoError>
pub async fn snapshot<P>(&mut self, path: P) -> Result<(), UndoError>
Capture a snapshot of the file at path.
If the file does not exist, an empty-contents snapshot is recorded (so that restoring will effectively delete the file by writing empty contents, or more precisely, the restore will write the empty string back — matching what was there: nothing).
If the stack is at maximum depth, the oldest snapshot is discarded before the new one is pushed.
§Errors
Returns an error if reading the file fails for a reason other than the file not existing.
Sourcepub async fn restore_last(&mut self) -> Result<FileSnapshot, UndoError>
pub async fn restore_last(&mut self) -> Result<FileSnapshot, UndoError>
Restore the most recent snapshot, removing it from the stack.
The file is written atomically (write to a temp file in the same directory, then rename).
§Errors
Returns UndoError::StackEmpty if there are no snapshots to restore.
Returns UndoError::Io if writing the file fails.
Sourcepub async fn restore_all(&mut self) -> Result<(), UndoError>
pub async fn restore_all(&mut self) -> Result<(), UndoError>
Restore all snapshots in reverse order (most recent first).
Each file is written atomically. The stack is emptied after this call regardless of whether any individual restore fails — snapshots that were successfully restored are not re-pushed.
§Errors
Returns the first error encountered. Snapshots already processed before the error are restored; those after are lost.
Sourcepub fn peek(&self) -> Option<&FileSnapshot>
pub fn peek(&self) -> Option<&FileSnapshot>
Peek at the most recent snapshot without removing or restoring it.
Returns None if the stack is empty.