file_operation/file/read/async.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
use std::path::Path;
use tokio::fs::*;
use tokio::io::AsyncReadExt;
/// Reads the content of a file at the specified `file_path` and converts it to the type `T`.
/// The conversion is done by using the `From<Vec<u8>>` trait, which allows the file content
/// (read as raw bytes) to be converted into a type `T`.
///
/// # Parameters
/// - `file_path`: The path to the file that will be read.
///
/// # Returns
/// - `Result<T, Box<dyn std::error::Error>>`:
/// - `Ok(T)`: The file was read successfully and converted to the specified type `T`.
/// - `Err(Box<dyn std::error::Error>)`: An error occurred while opening or reading the file.
/// This will contain any errors encountered during the file operations, including I/O or conversion errors.
///
/// # Errors
/// - This function may return an error if:
/// - The file at `file_path` does not exist.
/// - There is an I/O error while reading the file.
/// - The content of the file cannot be converted to type `T`.
///
/// # Notes
/// - The function assumes that the content of the file can be represented as a byte vector (`Vec<u8>`),
/// which is then passed to `T::from` for conversion.
#[inline]
pub async fn async_read_from_file<T>(file_path: &str) -> Result<T, Box<dyn std::error::Error>>
where
T: From<Vec<u8>>,
{
let path: &Path = Path::new(file_path);
let mut file: File = File::open(path).await?;
let mut content: Vec<u8> = Vec::new();
file.read_to_end(&mut content).await?;
Ok(T::from(content))
}
/// Retrieves the size of a file at the specified `file_path` in bytes.
///
/// # Parameters
/// - `file_path`: The path to the file whose size is to be fetched.
///
/// # Returns
/// - `Option<u64>`:
/// - `Some(size)`: The size of the file in bytes if the file exists and its metadata can be accessed.
/// - `None`: If the file doesn't exist, or there is an error retrieving the file's metadata.
///
/// # Errors
/// - If the file at `file_path` doesn't exist, or there is an issue accessing its metadata,
/// the function will return `None` to indicate the failure.
///
/// # Notes
/// - The function uses `metadata` to retrieve the file's metadata and specifically its size (`metadata.len()`).
/// If the file metadata is not accessible or the file does not exist, the function will return `None`.
/// This is useful when you want to safely handle cases where the file might not be present or readable.
#[inline]
pub async fn async_get_file_size(file_path: &str) -> Option<u64> {
metadata(file_path)
.await
.and_then(|metadata| Ok(Some(metadata.len())))
.unwrap_or(None)
}