file_operation/file/write/sync.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
use std::fs::{create_dir_all, OpenOptions};
use std::io::{Error, Write};
/// Writes the provided content to a file at the specified `file_path`.
/// If the file does not exist, it will be created. If the file exists, the content will be appended to it.
///
/// # Parameters
/// - `file_path`: The path to the file where the content will be written.
/// - `content`: A byte slice (`&[u8]`) containing the content to be written to the file.
///
/// # Returns
/// - `Result<(), Error>`:
/// - `Ok(())`: If the content was successfully written to the file.
/// - `Err(Error)`: If there was an error during file creation or writing.
///
/// # Errors
/// - If the file cannot be created or opened for writing, an error will be returned. This can happen if:
/// - There is a problem with the file path (e.g., invalid or inaccessible path).
/// - There are I/O issues when writing to the file.
#[inline]
pub fn write_to_file(file_path: &str, content: &[u8]) -> Result<(), Error> {
if let Some(parent_dir) = std::path::Path::new(file_path).parent() {
create_dir_all(parent_dir)?;
}
OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(file_path)
.and_then(|mut file| file.write_all(content))
}
/// Append the provided content to a file at the specified `file_path`.
/// If the file does not exist, it will be created. If the file exists, the content will be appended to it.
///
/// # Parameters
/// - `file_path`: The path to the file where the content will be written.
/// - `content`: A byte slice (`&[u8]`) containing the content to be written to the file.
///
/// # Returns
/// - `Result<(), Error>`:
/// - `Ok(())`: If the content was successfully written to the file.
/// - `Err(Error)`: If there was an error during file creation or writing.
///
/// # Errors
/// - If the file cannot be created or opened for writing, an error will be returned. This can happen if:
/// - There is a problem with the file path (e.g., invalid or inaccessible path).
/// - There are I/O issues when writing to the file.
#[inline]
pub fn append_to_file(file_path: &str, content: &[u8]) -> Result<(), Error> {
if let Some(parent_dir) = std::path::Path::new(file_path).parent() {
create_dir_all(parent_dir)?;
}
OpenOptions::new()
.write(true)
.create(true)
.append(true)
.open(file_path)
.and_then(|mut file| file.write_all(content))
}