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
17	pub(super) writer:Arc<dyn FileSystemWriter>,
18}
19
20impl Struct {
21	pub fn new(reader:Arc<dyn FileSystemReader>, writer:Arc<dyn FileSystemWriter>) -> Self { Self { reader, writer } }
22
23	pub async fn read_file(&self, path:String) -> Result<Vec<u8>, String> {
24		self.reader.ReadFile(&PathBuf::from(path)).await.map_err(|e| e.to_string())
25	}
26
27	pub async fn write_file(&self, path:String, content:Vec<u8>) -> Result<(), String> {
28		self.writer
29			.WriteFile(&PathBuf::from(path), content, true, true)
30			.await
31			.map_err(|e:CommonError| e.to_string())
32	}
33
34	pub async fn stat_file(&self, path:String) -> Result<serde_json::Value, String> {
35		let stat_dto = self
36			.reader
37			.StatFile(&PathBuf::from(path))
38			.await
39			.map_err(|e:CommonError| e.to_string())?;
40
41		Ok(json!(stat_dto))
42	}
43}