pub mod req;
pub mod resp;
mod errors;
pub use self::{
errors::IpcError,
req::{AuthReq, IpcReq},
resp::{AuthGranted, IpcResp},
};
use serde::{Deserialize, Serialize};
use std::u32;
#[allow(clippy::large_enum_variant)]
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
pub enum IpcMsg {
Req(IpcReq),
Resp(IpcResp),
Err(IpcError),
}
impl IpcMsg {
pub fn new_auth_req(app_id: &str, app_name: &str, app_vendor: &str) -> Self {
use rand::Rng;
let req_id: u32 = rand::thread_rng().gen_range(0..u32::max_value()) + 1;
Self::Req(IpcReq::Auth(AuthReq {
req_id,
app_id: app_id.to_string(),
app_name: app_name.to_string(),
app_vendor: app_vendor.to_string(),
}))
}
pub fn new_unreg_req(user_data: &[u8]) -> Self {
Self::Req(IpcReq::Unregistered(user_data.to_vec()))
}
pub fn to_string(&self) -> Result<String, IpcError> {
serde_json::to_string(self).map_err(|_| IpcError::EncodeDecodeError)
}
pub fn from_string(msg_str: &str) -> Result<IpcMsg, IpcError> {
serde_json::from_str(msg_str).map_err(|_| IpcError::EncodeDecodeError)
}
}