Skip to main content

taba/
procedure.rs

1//! Procedures, as a types crate declares them.
2
3use alloc::vec::Vec;
4use core::marker::PhantomData;
5
6use serde_core::{Serialize, Serializer};
7
8use crate::{
9    Output,
10    typescript::{Named, Request, Shape},
11};
12
13#[doc(hidden)]
14pub mod __private {
15    pub use super::{Declaration, ReplyShape};
16}
17
18/// What TypeScript can call or subscribe to.
19///
20/// TypeScript calls a procedure by its type's path, which a generic type's instantiations share:
21///
22/// ```rust,ignore
23/// #[derive(Deserialize, Input)]
24/// struct Find<T> {
25///     query: T,
26/// }
27///
28/// #[derive(Deserialize, Input)]
29/// struct Id(String);
30///
31/// // both would be `shop::Find`, sent as `{ query: string }`
32/// impl Procedure for Find<String> { ... }
33/// impl Procedure for Find<Id> { ... }
34/// ```
35///
36/// To call each instantiation, wrap it in a struct of its own:
37///
38/// ```rust,ignore
39/// #[derive(Deserialize, Input)]
40/// struct FindByName(Find<String>);
41///
42/// #[derive(Deserialize, Input)]
43/// struct FindById(Find<Id>);
44///
45/// impl Procedure for FindByName { ... }
46/// impl Procedure for FindById { ... }
47/// ```
48pub trait Procedure: Request {
49    /// What TypeScript gets on success.
50    type Output;
51
52    /// What TypeScript gets on error.
53    type Error;
54}
55
56/// How a procedure replies with its [`Output`](Procedure::Output).
57pub trait IntoReply {
58    /// [`Once`] for a call, [`Strand`] for a subscription.
59    type Reply;
60}
61
62impl<T: Serialize> IntoReply for T {
63    type Reply = Once<T>;
64}
65
66impl<T> IntoReply for Strand<T> {
67    type Reply = Strand<T>;
68}
69
70/// A [`Reply`](IntoReply::Reply) TypeScript can see.
71pub trait Reply {
72    const SHAPE: ReplyShape;
73}
74
75impl<T: Output> Reply for Once<T> {
76    const SHAPE: ReplyShape = ReplyShape::Once(T::__SHAPE);
77}
78
79impl<T: Output> Reply for Strand<T> {
80    const SHAPE: ReplyShape = ReplyShape::Strand(T::__SHAPE);
81}
82
83/// A reply, as TypeScript sees it.
84#[derive(Debug)]
85pub enum ReplyShape {
86    /// A call's value.
87    Once(Shape),
88
89    /// Each of a subscription's values.
90    Strand(Shape),
91}
92
93/// A list of procedures.
94#[derive(Debug, Default)]
95pub struct Procedures {
96    pub(crate) declarations: Vec<Declaration>,
97}
98
99/// One procedure in a [`Procedures`] list, as TypeScript sees it.
100#[derive(Debug)]
101pub struct Declaration {
102    /// Its request type's `Named`.
103    pub named: fn() -> &'static Named,
104
105    /// What it replies with.
106    pub reply: &'static ReplyShape,
107
108    /// What it fails with, `never` if it can't.
109    pub error: &'static Shape,
110}
111
112impl Procedures {
113    /// A list with no procedures.
114    pub fn new() -> Self {
115        Self::default()
116    }
117
118    /// Adds `P`.
119    pub fn declare<P: Procedure>(mut self) -> Self
120    where
121        P::Output: IntoReply<Reply: Reply>,
122        P::Error: Output,
123    {
124        let reply = const { &<<P::Output as IntoReply>::Reply as Reply>::SHAPE };
125        let error = const { &<P::Error as Output>::__SHAPE };
126        self.declarations.push(Declaration { named: P::named, reply, error });
127        self
128    }
129
130    #[doc(hidden)]
131    pub fn __declarations(&self) -> &[Declaration] {
132        &self.declarations
133    }
134}
135
136/// A call's `T`, wrapped so rustc names the bound a handler missed.
137#[derive(Debug)]
138pub struct Once<T>(PhantomData<T>);
139
140/// A subscription's `T`s, as a procedure's [`Output`](Procedure::Output).
141#[derive(Debug)]
142pub struct Strand<T>(PhantomData<T>);
143
144/// An error that can never happen.
145#[derive(Debug)]
146pub enum Never {}
147
148impl Serialize for Never {
149    fn serialize<S: Serializer>(&self, _: S) -> Result<S::Ok, S::Error> {
150        match *self {}
151    }
152}
153
154impl Output for Never {
155    const __SHAPE: Shape = Shape::Never;
156}