aither_sandbox/spawn/
ipc.rs1use std::fmt;
4use std::sync::Arc;
5
6use super::LaunchInfo;
7
8pub trait IpcProvider: Send + Sync + 'static {
17 fn router(&self, launch: &LaunchInfo) -> Option<heel::IpcRouter>;
20}
21
22trait IpcProviderImpl: Send + Sync {
24 fn router(&self, launch: &LaunchInfo) -> Option<heel::IpcRouter>;
25}
26
27impl<T: IpcProvider> IpcProviderImpl for T {
28 fn router(&self, launch: &LaunchInfo) -> Option<heel::IpcRouter> {
29 IpcProvider::router(self, launch)
30 }
31}
32
33#[derive(Clone)]
35pub struct AnyIpcProvider(Arc<dyn IpcProviderImpl>);
36
37impl AnyIpcProvider {
38 #[must_use]
40 pub fn new(provider: impl IpcProvider) -> Self {
41 Self(Arc::new(provider))
42 }
43
44 #[must_use]
46 pub fn router(&self, launch: &LaunchInfo) -> Option<heel::IpcRouter> {
47 self.0.router(launch)
48 }
49}
50
51impl fmt::Debug for AnyIpcProvider {
52 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53 f.write_str("AnyIpcProvider")
54 }
55}
56
57#[cfg(test)]
60pub mod fixture {
61 use std::sync::{Arc, Mutex};
62
63 use super::{IpcProvider, LaunchInfo};
64
65 pub const ANSWER: &str = "probe_ok";
67
68 struct Probe;
69
70 #[derive(serde::Deserialize, schemars::JsonSchema)]
71 struct ProbeArgs {}
72
73 impl heel::IpcCommand for Probe {
74 fn name(&self) -> std::borrow::Cow<'static, str> {
75 "probe".into()
76 }
77 type Args = ProbeArgs;
78 type Response = String;
79 fn handle(
80 &self,
81 _args: ProbeArgs,
82 _context: heel::IpcContext,
83 ) -> impl Future<Output = Result<String, heel::IpcError>> {
84 std::future::ready(Ok(ANSWER.into()))
85 }
86 }
87
88 #[derive(Clone, Default)]
89 pub struct Recording {
90 pub launches: Arc<Mutex<Vec<LaunchInfo>>>,
91 }
92
93 impl IpcProvider for Recording {
94 fn router(&self, launch: &LaunchInfo) -> Option<heel::IpcRouter> {
95 self.launches.lock().unwrap().push(launch.clone());
96 Some(heel::IpcRouter::new().register(Probe))
97 }
98 }
99}