Common/FileSystem/
WriteFileBytes.rs

1//! # WriteFileBytes Effect
2//!
3//! Defines the `ActionEffect` for writing raw byte content to a file.
4
5use std::{path::PathBuf, sync::Arc};
6
7use super::FileSystemWriter::FileSystemWriter;
8use crate::{Effect::ActionEffect::ActionEffect, Error::CommonError::CommonError};
9
10/// Creates an effect that, when executed, will write a vector of bytes to a
11/// file at the specified path.
12///
13/// It uses the `FileSystemWriter` capability from the environment to perform
14/// the actual file I/O.
15///
16/// # Parameters
17/// * `Path`: The `PathBuf` of the file to write to.
18/// * `Content`: The `Vec<u8>` content to be written.
19/// * `Create`: If `true`, the file will be created if it does not exist.
20/// * `Overwrite`: If `true`, an existing file will be overwritten.
21///
22/// # Returns
23/// An `ActionEffect` that resolves to `()` on success.
24pub fn WriteFileBytes(
25	Path:PathBuf,
26
27	Content:Vec<u8>,
28
29	Create:bool,
30
31	Overwrite:bool,
32) -> ActionEffect<Arc<dyn FileSystemWriter>, CommonError, ()> {
33	ActionEffect::New(Arc::new(move |Writer:Arc<dyn FileSystemWriter>| {
34		let PathClone = Path.clone();
35
36		let ContentClone = Content.clone();
37
38		Box::pin(async move { Writer.WriteFile(&PathClone, ContentClone, Create, Overwrite).await })
39	}))
40}