Skip to content

Commit

Permalink
Split rust struct definitions into a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
EliteAsian123 committed Aug 2, 2024
1 parent 82b857b commit 09a4108
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 68 deletions.
70 changes: 8 additions & 62 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,18 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]

mod utils;
mod types;

use std::{fs, path::PathBuf, process::Command, sync::{LazyLock, Mutex}};
use std::{fs, path::PathBuf, process::Command, sync::Mutex};

use directories::BaseDirs;
use serde_repr::{Deserialize_repr, Serialize_repr};
use tauri::{AppHandle, Manager};
use window_shadows::set_shadow;
use utils::{clear_folder, download, extract, extract_encrypted};
use utils::*;
use types::*;
use clap::Parser;

#[derive(Default, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ImportantDirs {
pub yarc_folder: String,
pub launcher_folder: String,
pub temp_folder: String,
}

#[derive(Default, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CustomDirs {
pub yarg_folder: String,
pub setlist_folder: String,
}

#[derive(Default, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ReleaseContent {
pub name: String,
pub platforms: Vec<String>,
pub files: Vec<ReleaseContentFile>,
}

#[derive(Default, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ReleaseContentFile {
pub url: String,
pub file_type: String,
pub signature: Option<String>,
}

#[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)]
#[repr(u8)]
enum ProfileFolderState {
Error = 0,
UpToDate = 1,
UpdateRequired = 2,
FirstDownload = 3
}

fn path_to_string(p: PathBuf) -> Result<String, String> {
Ok(p.into_os_string()
.into_string()
.map_err(|e| format!("Failed to convert path to string!\n{:?}", e))?)
}
static COMMAND_LINE_ARG_LAUNCH: Mutex<Option<String>> = Mutex::new(None);

#[tauri::command(async)]
fn get_important_dirs() -> Result<ImportantDirs, String> {
Expand Down Expand Up @@ -234,27 +191,16 @@ fn open_folder_profile(profile_path: String) -> Result<(), String> {

#[tauri::command(async)]
fn get_launch_argument() -> Option<String> {
let launch_arg = DO_LAUNCH.lock().unwrap();
let launch_arg = COMMAND_LINE_ARG_LAUNCH.lock().unwrap();
return launch_arg.to_owned();
}

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
/// UUID of the profile to launch
#[arg(short, long)]
launch: Option<String>
}

static DO_LAUNCH: LazyLock<Mutex<Option<String>>> = LazyLock::new(|| Mutex::new(None));

fn main() {
let args = Args::parse();
let args = CommandLineArgs::parse();

{
// Stores the launch option in a static so the frontend can request it later.
// TODO: Maybe change this to something more generic so the frontend can request any argument
let mut launch_option = DO_LAUNCH.lock().unwrap();
let mut launch_option = COMMAND_LINE_ARG_LAUNCH.lock().unwrap();
*launch_option = args.launch;
}

Expand Down
57 changes: 57 additions & 0 deletions src-tauri/src/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use clap::{command, Parser};
use serde_repr::{Deserialize_repr, Serialize_repr};

#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct CommandLineArgs {
/// UUID of the profile to launch
#[arg(short, long)]
pub launch: Option<String>,
}

#[derive(Clone, serde::Serialize)]
pub struct ProgressPayload {
pub state: String,
pub total: u64,
pub current: u64,
}

#[derive(Default, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ImportantDirs {
pub yarc_folder: String,
pub launcher_folder: String,
pub temp_folder: String,
}

#[derive(Default, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CustomDirs {
pub yarg_folder: String,
pub setlist_folder: String,
}

#[derive(Default, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ReleaseContent {
pub name: String,
pub platforms: Vec<String>,
pub files: Vec<ReleaseContentFile>,
}

#[derive(Default, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ReleaseContentFile {
pub url: String,
pub file_type: String,
pub signature: Option<String>,
}

#[derive(Serialize_repr, Deserialize_repr, PartialEq, Debug)]
#[repr(u8)]
pub enum ProfileFolderState {
Error = 0,
UpToDate = 1,
UpdateRequired = 2,
FirstDownload = 3,
}
13 changes: 7 additions & 6 deletions src-tauri/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
use crate::ProgressPayload;

use futures_util::StreamExt;
use reqwest;
use sevenz_rust::Password;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
use std::{fs::File, io::Write};
use tauri::{AppHandle, Manager};

const LETTERS: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
const EMIT_BUFFER_RATE: f64 = 1.0 / 15.0;

#[derive(Clone, serde::Serialize)]
pub struct ProgressPayload {
pub state: String,
pub total: u64,
pub current: u64,
pub fn path_to_string(p: PathBuf) -> Result<String, String> {
Ok(p.into_os_string()
.into_string()
.map_err(|e| format!("Failed to convert path to string!\n{:?}", e))?)
}

pub fn clear_folder(path: &Path) -> Result<(), String> {
Expand Down

0 comments on commit 09a4108

Please sign in to comment.