diff --git a/wallitor-gui/src-tauri/src/main.rs b/wallitor-gui/src-tauri/src/main.rs index 9a4a6aa..f77fb56 100644 --- a/wallitor-gui/src-tauri/src/main.rs +++ b/wallitor-gui/src-tauri/src/main.rs @@ -2,6 +2,11 @@ #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] mod setup; +mod reader; + +use std::fs; +use serde_json; +use std::path::Path; fn main() { tauri::Builder::default() @@ -15,4 +20,12 @@ fn main() { #[tauri::command] async fn test_command() -> String { return String::from("Hello World"); +} + +#[tauri::command] +async fn read_resource_dir() -> String { + let mut file_map = reader::FileMap::new(); + let path = Path::new("./resource"); + fs::exists(path).is_err_and(|_|{fs::create_dir(path);return true;}); + serde_json::to_string(&file_map).unwrap() } \ No newline at end of file diff --git a/wallitor-gui/src-tauri/src/reader.rs b/wallitor-gui/src-tauri/src/reader.rs new file mode 100644 index 0000000..15134f9 --- /dev/null +++ b/wallitor-gui/src-tauri/src/reader.rs @@ -0,0 +1,59 @@ +use std::fs; +use std::path::Path; +use serde::{ Serialize,Serializer}; +use serde_json; +use std::collections::HashMap; + +enum FileType { + File(String), + Directory(HashMap) +} + +impl Serialize for FileType { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + match self { + FileType::File(file) => serializer.serialize_str(file), // 文件只序列化路径 + FileType::Directory(dir) => dir.serialize(serializer), // 目录序列化为HashMap + } + } +} + +#[derive(Serialize)] +pub struct FileMap { + files: HashMap, // 用于存储文件名和路径 +} + +impl FileMap { + pub fn new()->FileMap{ + FileMap{ + files:HashMap::new() + } + } + + fn read_resource(&self,p:&Path)->std::io::Result>{ + let mut files:HashMap = HashMap::new(); + for entry in fs::read_dir(p)?{ + let dir = entry?; + let path = dir.path(); + if path.is_file() { + files.insert(path.file_name().unwrap().to_string_lossy().to_string(), FileType::File(path.to_string_lossy().to_string())); + } + } + Ok(files) + } + + fn read_resourse_directory(&mut self,p:&Path)->std::io::Result<()>{ + for entry in fs::read_dir(p)?{ + let dir = entry?; + let path = dir.path(); + if path.is_dir() { + let resource = self.read_resource(path.as_path())?; + self.files.insert(path.to_string_lossy().to_string(), FileType::Directory(resource)) ; + } + } + Ok(()) + } +}