Guard

Struct Guard 

Source
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.tomlCargo.toml.Backup
  • tauri.conf.jsontauri.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: PathBuf

The path to the original file that will be modified.

§Store: PathBuf

The path to the backup file created by this guard.

§Active: bool

Whether a backup was actually created (false if the original file didn’t exist).

§Note: String

A descriptive note for logging and debugging purposes.

Implementations§

Source§

impl Guard

Source

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 up
  • Description - 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 exists
  • BuildError::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.

Source

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());
Source

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.

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.

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl Freeze for Guard

§

impl RefUnwindSafe for Guard

§

impl Send for Guard

§

impl Sync for Guard

§

impl Unpin for Guard

§

impl UnwindSafe for Guard

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.