added license, readme and theme management
This commit is contained in:
@@ -25,6 +25,9 @@ pub enum DbError {
|
||||
#[error("Serialization error: {0}")]
|
||||
Serialization(String),
|
||||
|
||||
#[error("JSON error: {0}")]
|
||||
Json(#[from] serde_json::Error),
|
||||
|
||||
#[error("IO error: {0}")]
|
||||
Io(#[from] std::io::Error),
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ mod collections;
|
||||
mod db;
|
||||
mod http;
|
||||
mod requests;
|
||||
mod settings;
|
||||
mod variables;
|
||||
mod workspaces;
|
||||
|
||||
@@ -18,6 +19,7 @@ use requests::{
|
||||
create_request, delete_request, get_all_requests_by_workspace, get_request,
|
||||
get_requests_by_collection, get_standalone_requests_by_workspace, update_request,
|
||||
};
|
||||
use settings::{get_settings, reset_settings, update_settings};
|
||||
use variables::{
|
||||
create_variable, delete_variable, get_collection_variables, get_global_variables,
|
||||
get_request_variables, get_resolved_variables, get_variable, get_workspace_variables,
|
||||
@@ -86,6 +88,10 @@ pub fn run() {
|
||||
delete_variable,
|
||||
// HTTP client
|
||||
send_http_request,
|
||||
// Settings commands
|
||||
get_settings,
|
||||
update_settings,
|
||||
reset_settings,
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
|
||||
27
src-tauri/src/settings/commands.rs
Normal file
27
src-tauri/src/settings/commands.rs
Normal file
@@ -0,0 +1,27 @@
|
||||
use tauri::State;
|
||||
|
||||
use crate::db::Database;
|
||||
|
||||
use super::service::SettingsService;
|
||||
use super::types::{AppSettings, UpdateSettingsInput};
|
||||
|
||||
#[tauri::command]
|
||||
pub fn get_settings(db: State<Database>) -> Result<AppSettings, String> {
|
||||
let service = SettingsService::new(db.inner().clone());
|
||||
service.get().map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn update_settings(
|
||||
db: State<Database>,
|
||||
input: UpdateSettingsInput,
|
||||
) -> Result<AppSettings, String> {
|
||||
let service = SettingsService::new(db.inner().clone());
|
||||
service.update(input).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn reset_settings(db: State<Database>) -> Result<AppSettings, String> {
|
||||
let service = SettingsService::new(db.inner().clone());
|
||||
service.reset().map_err(|e| e.to_string())
|
||||
}
|
||||
7
src-tauri/src/settings/mod.rs
Normal file
7
src-tauri/src/settings/mod.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
mod commands;
|
||||
mod service;
|
||||
mod types;
|
||||
|
||||
pub use commands::*;
|
||||
#[allow(unused_imports)]
|
||||
pub use types::{AppSettings, CustomTheme, Theme, ThemeColors, UpdateSettingsInput};
|
||||
78
src-tauri/src/settings/service.rs
Normal file
78
src-tauri/src/settings/service.rs
Normal file
@@ -0,0 +1,78 @@
|
||||
use crate::db::{Database, DbResult, APP_SETTINGS};
|
||||
|
||||
use super::types::{AppSettings, UpdateSettingsInput};
|
||||
|
||||
const SETTINGS_KEY: &str = "settings";
|
||||
|
||||
pub struct SettingsService {
|
||||
db: Database,
|
||||
}
|
||||
|
||||
impl SettingsService {
|
||||
pub fn new(db: Database) -> Self {
|
||||
Self { db }
|
||||
}
|
||||
|
||||
pub fn get(&self) -> DbResult<AppSettings> {
|
||||
let read_txn = self.db.begin_read()?;
|
||||
let table = read_txn.open_table(APP_SETTINGS)?;
|
||||
|
||||
match table.get(SETTINGS_KEY)? {
|
||||
Some(value) => {
|
||||
let settings: AppSettings = serde_json::from_str(value.value())?;
|
||||
Ok(settings)
|
||||
}
|
||||
None => Ok(AppSettings::default()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(&self, input: UpdateSettingsInput) -> DbResult<AppSettings> {
|
||||
let mut settings = self.get()?;
|
||||
|
||||
if let Some(theme) = input.theme {
|
||||
settings.theme = theme;
|
||||
}
|
||||
if let Some(custom_themes) = input.custom_themes {
|
||||
settings.custom_themes = custom_themes;
|
||||
}
|
||||
if let Some(default_timeout) = input.default_timeout {
|
||||
settings.default_timeout = default_timeout;
|
||||
}
|
||||
if let Some(follow_redirects) = input.follow_redirects {
|
||||
settings.follow_redirects = follow_redirects;
|
||||
}
|
||||
if let Some(validate_ssl) = input.validate_ssl {
|
||||
settings.validate_ssl = validate_ssl;
|
||||
}
|
||||
if let Some(max_history_items) = input.max_history_items {
|
||||
settings.max_history_items = max_history_items;
|
||||
}
|
||||
if let Some(auto_save_requests) = input.auto_save_requests {
|
||||
settings.auto_save_requests = auto_save_requests;
|
||||
}
|
||||
|
||||
let write_txn = self.db.begin_write()?;
|
||||
{
|
||||
let mut table = write_txn.open_table(APP_SETTINGS)?;
|
||||
let json = serde_json::to_string(&settings)?;
|
||||
table.insert(SETTINGS_KEY, json.as_str())?;
|
||||
}
|
||||
write_txn.commit()?;
|
||||
|
||||
Ok(settings)
|
||||
}
|
||||
|
||||
pub fn reset(&self) -> DbResult<AppSettings> {
|
||||
let settings = AppSettings::default();
|
||||
|
||||
let write_txn = self.db.begin_write()?;
|
||||
{
|
||||
let mut table = write_txn.open_table(APP_SETTINGS)?;
|
||||
let json = serde_json::to_string(&settings)?;
|
||||
table.insert(SETTINGS_KEY, json.as_str())?;
|
||||
}
|
||||
write_txn.commit()?;
|
||||
|
||||
Ok(settings)
|
||||
}
|
||||
}
|
||||
95
src-tauri/src/settings/types.rs
Normal file
95
src-tauri/src/settings/types.rs
Normal file
@@ -0,0 +1,95 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum Theme {
|
||||
System,
|
||||
Light,
|
||||
Dark,
|
||||
Latte,
|
||||
Frappe,
|
||||
Macchiato,
|
||||
Mocha,
|
||||
Custom,
|
||||
}
|
||||
|
||||
impl Default for Theme {
|
||||
fn default() -> Self {
|
||||
Self::System
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct CustomTheme {
|
||||
pub name: String,
|
||||
pub colors: ThemeColors,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ThemeColors {
|
||||
pub background: String,
|
||||
pub foreground: String,
|
||||
pub primary: String,
|
||||
pub secondary: String,
|
||||
pub accent: String,
|
||||
pub muted: String,
|
||||
pub border: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct AppSettings {
|
||||
pub theme: Theme,
|
||||
#[serde(default)]
|
||||
pub custom_themes: Vec<CustomTheme>,
|
||||
#[serde(default = "default_timeout")]
|
||||
pub default_timeout: u32,
|
||||
#[serde(default = "default_true")]
|
||||
pub follow_redirects: bool,
|
||||
#[serde(default = "default_true")]
|
||||
pub validate_ssl: bool,
|
||||
#[serde(default = "default_max_history")]
|
||||
pub max_history_items: u32,
|
||||
#[serde(default = "default_true")]
|
||||
pub auto_save_requests: bool,
|
||||
}
|
||||
|
||||
fn default_timeout() -> u32 {
|
||||
30000
|
||||
}
|
||||
|
||||
fn default_true() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn default_max_history() -> u32 {
|
||||
100
|
||||
}
|
||||
|
||||
impl Default for AppSettings {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
theme: Theme::System,
|
||||
custom_themes: Vec::new(),
|
||||
default_timeout: 30000,
|
||||
follow_redirects: true,
|
||||
validate_ssl: true,
|
||||
max_history_items: 100,
|
||||
auto_save_requests: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct UpdateSettingsInput {
|
||||
pub theme: Option<Theme>,
|
||||
pub custom_themes: Option<Vec<CustomTheme>>,
|
||||
pub default_timeout: Option<u32>,
|
||||
pub follow_redirects: Option<bool>,
|
||||
pub validate_ssl: Option<bool>,
|
||||
pub max_history_items: Option<u32>,
|
||||
pub auto_save_requests: Option<bool>,
|
||||
}
|
||||
Reference in New Issue
Block a user