creddy/src-tauri/src/ipc.rs

79 lines
1.8 KiB
Rust
Raw Normal View History

2022-11-29 00:16:33 +00:00
use serde::{Serialize, Deserialize};
use tauri::State;
use crate::errors::*;
use crate::config::AppConfig;
2022-12-20 00:20:46 +00:00
use crate::clientinfo::Client;
use crate::state::{AppState, Session, BaseCredentials};
2022-11-29 00:16:33 +00:00
#[derive(Clone, Debug, Serialize, Deserialize)]
2022-11-30 00:13:09 +00:00
pub struct Request {
pub id: u64,
2022-12-21 00:11:49 +00:00
pub clients: Vec<Option<Client>>,
2022-11-30 00:13:09 +00:00
}
#[derive(Debug, Serialize, Deserialize)]
2022-11-29 00:16:33 +00:00
pub struct RequestResponse {
pub id: u64,
pub approval: Approval,
}
#[derive(Debug, Serialize, Deserialize)]
2022-11-29 00:16:33 +00:00
pub enum Approval {
Approved,
Denied,
}
2022-11-29 00:16:33 +00:00
#[tauri::command]
2022-12-22 00:04:12 +00:00
pub fn respond(response: RequestResponse, app_state: State<'_, AppState>) -> Result<(), String> {
2022-11-29 00:16:33 +00:00
app_state.send_response(response)
2022-12-22 00:04:12 +00:00
.map_err(|e| format!("Error responding to request: {e}"))
2022-11-29 00:16:33 +00:00
}
#[tauri::command]
2022-12-30 00:40:48 +00:00
pub async fn unlock(passphrase: String, app_state: State<'_, AppState>) -> Result<(), UnlockError> {
app_state.unlock(&passphrase).await
2022-12-04 05:47:09 +00:00
}
#[tauri::command]
2022-12-14 00:50:44 +00:00
pub fn get_session_status(app_state: State<'_, AppState>) -> String {
2022-12-04 05:47:09 +00:00
let session = app_state.session.read().unwrap();
match *session {
Session::Locked(_) => "locked".into(),
Session::Unlocked{..} => "unlocked".into(),
2022-12-04 05:47:09 +00:00
Session::Empty => "empty".into()
}
2022-11-29 00:16:33 +00:00
}
2022-12-14 05:50:34 +00:00
#[tauri::command]
pub async fn save_credentials(
credentials: BaseCredentials,
2022-12-14 05:50:34 +00:00
passphrase: String,
app_state: State<'_, AppState>
2022-12-30 00:40:48 +00:00
) -> Result<(), UnlockError> {
app_state.save_creds(credentials, &passphrase).await
2022-12-14 05:50:34 +00:00
}
#[tauri::command]
pub fn get_config(app_state: State<'_, AppState>) -> AppConfig {
let config = app_state.config.read().unwrap();
config.clone()
}
2023-04-26 05:10:14 +00:00
#[tauri::command]
2023-04-26 22:49:08 +00:00
pub async fn save_config(config: AppConfig, app_state: State<'_, AppState>) -> Result<(), String> {
app_state.update_config(config)
.await
2023-04-28 21:33:04 +00:00
.map_err(|e| format!("Error saving config: {e}"))?;
Ok(())
2023-04-26 05:10:14 +00:00
}