Skip to main content

Mountain/IPC/WindServiceAdapters/
WindFileService.rs

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