A Rust library providing comprehensive utilities for file operations with both sync/async support. Includes operations for copy, delete, move, read and write files. Simplifies file handling in Rust projects with safe and efficient methods for file manipulation and metadata querying.
To use this crate, you can run cmd:
cargo add file-operationCode
let _ = write_to_file(FILE_PATH, "test".as_bytes());Description
Writes the given data ("test".as_bytes()) to the file specified by FILE_PATH.
FILE_PATH- Path to the target file.- Returns - A
Resultindicating success or failure.
Code
let res: Vec<u8> = read_from_file(FILE_PATH).unwrap_or_default();Description
Reads the contents of the file specified by FILE_PATH.
FILE_PATH- Path to the target file.- Returns - A
Vec<u8>containing the file content or an empty vector on failure.
Code
let size: Option<u64> = get_file_size(FILE_PATH);Description
Retrieves the size of the file specified by FILE_PATH.
FILE_PATH- Path to the target file.- Returns - An
Option<u64>containing the file size in bytes orNoneif the file does not exist.
Code
let res: Result<(), std::io::Error> = copy_dir_files(FILE_DIR, NEW_FILE_DIR);Description
Copies all files from FILE_DIR to NEW_FILE_DIR.
FILE_DIR- Source directory path.NEW_FILE_DIR- Destination directory path.- Returns - A
Resultindicating success or failure.
Code
let res: Result<(), std::io::Error> = delete_file(FILE_PATH);Description
Deletes the file specified by FILE_PATH.
FILE_PATH- Path to the target file.- Returns - A
Resultindicating success or failure.
Code
let res: Result<(), std::io::Error> = move_dir(FILE_DIR, NEW_TEST_DIR);Description
Moves the directory specified by FILE_DIR to NEW_TEST_DIR.
FILE_DIR- Source directory path.NEW_TEST_DIR- Destination directory path.- Returns - A
Resultindicating success or failure.
Code
let res: Result<(), std::io::Error> = delete_dir(NEW_TEST_DIR);Description
Deletes the directory specified by NEW_TEST_DIR.
NEW_TEST_DIR- Path to the target directory.- Returns - A
Resultindicating success or failure.
Code
let _ = async_write_to_file(FILE_PATH, "test".as_bytes()).await;Description
Writes the given data ("test".as_bytes()) to the file specified by FILE_PATH asynchronously.
FILE_PATH- Path to the target file.- Returns - A
Resultindicating success or failure.
Code
let res: Vec<u8> = async_read_from_file(FILE_PATH).await.unwrap_or_default();Description
Reads the contents of the file specified by FILE_PATH asynchronously.
FILE_PATH- Path to the target file.- Returns - A
Vec<u8>containing the file content or an empty vector on failure.
Code
let size: Option<u64> = async_get_file_size(FILE_PATH).await;Description
Retrieves the size of the file specified by FILE_PATH asynchronously.
FILE_PATH- Path to the target file.- Returns - An
Option<u64>containing the file size in bytes orNoneif the file does not exist.
Code
let res: Result<(), std::io::Error> = async_copy_dir_files(FILE_DIR, NEW_FILE_DIR).await;Description
Copies all files from FILE_DIR to NEW_FILE_DIR asynchronously.
FILE_DIR- Source directory path.NEW_FILE_DIR- Destination directory path.- Returns - A
Resultindicating success or failure.
Code
let res: Result<(), std::io::Error> = async_delete_file(FILE_PATH).await;Description
Deletes the file specified by FILE_PATH asynchronously.
FILE_PATH- Path to the target file.- Returns - A
Resultindicating success or failure.
Code
let res: Result<(), std::io::Error> = async_move_dir(FILE_DIR, NEW_TEST_DIR).await;Description
Moves the directory specified by FILE_DIR to NEW_TEST_DIR asynchronously.
FILE_DIR- Source directory path.NEW_TEST_DIR- Destination directory path.- Returns - A
Resultindicating success or failure.
Code
let res: Result<(), std::io::Error> = async_delete_dir(NEW_TEST_DIR).await;Description
Deletes the directory specified by NEW_TEST_DIR asynchronously.
NEW_TEST_DIR- Path to the target directory.- Returns - A
Resultindicating success or failure.
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please open an issue or submit a pull request.
For any inquiries, please reach out to the author at [email protected].