1use 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
18pub trait Procedure: Request {
49 type Output;
51
52 type Error;
54}
55
56pub trait IntoReply {
58 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
70pub 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#[derive(Debug)]
85pub enum ReplyShape {
86 Once(Shape),
88
89 Strand(Shape),
91}
92
93#[derive(Debug, Default)]
95pub struct Procedures {
96 pub(crate) declarations: Vec<Declaration>,
97}
98
99#[derive(Debug)]
101pub struct Declaration {
102 pub named: fn() -> &'static Named,
104
105 pub reply: &'static ReplyShape,
107
108 pub error: &'static Shape,
110}
111
112impl Procedures {
113 pub fn new() -> Self {
115 Self::default()
116 }
117
118 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#[derive(Debug)]
138pub struct Once<T>(PhantomData<T>);
139
140#[derive(Debug)]
142pub struct Strand<T>(PhantomData<T>);
143
144#[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}