Skip to content

Commit

Permalink
feat(config): implement config system (#34)
Browse files Browse the repository at this point in the history
* feat(config): implement config system

Resolved: #25

* chore(changeset): add changeset

* feat(config): add config proxy object

* feat(config): extend `String` to handle default errors

* chore(changeset): add changeset
  • Loading branch information
fu050409 authored Jul 14, 2024
1 parent 49f0aca commit 73d4db4
Show file tree
Hide file tree
Showing 18 changed files with 234 additions and 31 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-cooks-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@noctisynth/grassator': patch
---

Extend `String` to `ErrorPayload` to handle default errors
5 changes: 5 additions & 0 deletions .changeset/early-papayas-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@noctisynth/grassator': patch
---

Fix set config arg name to `newConfig`
6 changes: 6 additions & 0 deletions .changeset/hip-sheep-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'grassator': patch
'@noctisynth/grassator': patch
---

Implement config system and auto load on startup
5 changes: 5 additions & 0 deletions .changeset/itchy-lobsters-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@noctisynth/grassator': patch
---

Add global config proxy object
6 changes: 6 additions & 0 deletions .changeset/olive-ways-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'grassator': patch
'@noctisynth/grassator': patch
---

Add config file path to callback data structure
3 changes: 2 additions & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"tauri",
"toastservice",
"unocss",
"unplugin"
"unplugin",
"usetoast"
],
"ignorePaths": [
// Tauri files
Expand Down
54 changes: 37 additions & 17 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ tauri-plugin-shell = "2.0.0-beta"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
reqwest = "0.12.5"
toml = "0.8.14"
51 changes: 48 additions & 3 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,53 @@
use models::ErrorPayload;

mod models;
mod state;
mod utils;

#[tauri::command]
fn get_config(config: tauri::State<state::Config>) -> Result<state::Config, ()> {
Ok(config.inner().clone())
async fn load_config<R: tauri::Runtime>(
app: tauri::AppHandle<R>,
config: tauri::State<'_, state::Config>,
) -> Result<models::ConfigResult, ErrorPayload> {
let config_file_path = utils::get_config_path_buf(&app)?;
let config_data = if !config_file_path.exists() {
let config_data = toml::to_string_pretty(&state::InnerConfig::default()).unwrap();
println!("{}", config_data);
std::fs::write(&config_file_path, &config_data).unwrap();
config_data
} else {
std::fs::read_to_string(&config_file_path).unwrap()
};
let inner_config: state::InnerConfig = toml::from_str(&config_data).unwrap();
*config.inner().inner.write().await = inner_config.clone();
Ok(models::ConfigResult {
inner: inner_config,
file_path: config_file_path.to_string_lossy().to_string(),
})
}

#[tauri::command]
async fn get_config(config: tauri::State<'_, state::Config>) -> Result<state::InnerConfig, ()> {
Ok(config.inner().inner.read().await.clone())
}

#[tauri::command]
async fn set_config<R: tauri::Runtime>(
app: tauri::AppHandle<R>,
new_config: state::InnerConfig,
config: tauri::State<'_, state::Config>,
) -> Result<models::SetConfigResult, models::ErrorPayload> {
let config_file_path = utils::get_config_path_buf(&app)?;
let toml = toml::to_string_pretty(&new_config).unwrap();
if let Err(e) = std::fs::write(&config_file_path, toml) {
return Err(ErrorPayload {
message: format!("Failed to update config file: {}", e),
});
};
*config.inner().inner.write().await = new_config;
Ok(models::SetConfigResult {
config_path: config_file_path.to_string_lossy().to_string(),
})
}

#[tauri::command]
Expand All @@ -24,7 +67,7 @@ async fn download_file(
output: &str,
config: tauri::State<'_, state::Config>,
) -> Result<models::DownloadResult, ()> {
match utils::download_file(url, output, &config.inner()).await {
match utils::download_file(url, output, &*config.inner().inner.read().await).await {
Ok(_) => Ok(models::DownloadResult {
file_path: output.to_string(),
error: None,
Expand All @@ -42,7 +85,9 @@ pub fn run() {
.manage(state::Config::default())
.plugin(tauri_plugin_shell::init())
.invoke_handler(tauri::generate_handler![
load_config,
get_config,
set_config,
get_file_size,
download_file
])
Expand Down
18 changes: 18 additions & 0 deletions src-tauri/src/models.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use serde::{Deserialize, Serialize};

use crate::state::InnerConfig;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct FileSize {
pub(crate) size: u64,
Expand All @@ -11,3 +13,19 @@ pub(crate) struct DownloadResult {
pub(crate) file_path: String,
pub(crate) error: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct SetConfigResult {
pub(crate) config_path: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct ConfigResult {
pub(crate) inner: InnerConfig,
pub(crate) file_path: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct ErrorPayload {
pub(crate) message: String,
}
21 changes: 17 additions & 4 deletions src-tauri/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
use serde::{Deserialize, Serialize};
use tauri::async_runtime::RwLock;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct InnerConfig {
pub(crate) preset_file: Option<String>,
pub(crate) num_threads: Option<u32>,
}

impl Default for InnerConfig {
fn default() -> Self {
Self {
preset_file: None,
num_threads: Some(12),
}
}
}

pub(crate) struct Config {
pub(crate) preset_file: String,
pub(crate) num_threads: u32,
pub(crate) inner: RwLock<InnerConfig>,
}

impl Default for Config {
fn default() -> Self {
Self {
preset_file: String::from("presets/default.json"),
num_threads: 7,
inner: RwLock::new(InnerConfig::default()),
}
}
}
Loading

0 comments on commit 73d4db4

Please sign in to comment.