use std::sync::Arc;
use crate::error::BridgeError;
pub type Completion = Box<dyn FnOnce(String) + Send + 'static>;
pub type EventSink = Arc<dyn Fn(Option<String>, String) + Send + Sync + 'static>;
pub trait DotNetHost: Send + Sync + 'static {
fn start(&self, events: EventSink) -> Result<(), BridgeError>;
fn call(&self, window_label: &str, request_json: String, on_done: Completion);
fn cancel(&self, call_id: &str);
fn stop(&self) {}
fn on_start_error(&self) -> OnStartError {
OnStartError::ShowDialogAndExit
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum OnStartError {
#[default]
ShowDialogAndExit,
KeepRunning,
}
pub(crate) const STOPPED: &str = "HostStopped";
pub(crate) struct NoHost;
impl DotNetHost for NoHost {
fn start(&self, _events: EventSink) -> Result<(), BridgeError> {
Ok(())
}
fn call(&self, _window_label: &str, _request_json: String, on_done: Completion) {
on_done(error_response(&BridgeError::new(
"HostNotConfigured",
"No .NET host is configured for tauri-plugin-dotnet.",
)));
}
fn cancel(&self, _call_id: &str) {}
}
pub(crate) fn error_response(error: &BridgeError) -> String {
serde_json::json!({ "error": { "message": error.message, "type": error.kind } }).to_string()
}