use native_dialog::FileDialog;
use tauri::{Manager, PhysicalSize, Size};
-use std::{io::{Error, ErrorKind}, path::PathBuf, sync::Mutex};
+use std::{env, io::{Error, ErrorKind}, path::PathBuf, sync::Mutex};
#[macro_use]
extern crate lazy_static;
#[tauri::command]
async fn slice_button(app: tauri::AppHandle, chapter: String, fileformat: String){
+ // Check the user selected a file and a folder
+ let mut cancel_slicing = false;
+ if FILE_PATH.lock().unwrap().to_owned() == "" || FILE_PATH.lock().unwrap().to_owned() == "None"{
+ println!("error no file selected, slicing aborted");
+ app.emit_all("backend_error", Payload { message: "file_empty".to_owned() }).unwrap();
+ cancel_slicing = true;
+ }
+ if FOLDER_PATH.lock().unwrap().to_owned() == "" || FOLDER_PATH.lock().unwrap().to_owned() == "None"{
+ println!("error no folder selected, slicing aborted");
+ app.emit_all("backend_error", Payload { message: "folder_empty".to_owned() }).unwrap();
+ cancel_slicing = true;
+ }
+ if cancel_slicing == true { return; }
+
// Try to format the chapters and panic if it was not able to
let formated_chapters = match format_chapter(&chapter) {
Ok(res) => res,
- Err(error) => panic!("Problem slicing chapter: {:?}", error),
+ Err(_error) => {
+ println!("error formating chapters, slicing aborted");
+ app.emit_all("backend_error", Payload { message: "formating_issue".to_owned() }).unwrap();
+ return;
+ },
};
+
let time_codes: Vec<String> = formated_chapters.0;
let title_names: Vec<String> = formated_chapters.1;
let lines: Vec<&str> = chapter.split("\n").collect();
let mut time_code: Vec<String> = vec![];
let mut title_names: Vec<String> = vec![];
+ if lines.len() < 2 { return Err(Error::new(ErrorKind::Other, "Lines are empty")); }
for l in lines.iter(){
if l.is_empty() { break; }
if splited_line.len()<2 || splited_line[1] == "" { // To avoid blank title
return Err(Error::new(ErrorKind::Other, "No title associated with the time code"));
}
+ if !is_time_code_valid(&splited_line[0].to_owned()){
+ return Err(Error::new(ErrorKind::Other, "one or multiple time code is invalid"));
+ }
time_code.push(splited_line[0].to_owned());
title_names.push(splited_line[1..].join(" - "));
}
Ok((time_code, title_names))
}
+/// return true if the time code is valid, and false otherwise
+fn is_time_code_valid(time_code: &String) -> bool{
+ let allowed_char = "0123456789:";
+ for c in time_code.chars(){
+ if !allowed_char.contains(c) { return false; }
+ }
+ true
+}
+
// prompt user file chooser using native_dialogue crate
fn choose_file() -> String{
println!("Let's choose a file !");
}
fn main() {
+ env::set_var("WEBKIT_DISABLE_COMPOSITING_MODE", "1");
// generate the tauri app
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![select_file_button, select_folder_button, debug_call, slice_button, about_button])
// listen for file location from rust backend
const unlistenfile = await listen('file_path_changed', (event) => {
+ document.getElementById("fileLocation").style.color = "white";
document.getElementById("fileLocation").innerHTML = event.payload.message;
console.log("changing file label to : ", event.payload.message);
})
// listen for folder location from rust backend
const unlistenfolder = await listen('folder_path_changed', (event) => {
+ document.getElementById("folderLocation").style.color = "white";
document.getElementById("folderLocation").innerHTML = event.payload.message;
console.log("changing folder label to : ", event.payload.message);
+})
+
+// listen for error from rust backend
+const unlistenerror = await listen('backend_error', (event) => {
+ switch (event.payload.message) {
+ case "file_empty":
+ document.getElementById("fileLocation").style.color = "red";
+ document.getElementById("fileLocation").innerHTML = "please select an input file"
+ console.log("please select an input file");
+ break;
+ case "folder_empty":
+ document.getElementById("folderLocation").style.color = "red";
+ document.getElementById("folderLocation").innerHTML = "please select an output folder"
+ console.log("please select an output folder");
+ break;
+ case "formating_issue":
+ document.getElementById("errorLabel").innerHTML = "error: wrong chapter format. use 0:13 - my title"
+ console.log("error: wrong chapter format. use \n0:00 - first song title \n2:13 - second song title \n3:45 - third song title \n...");
+ break;
+ }
})
\ No newline at end of file