FileManager

Struct FileManager 

Source
pub struct FileManager { /* private fields */ }
Expand description

Safe file operation manager with rollback capabilities.

The FileManager provides atomic file operations with automatic rollback functionality. It tracks all file creations and deletions, allowing complete operation reversal in case of errors.

§Use Cases

  • Batch file operations that need to be atomic
  • Temporary file creation during processing
  • Safe file replacement with backup

§Examples

use subx_cli::core::file_manager::FileManager;
use std::path::Path;

let mut manager = FileManager::new();
// Create a new file (tracked for rollback)
manager.record_creation(Path::new("output.srt"));
// Remove an existing file (backed up for rollback)
manager.remove_file(Path::new("old_file.srt")).unwrap();
// If something goes wrong, rollback all operations
manager.rollback().unwrap();

§Safety

The manager ensures that:

  • Created files are properly removed on rollback
  • Removed files are backed up and restored on rollback
  • No partial state is left after rollback completion

Implementations§

Source§

impl FileManager

Source

pub fn new() -> Self

Creates a new FileManager with an empty operation history.

The new manager starts with no tracked operations and is ready to begin recording file operations for potential rollback.

§Examples
use subx_cli::core::file_manager::FileManager;

let manager = FileManager::new();
Source

pub fn record_creation<P: AsRef<Path>>(&mut self, path: P)

Records the creation of a file for potential rollback.

This method should be called after successfully creating a file that may need to be removed if a rollback is performed. The file is not immediately affected, only tracked for future rollback.

§Arguments
  • path: Path to the created file
§Examples
use subx_cli::core::file_manager::FileManager;
use std::path::Path;

let mut manager = FileManager::new();

// After creating a file...
manager.record_creation(Path::new("output.srt"));

// File will be removed if rollback() is called
Source

pub fn remove_file<P: AsRef<Path>>(&mut self, path: P) -> Result<()>

Safely removes a file and tracks the operation for rollback.

The file is backed up before removal, allowing it to be restored if a rollback is performed. The backup is created with a .bak extension in the same directory as the original file.

§Arguments
  • path: Path to the file to remove
§Returns

Returns Ok(()) if the file was successfully removed and backed up, or an error if the file doesn’t exist or removal fails.

§Errors
§Examples
Source

pub fn rollback(&mut self) -> Result<()>

Rolls back all recorded operations in reverse execution order.

This method undoes all file operations that have been recorded, restoring the filesystem to its state before any operations were performed. Operations are reversed in LIFO order to maintain consistency.

§Rollback Behavior
  • Created files: Removed from the filesystem
  • Removed files: Restored from backup (if backup was created)
§Returns

Returns Ok(()) if all rollback operations succeed, or the first error encountered during rollback.

§Errors

Returns SubXError::FileOperationFailed if any rollback operation fails. Note that partial rollback may occur if some operations succeed before an error is encountered.

§Examples
use subx_cli::core::file_manager::FileManager;

let mut manager = FileManager::new();
// ... perform some file operations ...

// Rollback all operations
manager.rollback()?;
Source

pub fn operation_count(&self) -> usize

Returns the number of operations currently tracked.

This can be useful for testing or monitoring the state of the file manager.

§Examples
use subx_cli::core::file_manager::FileManager;

let manager = FileManager::new();
assert_eq!(manager.operation_count(), 0);

Trait Implementations§

Source§

impl Default for FileManager

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,