Skip to main content

Crate tfio

Crate tfio 

Source
Expand description

TFIO is a library that provides a Transaction-like interface traditionally used in databases, applied to FileIO operations. It gives the flexibility to execute and rollback singular operations as well as transactions on the fly. The library also provides a builder-pattern interface to chain operations and execute them in one go.

§Examples

Create a transaction and execute it. If any Error is encountered, rollback the entire transaction:

use std::io;
use tfio::*;

fn main() -> io::Result<()> {
    let mut tr = Transaction::new()
        .create_file("./foo.txt")
        .create_dir("./bar")
        .write_file("./foo.txt", b"Hello World".to_vec())
        .move_file("./foo.txt", "./bar/foo.txt")
        .append_file("./bar/foo.txt", b"dlroW olleH".to_vec());

    // Execute the transaction
    if let Err(e) = tr.execute() {
        eprintln!("Error during execution: {}", e);

        // All operations can be reverted in reverse-order if `Error` is encountered
        if let Err(ee) = tr.rollback() {
            panic!("Error during transaction rollback: {}", ee);
        }
    }
    Ok(())
}

You can also import single operations to use:

use std::io;
use std::fs;
use tfio::{CopyFile, RollbackableOperation};

fn main() -> io::Result<()> {
    fs::File::create("./foo.txt")?;
    fs::create_dir_all("./bar/baz")?;

    let mut copy_operation = CopyFile::new("./foo.txt", "./bar/baz/foo.txt");

    // Execute the operation
    if let Err(e) = copy_operation.execute() {
        eprintln!("Error during execution: {}", e);

        // Rollback the operation
        if let Err(ee) = copy_operation.rollback() {
            panic!("Error during rollback: {}", ee);
        }
    }
    Ok(())
}

Structs§

AppendFile
Appends data to a file.
CopyDirectory
Copies a directory to the destination.
CopyFile
Copies a file to the destination.
CreateDirectory
Creates a new directory (and any missing parent directories).
CreateFile
Creates a new file. Fails if the file already exists.
DeleteDirectory
Deletes a directory (backed up so it can be restored on rollback).
DeleteFile
Deletes a file (backed up so it can be restored on rollback).
MoveOperation
Move operation (works for both files and directories).
TouchFile
Creates a file if it does not exist, or updates its access and modification times to now
Transaction
A rollbackable transaction that executes a sequence of RollbackableOperations.
WriteFile
Writes data to a file, replacing its existing contents.

Traits§

DirectoryOperation
Trait that represents a directory operation.
RollbackableOperation
Trait that represents a rollbackable operation.
SingleFileOperation
Trait that represents a single-file operation.

Type Aliases§

MoveDirectory
Moves a directory from source to destination. Type alias for MoveOperation for API consistency.
MoveFile
Moves a file from source to destination. Type alias for MoveOperation for API consistency.