#![cfg_attr(docsrs, feature(doc_cfg))]
#[cfg(any(debug_assertions, feature = "allow-in-release"))]
mod server;
use std::{
net::{IpAddr, Ipv4Addr},
time::Duration,
};
use tauri::{
plugin::{Builder as PluginBuilder, TauriPlugin},
Runtime,
};
pub const DEFAULT_PORT: u16 = 3030;
#[cfg(any(debug_assertions, feature = "allow-in-release"))]
pub(crate) const BRIDGE_EVENT: &str = "dev-invoke://callback";
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OriginPolicy {
Auto,
Any,
List(Vec<String>),
}
#[derive(Debug, Clone)]
pub struct Config {
pub host: IpAddr,
pub port: u16,
pub window: Option<String>,
pub origins: OriginPolicy,
pub extra_origins: Vec<String>,
pub serve_assets: bool,
pub timeout: Duration,
pub enabled_in_release: bool,
pub headless: bool,
}
impl Default for Config {
fn default() -> Self {
Self {
host: IpAddr::V4(Ipv4Addr::LOCALHOST),
port: DEFAULT_PORT,
window: None,
origins: OriginPolicy::Auto,
extra_origins: Vec::new(),
serve_assets: true,
timeout: Duration::from_secs(600),
enabled_in_release: false,
headless: false,
}
}
}
#[derive(Debug, Clone, Default)]
pub struct Builder {
config: Config,
}
impl Builder {
pub fn new() -> Self {
Self::default()
}
pub fn host(mut self, host: impl Into<IpAddr>) -> Self {
self.config.host = host.into();
self
}
pub fn port(mut self, port: u16) -> Self {
self.config.port = port;
self
}
pub fn window(mut self, label: impl Into<String>) -> Self {
self.config.window = Some(label.into());
self
}
pub fn allow_origin(mut self, origin: impl Into<String>) -> Self {
self.config.extra_origins.push(origin.into());
self
}
pub fn allowed_origins<I, S>(mut self, origins: I) -> Self
where
I: IntoIterator<Item = S>,
S: Into<String>,
{
self.config.origins = OriginPolicy::List(origins.into_iter().map(Into::into).collect());
self
}
pub fn allow_any_origin(mut self) -> Self {
self.config.origins = OriginPolicy::Any;
self
}
pub fn serve_assets(mut self, enabled: bool) -> Self {
self.config.serve_assets = enabled;
self
}
pub fn timeout(mut self, timeout: Duration) -> Self {
self.config.timeout = timeout;
self
}
pub fn headless(mut self, headless: bool) -> Self {
self.config.headless = headless;
self
}
pub fn enabled_in_release(mut self, enabled: bool) -> Self {
self.config.enabled_in_release = enabled;
self
}
pub fn build<R: Runtime>(self) -> TauriPlugin<R> {
#[cfg(any(debug_assertions, feature = "allow-in-release"))]
{
if cfg!(debug_assertions) || self.config.enabled_in_release {
return server::plugin(self.config);
}
}
let _ = self;
PluginBuilder::new("dev-invoke").build()
}
}
pub fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::new().build()
}