generated from avior/template-web-astro
83 lines
2.2 KiB
Rust
83 lines
2.2 KiB
Rust
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
|
|
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
|
|
|
use std::fs;
|
|
|
|
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
|
|
#[tauri::command]
|
|
fn greet(name: &str) -> String {
|
|
format!("Hello, {}! You've been greeted from Rust!", name)
|
|
}
|
|
|
|
#[derive(serde::Serialize)]
|
|
struct File {
|
|
filename: String,
|
|
}
|
|
|
|
#[derive(serde::Serialize)]
|
|
struct FileList {
|
|
path: String,
|
|
list: Vec<File>,
|
|
}
|
|
|
|
#[tauri::command]
|
|
fn list_files(path: &str) -> Result<FileList, String> {
|
|
let run = || -> Result<Vec<File>, ()> {
|
|
let paths = fs::read_dir(path).unwrap();
|
|
let mut final_string: String = String::new();
|
|
let mut arr = Vec::new();
|
|
for (idx, path) in paths.enumerate() {
|
|
if idx > 0 {
|
|
final_string.push(',');
|
|
}
|
|
arr.push(File {
|
|
filename: path.unwrap().path().display().to_string(),
|
|
});
|
|
// final_string.push_str(path.unwrap().path().display().to_string().as_str())
|
|
}
|
|
Ok(arr)
|
|
};
|
|
let list = run();
|
|
|
|
if list.is_err() {
|
|
Err("Error".to_owned())
|
|
} else {
|
|
Ok(FileList {
|
|
path: fs::canonicalize(path)
|
|
.unwrap()
|
|
.display()
|
|
.to_string()
|
|
.to_owned(),
|
|
list: list.unwrap(),
|
|
})
|
|
}
|
|
}
|
|
|
|
#[tauri::command]
|
|
fn get_file(path: &str) -> Result<String, String> {
|
|
let run = || -> Result<String, String> {
|
|
let content = fs::read(path);
|
|
if content.is_err() {
|
|
return Err("Error".to_owned());
|
|
}
|
|
Ok(String::from_utf8(content.unwrap())
|
|
.unwrap_or("error decoding file content :(".to_owned()))
|
|
};
|
|
let res = run();
|
|
|
|
if res.is_err() {
|
|
Err("Error".to_owned())
|
|
} else {
|
|
Ok(res.unwrap())
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
app_lib::run();
|
|
tauri::Builder::default()
|
|
.plugin(tauri_plugin_shell::init())
|
|
.invoke_handler(tauri::generate_handler![greet, list_files, get_file])
|
|
.run(tauri::generate_context!())
|
|
.expect("error while running tauri application");
|
|
}
|