use serde::Serialize;
use topcoat_router::Route;
use crate::{Surrogate, Surrogated};
pub trait TypedProcedure: Route {
type Args: Surrogated;
type Output: Surrogated;
}
pub struct ProcedureSurrogate<P> {
procedure: P,
url: &'static str,
}
impl<P: TypedProcedure> ProcedureSurrogate<P> {
#[must_use]
pub const fn new(procedure: P, url: &'static str) -> Self {
Self { procedure, url }
}
#[allow(clippy::unused_async)]
pub async fn call(
&self,
_args: <P::Args as Surrogated>::Surrogate,
) -> <P::Output as Surrogated>::Surrogate {
panic!("procedures cannot be executed on the server");
}
}
impl<P> Surrogate for &'static ProcedureSurrogate<P>
where
P: TypedProcedure + Copy + Surrogated<Surrogate = Self>,
{
type Real = P;
fn into_real(self) -> Self::Real {
self.procedure
}
}
impl<P: TypedProcedure> Serialize for ProcedureSurrogate<P> {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
#[derive(Serialize)]
struct TaggedProcedure {
t: &'static str,
path: &'static str,
}
TaggedProcedure {
t: "Procedure",
path: self.url,
}
.serialize(serializer)
}
}