use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum OriginType {
Localhost,
Subnet,
Any,
}
impl OriginType {
pub fn bind_address(self) -> &'static str {
match self {
OriginType::Localhost => "127.0.0.1",
OriginType::Subnet | OriginType::Any => "0.0.0.0",
}
}
}
impl From<OriginType> for &'static str {
fn from(value: OriginType) -> Self {
value.bind_address()
}
}
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RemoteUiConfig {
pub(crate) application_ui: bool,
pub(crate) allowed_origin: OriginType,
pub(crate) port: Option<u16>,
pub(crate) bundle_path: Option<String>,
pub(crate) primary_window_label: String,
pub(crate) minimize_app: bool,
pub(crate) enable_info_url: bool,
pub(crate) custom_blocking_ui: Option<String>,
pub(crate) custom_disconnect_ui: Option<String>,
}
impl Default for RemoteUiConfig {
fn default() -> Self {
RemoteUiConfig {
application_ui: false,
allowed_origin: OriginType::Localhost,
port: None,
bundle_path: None,
primary_window_label: "main".to_owned(),
enable_info_url: true,
minimize_app: false,
custom_disconnect_ui: None,
custom_blocking_ui: None,
}
}
}
impl RemoteUiConfig {
pub fn enable_application_ui(mut self) -> Self {
self.application_ui = true;
self
}
pub fn minimize_app(mut self) -> Self {
self.minimize_app = true;
self
}
pub fn disable_info_url(mut self) -> Self {
self.enable_info_url = false;
self
}
pub fn set_allowed_origin(mut self, allowed_origin: OriginType) -> Self {
self.allowed_origin = allowed_origin;
self
}
pub fn set_port(mut self, port: Option<u16>) -> Self {
self.port = port;
self
}
pub fn set_bundle_path(mut self, bundle_path: Option<String>) -> Self {
self.bundle_path = bundle_path;
self
}
pub fn set_custom_blocking_ui(mut self, custom_blocking_ui: Option<String>) -> Self {
self.custom_blocking_ui = custom_blocking_ui;
self
}
pub fn set_custom_disconnect_ui(mut self, custom_disconnect_ui: Option<String>) -> Self {
self.custom_disconnect_ui = custom_disconnect_ui;
self
}
pub fn set_primary_window_label(mut self, label: impl Into<String>) -> Self {
self.primary_window_label = label.into();
self
}
pub fn allowed_origin(&self) -> OriginType {
self.allowed_origin
}
pub fn port(&self) -> Option<u16> {
self.port
}
pub fn bundle_path(&self) -> Option<&str> {
self.bundle_path.as_deref()
}
pub fn primary_window_label(&self) -> &str {
&self.primary_window_label
}
}
#[derive(Debug, Deserialize)]
pub struct WsPayload {
pub id: usize,
pub cmd: String,
pub args: Option<Value>,
pub option: Option<Value>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum RpcStatus {
Success,
Error,
}
impl RpcStatus {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Success => "success",
Self::Error => "error",
}
}
}