use std::collections::HashMap; use std::sync::RwLock; use serde::{Serialize, Deserialize}; use tokio::sync::oneshot::Sender; use crate::ipc; use crate::errors::*; #[derive(Serialize, Deserialize)] #[serde(rename_all = "PascalCase")] #[serde(untagged)] pub enum Credentials { LongLived { access_key_id: String, secret_access_key: String, }, ShortLived { access_key_id: String, secret_access_key: String, token: String, expiration: String, }, } #[derive(Serialize, Deserialize)] pub enum SessionStatus { Unlocked, Locked, Empty, } pub struct AppState { status: RwLock, credentials: RwLock>, request_count: RwLock, open_requests: RwLock>>, } impl AppState { pub fn new(status: SessionStatus, creds: Option) -> Self { AppState { status: RwLock::new(status), credentials: RwLock::new(creds), request_count: RwLock::new(0), open_requests: RwLock::new(HashMap::new()), } } pub fn register_request(&self, chan: Sender) -> u64 { let count = { let c = self.request_count.write().unwrap(); *c += 1; c }; let open_requests = self.open_requests.write().unwrap(); open_requests.insert(*count, chan); *count } pub fn send_response(&self, response: ipc::RequestResponse) -> Result<(), SendResponseError> { let mut open_requests = self.open_requests.write().unwrap(); let chan = open_requests .remove(&response.id) .ok_or(SendResponseError::NotFound) ?; chan.send(response.approval) .map_err(|_e| SendResponseError::Abandoned) } pub fn get_creds_serialized(&self) -> String { let creds = self.credentials.read().unwrap(); // fix this at some point serde_json::to_string(&creds.unwrap()).unwrap() } }