DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/IPC/WindServiceAdapters/
WindFileService.rs1use std::{path::PathBuf, sync::Arc};
5
6use CommonLibrary::{
7 Error::CommonError::CommonError,
8 FileSystem::{FileSystemReader::FileSystemReader, FileSystemWriter::FileSystemWriter},
9};
10use serde_json::json;
11
12pub struct Struct {
13 pub(super) reader:Arc<dyn FileSystemReader>,
14
15 pub(super) writer:Arc<dyn FileSystemWriter>,
16}
17
18impl Struct {
19 pub fn new(reader:Arc<dyn FileSystemReader>, writer:Arc<dyn FileSystemWriter>) -> Self { Self { reader, writer } }
20
21 pub async fn read_file(&self, path:String) -> Result<Vec<u8>, String> {
22 self.reader.ReadFile(&PathBuf::from(path)).await.map_err(|e| e.to_string())
23 }
24
25 pub async fn write_file(&self, path:String, content:Vec<u8>) -> Result<(), String> {
26 self.writer
27 .WriteFile(&PathBuf::from(path), content, true, true)
28 .await
29 .map_err(|e:CommonError| e.to_string())
30 }
31
32 pub async fn stat_file(&self, path:String) -> Result<serde_json::Value, String> {
33 let stat_dto = self
34 .reader
35 .StatFile(&PathBuf::from(path))
36 .await
37 .map_err(|e:CommonError| e.to_string())?;
38
39 Ok(json!(stat_dto))
40 }
41}