use std::fmt;
use std::sync::Arc;
use super::LaunchInfo;
pub trait IpcProvider: Send + Sync + 'static {
fn router(&self, launch: &LaunchInfo) -> Option<heel::IpcRouter>;
}
trait IpcProviderImpl: Send + Sync {
fn router(&self, launch: &LaunchInfo) -> Option<heel::IpcRouter>;
}
impl<T: IpcProvider> IpcProviderImpl for T {
fn router(&self, launch: &LaunchInfo) -> Option<heel::IpcRouter> {
IpcProvider::router(self, launch)
}
}
#[derive(Clone)]
pub struct AnyIpcProvider(Arc<dyn IpcProviderImpl>);
impl AnyIpcProvider {
#[must_use]
pub fn new(provider: impl IpcProvider) -> Self {
Self(Arc::new(provider))
}
#[must_use]
pub fn router(&self, launch: &LaunchInfo) -> Option<heel::IpcRouter> {
self.0.router(launch)
}
}
impl fmt::Debug for AnyIpcProvider {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("AnyIpcProvider")
}
}
#[cfg(test)]
pub mod fixture {
use std::sync::{Arc, Mutex};
use super::{IpcProvider, LaunchInfo};
pub const ANSWER: &str = "probe_ok";
struct Probe;
#[derive(serde::Deserialize, schemars::JsonSchema)]
struct ProbeArgs {}
impl heel::IpcCommand for Probe {
fn name(&self) -> std::borrow::Cow<'static, str> {
"probe".into()
}
type Args = ProbeArgs;
type Response = String;
fn handle(
&self,
_args: ProbeArgs,
_context: heel::IpcContext,
) -> impl Future<Output = Result<String, heel::IpcError>> {
std::future::ready(Ok(ANSWER.into()))
}
}
#[derive(Clone, Default)]
pub struct Recording {
pub launches: Arc<Mutex<Vec<LaunchInfo>>>,
}
impl IpcProvider for Recording {
fn router(&self, launch: &LaunchInfo) -> Option<heel::IpcRouter> {
self.launches.lock().unwrap().push(launch.clone());
Some(heel::IpcRouter::new().register(Probe))
}
}
}