pub struct Guard {
Path: PathBuf,
Store: PathBuf,
Active: bool,
Note: String,
}Expand description
Manages the backup and restoration of a single file using the RAII pattern.
This struct ensures that an original file is restored to its initial state when the Guard goes out of scope, providing a safe way to temporarily modify configuration files during the build process.
§RAII Pattern
The Guard implements the RAII (Resource Acquisition Is Initialization) pattern:
- Acquisition: When created, it creates a backup of the original file
- Release: When dropped, it restores the original file from the backup
This ensures that files are restored even if:
- The function returns early
- An error occurs and propagates up
- A panic occurs
§Backup Naming
Backup files are created by appending a suffix to the original file extension:
Cargo.toml→Cargo.toml.Backuptauri.conf.json→tauri.conf.json.Backup
§Error Handling
The Guard constructor returns an error if:
- A backup file already exists at the target location
- The original file cannot be copied (IO error)
This prevents accidental overwriting of existing backups and ensures data safety.
§Fields
- Path: The path to the original file
- Store: The path to the backup file
- Active: Whether a backup was created
- Note: A descriptive note for logging purposes
Fields§
§Path: PathBufThe path to the original file that will be modified.
Store: PathBufThe path to the backup file created by this guard.
Active: boolWhether a backup was actually created (false if the original file didn’t exist).
Note: StringA descriptive note for logging and debugging purposes.
Implementations§
Source§impl Guard
impl Guard
Sourcepub fn New(OriginalPath: PathBuf, Description: String) -> Result<Self, Error>
pub fn New(OriginalPath: PathBuf, Description: String) -> Result<Self, Error>
Creates a new Guard that backs up the specified file.
This constructor creates a backup of the original file if it exists, and prepares to restore it when the Guard is dropped. The backup file is created with a special suffix to prevent accidental conflicts.
§Parameters
OriginalPath- The path to the file to be backed upDescription- A descriptive note for logging purposes
§Returns
Returns a Result containing the Guard or a BuildError if:
- A backup file already exists
- The file cannot be copied
§Errors
BuildError::Exists- If a backup file already existsBuildError::Io- If the file copy operation fails
§Example
use crate::Maintain::Source::Build::Definition;
let path = PathBuf::from("Cargo.toml");
let guard = Guard::New(path, "Cargo manifest".to_string())?;§Safety
The Guard ensures that the original file is restored even in the presence of panics, providing exception safety.
Sourcepub fn Path(&self) -> &Path
pub fn Path(&self) -> &Path
Returns a reference to the original file path.
This method provides read access to the path of the original file that this guard is protecting.
§Returns
A reference to the original file’s Path.
§Example
use crate::Maintain::Source::Build::Definition;
let guard = Guard::New(original_path, description.to_string())?;
println!("Original file: {}", guard.Path().display());Sourcepub fn Store(&self) -> &Path
pub fn Store(&self) -> &Path
Returns a reference to the backup file path.
This method provides read access to the path where the backup file is stored.
§Returns
A reference to the backup file’s Path.
§Example
use crate::Maintain::Source::Build::Definition;
let guard = Guard::New(original_path, description.to_string())?;
println!("Backup file: {}", guard.Store().display());Trait Implementations§
Source§impl Drop for Guard
Drop implementation that automatically restores the original file.
impl Drop for Guard
Drop implementation that automatically restores the original file.
This is the core of the RAII pattern: when the Guard goes out of scope, it automatically restores the original file from the backup. This ensures that file modifications are temporary and that the system is left in a consistent state even if an error occurs.
§Behavior
- If no backup was created (original file didn’t exist), does nothing
- If backup exists, copies it back to the original location
- After successful restore, deletes the backup file
- Logs success or failure of the restoration process
§Panics
This implementation handles panics internally and does not propagate them. If the restoration fails, it logs an error but does not panic, ensuring that cleanup failures don’t cause secondary failures.