creddy/src-tauri/src/ipc.rs

65 lines
1.4 KiB
Rust
Raw Normal View History

2022-11-29 00:16:33 +00:00
use serde::{Serialize, Deserialize};
use tauri::State;
2022-12-20 00:20:46 +00:00
use crate::clientinfo::Client;
2022-12-14 05:50:34 +00:00
use crate::state::{AppState, Session, Credentials};
2022-11-29 00:16:33 +00:00
2022-12-14 00:50:44 +00:00
#[derive(Clone, Serialize, Deserialize)]
2022-11-30 00:13:09 +00:00
pub struct Request {
pub id: u64,
2022-12-14 00:50:44 +00:00
pub clients: Vec<Client>,
2022-11-30 00:13:09 +00:00
}
2022-11-29 00:16:33 +00:00
#[derive(Serialize, Deserialize)]
pub struct RequestResponse {
pub id: u64,
pub approval: Approval,
}
#[derive(Serialize, Deserialize)]
pub enum Approval {
Approved,
Denied,
}
2022-11-29 00:16:33 +00:00
#[tauri::command]
pub fn respond(response: RequestResponse, app_state: State<'_, AppState>) -> Result<(), String> {
app_state.send_response(response)
.map_err(|e| format!("Error responding to request: {e}"))
}
#[tauri::command]
2022-12-04 05:47:09 +00:00
pub async fn unlock(passphrase: String, app_state: State<'_, AppState>) -> Result<(), String> {
app_state.decrypt(&passphrase)
.await
2022-12-14 05:50:34 +00:00
.map_err(|e| e.to_string())
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(),
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: Credentials,
passphrase: String,
app_state: State<'_, AppState>
) -> Result<(), String> {
app_state.save_creds(credentials, &passphrase)
.await
.map_err(|e| e.to_string())
}