Expand description
TFIO is a library that provides a Transaction-like interface that are traditionally used in databases on FileIO operations. It gives the flexibility to execute and rollback singlular 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:
Note: The paths should only use forward slashes (/
) and can either begin with disks or relative to the present working directory ie. begin with ./
use std::io;
use tfio::*;
fn main() -> io::Result<()> {
let temp_dir = "./PATH_TO_TEMP_DIR";
let mut tr = Transaction::new()
.create_file("./foo.txt")
.create_dir("./bar")
.write_file("./foo.txt", temp_dir, b"Hello World".to_vec())
.move_file("./foo.txt", "./bar/foo.txt")
.append_file("./bar/foo.txt", temp_dir, 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§
- Append
File - Appends data to a file
- Copy
Directory - Copies a directory to destination
- Copy
File - Copies a file to destination
- Create
Directory - Creates a new directory
- Create
File - Creates a new file
- Delete
Directory - Deletes a directory
- Delete
File - Deletes a file
- Move
Operation - Move operation
- Transaction
- A rollbackable Transaction
- Write
File - Writes data to a file
Traits§
- Directory
Operation - Trait that represents a Directory operation
- Rollbackable
Operation - Trait that represents a Rollbackable operation
- Single
File Operation - Trait that represents a single file operation
Type Aliases§
- Move
Directory - Moves a directory from source to destination. A type alias for MoveOperation for consistency in the API
- Move
File - Moves a file from source to destination. A type alias for MoveOperation for consistency in the API