file loader

master
cast1e 2024-09-27 17:04:12 +08:00
parent 9ce56708cd
commit 08b61c9779
2 changed files with 72 additions and 0 deletions

View File

@ -2,6 +2,11 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
mod setup; mod setup;
mod reader;
use std::fs;
use serde_json;
use std::path::Path;
fn main() { fn main() {
tauri::Builder::default() tauri::Builder::default()
@ -15,4 +20,12 @@ fn main() {
#[tauri::command] #[tauri::command]
async fn test_command() -> String { async fn test_command() -> String {
return String::from("Hello World"); 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()
} }

View File

@ -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<String,FileType>)
}
impl Serialize for FileType {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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<String, FileType>, // 用于存储文件名和路径
}
impl FileMap {
pub fn new()->FileMap{
FileMap{
files:HashMap::new()
}
}
fn read_resource(&self,p:&Path)->std::io::Result<HashMap<String,FileType>>{
let mut files:HashMap<String,FileType> = 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(())
}
}