Skip to main content

Mountain/IPC/WindServiceAdapters/
WindFileService.rs

1#![allow(non_snake_case)]
2
3//! Wind-shaped file service: read / write / stat over the
4//! injected `FileSystemReader` / `FileSystemWriter` traits.
5
6use std::{path::PathBuf, sync::Arc};
7
8use CommonLibrary::{
9	Error::CommonError::CommonError,
10	FileSystem::{FileSystemReader::FileSystemReader, FileSystemWriter::FileSystemWriter},
11};
12use serde_json::json;
13
14pub struct Struct {
15	pub(super) reader:Arc<dyn FileSystemReader>,
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		Ok(json!(stat_dto))
40	}
41}