pub struct AtomicGuard { /* private fields */ }Expand description
An RAII guard that backs up a file before mutation and restores it on drop unless explicitly committed.
The backup file is named {original}.bak.{epoch_nanos}.{pid} and lives alongside
the original. This mirrors the pattern used by the shell-based atomic helpers
in temp-reference-scripts/lib/atomic.sh.
§Behavior
new(path): Creates a backup copy of the file. If the file does not exist, no backup is created and the guard is a no-op on drop.commit(self): Removes the backup file and consumes the guard so thatDropdoes not run the restore logic.Drop: If the guard was not committed and a backup exists, restores the original file from the backup.
Implementations§
Source§impl AtomicGuard
impl AtomicGuard
Sourcepub fn new(path: &Path) -> Result<Self, SoukError>
pub fn new(path: &Path) -> Result<Self, SoukError>
Creates a new AtomicGuard for the file at path.
If the file exists, a timestamped backup is created immediately. If the file does not exist (e.g., it will be created fresh by the operation), no backup is made and the guard becomes a no-op on drop.
§Errors
Returns SoukError::Io if the file exists but cannot be copied.
Sourcepub fn backup_path(&self) -> Option<&Path>
pub fn backup_path(&self) -> Option<&Path>
Returns the path to the backup file, if one was created.
Sourcepub fn original_path(&self) -> &Path
pub fn original_path(&self) -> &Path
Returns the path to the original file being guarded.
Sourcepub fn commit(self) -> Result<(), SoukError>
pub fn commit(self) -> Result<(), SoukError>
Commits the operation, removing the backup file.
This consumes the guard so that Drop will not attempt to restore
the original file. Call this after the mutation has been verified
as successful.
§Errors
Returns SoukError::Io if the backup file exists but cannot be
removed. Even on error, the guard is marked as committed so that
Drop will not attempt a restore (the mutation already succeeded).