use tokio::sync::broadcast;
use wcode_harness::actor::SessionHandle;
use wcode_harness::event::AgentEvent;
use wcode_harness::protocol::{Request, SessionId};
#[cfg(unix)]
use crate::client::Client;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Closed;
impl std::fmt::Display for Closed {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("session is closed")
}
}
impl std::error::Error for Closed {}
#[derive(Clone)]
pub enum Backend {
Local(SessionHandle),
#[cfg(unix)]
Remote(Client),
}
impl Backend {
pub fn send(&self, request: Request) -> Result<(), Closed> {
match self {
Backend::Local(handle) => handle.send(request).map_err(|_| Closed),
#[cfg(unix)]
Backend::Remote(client) => client.send(request),
}
}
pub fn send_from(&self, from: SessionId, request: Request) -> Result<(), Closed> {
match self {
Backend::Local(handle) => handle.send_from(from, request).map_err(|_| Closed),
#[cfg(unix)]
Backend::Remote(client) => client.send_from(from, request),
}
}
pub async fn ask(&self, request: Request) -> Result<AgentEvent, Closed> {
match self {
Backend::Local(handle) => handle.ask(request).await.map_err(|_| Closed),
#[cfg(unix)]
Backend::Remote(client) => client.ask(request).await,
}
}
pub fn subscribe(&self) -> broadcast::Receiver<AgentEvent> {
match self {
Backend::Local(handle) => handle.subscribe(),
#[cfg(unix)]
Backend::Remote(client) => client.subscribe(),
}
}
}
impl From<SessionHandle> for Backend {
fn from(handle: SessionHandle) -> Self {
Backend::Local(handle)
}
}
#[cfg(unix)]
impl From<Client> for Backend {
fn from(client: Client) -> Self {
Backend::Remote(client)
}
}