leptoskit_rpc 0.3.0

The app's operations: leptos server functions with retries and error classification, queries and mutations that run on the database, one client for every platform, and publishing it all to generated clients.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//! Operations: what an app exposes, by name and kind, with typed arguments
//! and a typed result.

use std::fmt;

use serde::Serialize;
use serde::de::DeserializeOwned;

use crate::FnError;

/// What kind of operation something is, which decides how it runs and how
/// clients may call it.
///
/// | Kind                    | Declared with      | Runs                                                                 |
/// | ----------------------- | ------------------ | -------------------------------------------------------------------- |
/// | [`Call`](Self::Call)    | `#[server]`        | as a leptos server function, with its own encoding                   |
/// | [`Query`](Self::Query)  | `#[rpc::query]`    | reading the database as the caller, read-only by type                |
/// | [`Mutation`](Self::Mutation) | `#[rpc::mutation]` | in one serializable transaction, retried when another gets in its way |
/// | [`Action`](Self::Action) | `#[rpc::action]` | as the caller, outside any transaction, once: it may call anything |
/// | [`Job`](Self::Job)         | `#[rpc::job]`      | later, in the background, when something enqueues it |
/// | [`Stream`](Self::Stream)   | `#[rpc::stream]`   | as the caller, sending items over the live connection as the client has room |
///
/// More kinds may join these, so matching on a `Kind` needs a wildcard arm.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Kind {
	/// A leptos `#[server]` function.
	Call,
	/// An `#[rpc::query]`.
	Query,
	/// An `#[rpc::mutation]`.
	Mutation,
	/// An `#[rpc::action]`: not transactional and never retried, so it may
	/// call the outside world; it reaches the database through queries and
	/// mutations.
	Action,
	/// An `#[rpc::job]`: work that runs later, in the background, not a
	/// route a client calls.
	Job,
	/// An `#[rpc::stream]`: items over time on the live connection, such as
	/// a model's tokens or a log tail, sent only as fast as the client
	/// takes them.
	Stream,
}

impl Kind {
	/// The kind's name in the spec: `call`, `query`, `mutation`, `action`,
	/// `job`, or `stream`.
	#[must_use]
	pub const fn as_str(self) -> &'static str {
		match self {
			Self::Call => "call",
			Self::Query => "query",
			Self::Mutation => "mutation",
			Self::Action => "action",
			Self::Job => "job",
			Self::Stream => "stream",
		}
	}
}

impl fmt::Display for Kind {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		f.write_str(self.as_str())
	}
}

/// An operation's arguments, as one value: the struct `#[rpc::query]` and
/// `#[rpc::mutation]` generate from the function's arguments, named in
/// `PascalCase`.
///
/// The struct exists on every target, so any client can build one and send
/// it with [`client::call`](crate::client::call). Only a server build (the
/// `server` feature) knows how to [run](Self::run) it.
///
/// ```ignore
/// #[rpc::query]
/// pub async fn messages(cx: &rpc::QueryCx, channel: Id<Channel>, limit: u32) -> rpc::Result<Vec<PublicMessage>> { … }
///
/// // Anywhere: the arguments, by name.
/// let latest = rpc::client::call(Messages { channel, limit: 50 }).await?;
/// ```
pub trait Operation: Serialize + DeserializeOwned + Clone + Send + Sync + 'static {
	/// The operation's name: its route is `/api/<NAME>`.
	const NAME: &'static str;
	/// What kind of operation it is.
	const KIND: Kind;
	/// What a successful call returns.
	type Output: Serialize + DeserializeOwned + Send + Sync + 'static;

	/// What the body receives: [`QueryCx`](crate::QueryCx) or
	/// [`MutationCx`](crate::MutationCx).
	#[cfg(feature = "server")]
	type Context: crate::server::OperationContext;

	/// How many more times a mutation runs when another transaction gets in
	/// its way: `#[rpc::mutation(retries = N)]`, five by default. Queries
	/// never retry.
	#[cfg(feature = "server")]
	const RETRIES: u32 = 5;

	/// Runs the body. Prefer [`call`](crate::call), which sets up the
	/// context: a mutation's transaction, its retries, and its receipt.
	#[cfg(feature = "server")]
	fn run(self, cx: &Self::Context) -> impl Future<Output = Result<Self::Output>> + Send;
}

/// What an operation's body returns.
pub type Result<T, E = Error> = std::result::Result<T, E>;

/// Why an operation failed, as its body reports it.
///
/// Every variant reaches the caller with an HTTP status and the message,
/// classified as [`FnError`] classifies a server function's failure:
/// [`Invalid`](Self::Invalid) as [`FnError::Invalid`], the rest as
/// [`FnError::Server`]. So generated clients and the app's own client handle
/// them as they handle any server function's.
///
/// A database error converts with `?`: a row that isn't there becomes
/// [`NotFound`](Self::NotFound), a policy's refusal
/// [`Forbidden`](Self::Forbidden), a value that breaks a rule
/// [`Invalid`](Self::Invalid), a taken unique value or a changed row
/// [`Conflict`](Self::Conflict), and anything else a
/// [`Server`](Self::Server) error that says nothing of the database.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
	/// The arguments were wrong: `400`.
	Invalid(String),
	/// The caller must sign in first: `401`.
	Unauthorized(String),
	/// The caller may not do this: `403`.
	Forbidden(String),
	/// What the caller asked about doesn't exist, or they can't see it: `404`.
	NotFound(String),
	/// The request clashes with what is there: a taken name, a changed row:
	/// `409`.
	Conflict(String),
	/// Another transaction kept getting in the mutation's way, so it gave up
	/// after its retries: `409`. Trying again later can succeed.
	Contended,
	/// The server failed: `500`.
	Server(String),
	/// Something outside the server failed for now, such as a network call:
	/// `503`. A job that fails with it runs again after its backoff, as it
	/// does for any error but [`Permanent`](Self::Permanent).
	Retry(String),
	/// Trying again won't help, such as a payment the provider declined:
	/// `500`. A job that fails with it stops at once, without its remaining
	/// retries, and its `on_failure` mutation runs.
	Permanent(String),
}

impl Error {
	/// The arguments were wrong.
	pub fn invalid(message: impl Into<String>) -> Self {
		Self::Invalid(message.into())
	}

	/// The caller must sign in first.
	pub fn unauthorized(message: impl Into<String>) -> Self {
		Self::Unauthorized(message.into())
	}

	/// The caller may not do this.
	pub fn forbidden(message: impl Into<String>) -> Self {
		Self::Forbidden(message.into())
	}

	/// What the caller asked about doesn't exist.
	pub fn not_found(message: impl Into<String>) -> Self {
		Self::NotFound(message.into())
	}

	/// The request clashes with what is there.
	pub fn conflict(message: impl Into<String>) -> Self {
		Self::Conflict(message.into())
	}

	/// The server failed.
	pub fn server(message: impl Into<String>) -> Self {
		Self::Server(message.into())
	}

	/// Something outside failed for now: a job runs again after its backoff.
	pub fn retry(message: impl Into<String>) -> Self {
		Self::Retry(message.into())
	}

	/// Trying again won't help: a job stops without its remaining retries.
	pub fn permanent(message: impl Into<String>) -> Self {
		Self::Permanent(message.into())
	}

	/// Whether a job that failed with this should stop at once.
	#[must_use]
	pub fn is_permanent(&self) -> bool {
		matches!(self, Self::Permanent(_))
	}

	/// The HTTP status a caller receives.
	#[must_use]
	pub fn status(&self) -> u16 {
		match self {
			Self::Invalid(_) => 400,
			Self::Unauthorized(_) => 401,
			Self::Forbidden(_) => 403,
			Self::NotFound(_) => 404,
			Self::Conflict(_) | Self::Contended => 409,
			Self::Server(_) | Self::Permanent(_) => 500,
			Self::Retry(_) => 503,
		}
	}

	/// The message a caller reads.
	#[must_use]
	pub fn message(&self) -> &str {
		match self {
			Self::Invalid(message)
			| Self::Unauthorized(message)
			| Self::Forbidden(message)
			| Self::NotFound(message)
			| Self::Conflict(message)
			| Self::Server(message)
			| Self::Retry(message)
			| Self::Permanent(message) => message,
			Self::Contended => "the server was too busy to finish this; try again",
		}
	}

	/// Whether another transaction got in the way, so running the mutation
	/// again can succeed.
	#[must_use]
	pub fn is_contended(&self) -> bool {
		matches!(self, Self::Contended)
	}

	/// The body a caller receives: `Kind|message`, where `Kind` is the
	/// `ServerFnError` variant generated clients classify, as
	/// [`FnError::from_wire`] reads it.
	#[must_use]
	pub fn to_wire(&self) -> String {
		let kind = match self {
			Self::Invalid(_) => "Args",
			_ => "ServerError",
		};

		format!("{kind}|{}", self.message())
	}
}

impl fmt::Display for Error {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		f.write_str(self.message())
	}
}

impl std::error::Error for Error {}

impl From<Error> for FnError {
	fn from(error: Error) -> Self {
		Self::from_wire(&error.to_wire())
	}
}

#[cfg(feature = "db")]
impl From<leptoskit_db::DbError> for Error {
	fn from(error: leptoskit_db::DbError) -> Self {
		use leptoskit_db::DbError;

		if error.is_contended() {
			return Self::Contended;
		}

		match error {
			DbError::NotFound { .. } => Self::NotFound(error.to_string()),
			DbError::Forbidden { message, .. } => Self::Forbidden(message),
			DbError::Invalid(_) | DbError::Check { .. } | DbError::ForeignKey { .. } => {
				Self::Invalid(error.to_string())
			}
			DbError::Conflict { .. } | DbError::Stale { .. } => Self::Conflict(error.to_string()),
			// The rest are the server's own failures, and the caller hears
			// only that something went wrong.
			_ => Self::Server("something went wrong; try again".to_owned()),
		}
	}
}

/// Arguments an operation can take: they cross the wire as JSON, and the
/// spec describes them.
#[diagnostic::on_unimplemented(
	message = "`{Self}` can't be an operation's argument",
	label = "arguments must implement `Serialize`, `Deserialize`, and `JsonSchema`",
	note = "an operation's arguments travel as JSON and are described in the RPC spec; derive \
	        `serde::Serialize`, `serde::Deserialize`, and `schemars::JsonSchema` for it"
)]
#[doc(hidden)]
pub trait Argument: Serialize + DeserializeOwned + schemars::JsonSchema {}

impl<T: Serialize + DeserializeOwned + schemars::JsonSchema> Argument for T {}

/// Results an operation can return: they cross the wire as JSON.
#[diagnostic::on_unimplemented(
	message = "`{Self}` can't be an operation's result",
	label = "results must implement `Serialize` and `Deserialize`",
	note = "an operation's result travels back as JSON; derive `serde::Serialize` and \
	        `serde::Deserialize` for it"
)]
#[doc(hidden)]
pub trait Output: Serialize + DeserializeOwned + Send + Sync + 'static {}

impl<T: Serialize + DeserializeOwned + Send + Sync + 'static> Output for T {}

/// Checks an argument type, where the macro names it.
#[doc(hidden)]
pub const fn assert_argument<T: Argument>() {}

/// Checks a result type, where the macro names it.
#[doc(hidden)]
pub const fn assert_output<T: Output>() {}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn errors_reach_callers_classified_like_server_functions() {
		let cases = [
			(Error::invalid("too long"), 400, "Args|too long"),
			(Error::unauthorized("sign in"), 401, "ServerError|sign in"),
			(Error::forbidden("not yours"), 403, "ServerError|not yours"),
			(
				Error::not_found("no such post"),
				404,
				"ServerError|no such post",
			),
			(Error::conflict("taken"), 409, "ServerError|taken"),
			(Error::server("down"), 500, "ServerError|down"),
		];

		for (error, status, wire) in cases {
			assert_eq!(error.status(), status);
			assert_eq!(error.to_wire(), wire);
			assert_eq!(error.to_string(), error.message());
			assert!(!error.is_contended());
		}

		assert_eq!(Error::Contended.status(), 409);
		assert!(Error::Contended.is_contended());

		assert!(Error::Contended.to_wire().starts_with("ServerError|"));
		assert_eq!(
			FnError::from(Error::invalid("bad")),
			FnError::Invalid("bad".to_owned())
		);
		assert_eq!(
			FnError::from(Error::not_found("gone")),
			FnError::Server("gone".to_owned())
		);
	}

	#[test]
	fn kinds_have_their_spec_names() {
		assert_eq!(Kind::Call.to_string(), "call");
		assert_eq!(Kind::Query.as_str(), "query");
		assert_eq!(Kind::Mutation.as_str(), "mutation");
		assert_eq!(Kind::Action.as_str(), "action");
		assert_eq!(Kind::Job.as_str(), "job");
		assert_eq!(Kind::Stream.as_str(), "stream");
	}

	#[test]
	fn jobs_tell_retrying_from_giving_up() {
		let retry = Error::retry("the mail server is down");
		assert_eq!(retry.status(), 503);
		assert_eq!(retry.message(), "the mail server is down");
		assert!(!retry.is_permanent());
		let permanent = Error::permanent("declined");
		assert_eq!(permanent.status(), 500);
		assert_eq!(permanent.to_wire(), "ServerError|declined");
		assert!(permanent.is_permanent());

		assert!(!Error::server("down").is_permanent());
	}

	#[cfg(feature = "db")]
	#[test]
	fn database_errors_say_nothing_of_the_database() {
		use leptoskit_db::DbError;

		assert!(matches!(
			Error::from(DbError::NotFound {
				table: "posts".to_owned()
			}),
			Error::NotFound(_)
		));
		assert_eq!(
			Error::from(DbError::Forbidden {
				table: "posts".to_owned(),
				message: "Only the author can change a post".to_owned(),
			}),
			Error::forbidden("Only the author can change a post")
		);
		assert!(matches!(
			Error::from(DbError::Stale {
				table: "posts".to_owned()
			}),
			Error::Conflict(_)
		));
		assert_eq!(
			Error::from(DbError::Query("relation \"posts\" has a secret".into())),
			Error::server("something went wrong; try again")
		);
	}
}