restate-sdk 0.10.0

Restate SDK for Rust
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//! # Serialization
//!
//! Restate sends data over the network for storing state, journaling actions, awakeables, etc.
//!
//! Therefore, the types of the values that are stored, need to either:
//! - be a primitive type
//! - use a wrapper type [`Json`] for using [`serde-json`](https://serde.rs/). To enable JSON schema generation, you'll need to enable the `schemars` feature. See [PayloadMetadata] for more details.
//! - have the [`Serialize`] and [`Deserialize`] trait implemented. If you need to use a type for the handler input/output, you'll also need to implement [PayloadMetadata] to reply with correct content type and enable **JSON schema generation**.
//!

use bytes::Bytes;
use std::convert::Infallible;

const APPLICATION_JSON: &str = "application/json";
const APPLICATION_OCTET_STREAM: &str = "application/octet-stream";

/// Serialize trait for Restate services.
///
/// Default implementations are provided for primitives, and you can use the wrapper type [`Json`] to serialize using [`serde_json`].
///
/// This looks similar to [`serde::Serialize`], but allows to plug-in non-serde serialization formats (e.g. like Protobuf using `prost`).
pub trait Serialize {
    type Error: std::error::Error + Send + Sync + 'static;

    fn serialize(&self) -> Result<Bytes, Self::Error>;
}

// TODO perhaps figure out how to add a lifetime here so we can deserialize to borrowed types
/// Deserialize trait for Restate services.
///
/// Default implementations are provided for primitives, and you can use the wrapper type [`Json`] to serialize using [`serde_json`].
///
/// This looks similar to [`serde::Deserialize`], but allows to plug-in non-serde serialization formats (e.g. like Protobuf using `prost`).
pub trait Deserialize
where
    Self: Sized,
{
    type Error: std::error::Error + Send + Sync + 'static;

    fn deserialize(bytes: &mut Bytes) -> Result<Self, Self::Error>;
}

/// ## Payload metadata and Json Schemas
///
/// The SDK propagates during discovery some metadata to restate-server service catalog. This includes:
///
/// * The JSON schema of the payload. See below for more details.
/// * The [InputMetadata] used to instruct restate how to accept requests.
/// * The [OutputMetadata] used to instruct restate how to send responses out.
///
/// There are three approaches for generating JSON Schemas for handler inputs and outputs:
///
/// ### 1. Primitive Types
///
/// Primitive types (like `String`, `u32`, `bool`) have built-in schema implementations
/// that work automatically without additional code:
///
/// ```rust
/// use restate_sdk::prelude::*;
///
/// #[restate_sdk::service]
/// trait SimpleService {
///     async fn greet(name: String) -> HandlerResult<u32>;
/// }
/// ```
///
/// ### 2. Using `Json<T>` with schemars
///
/// For complex types wrapped in `Json<T>`, you need to add the `schemars` feature and derive `JsonSchema`:
///
/// ```rust
/// use restate_sdk::prelude::*;
///
/// #[derive(serde::Serialize, serde::Deserialize, schemars::JsonSchema)]
/// struct User {
///     name: String,
///     age: u32,
/// }
///
/// #[restate_sdk::service]
/// trait UserService {
///     async fn register(user: Json<User>) -> HandlerResult<Json<User>>;
/// }
/// ```
///
/// To enable rich schema generation with `Json<T>`, add the `schemars` feature to your dependency:
///
/// ```toml
/// [dependencies]
/// restate-sdk = { version = "0.8", features = ["schemars"] }
/// schemars = "1.0.0-alpha.17"
/// ```
///
/// ### 3. Custom Implementation
///
/// You can also implement the [PayloadMetadata] trait directly for your types to provide
/// custom schemas without relying on the `schemars` feature:
///
/// ```rust
/// use restate_sdk::serde::{PayloadMetadata, Serialize, Deserialize};
///
/// #[derive(serde::Serialize, serde::Deserialize)]
/// struct User {
///     name: String,
///     age: u32,
/// }
///
/// // Implement PayloadMetadata directly and override the json_schema implementation
/// impl PayloadMetadata for User {
///     fn json_schema() -> Option<serde_json::Value> {
///         Some(serde_json::json!({
///             "type": "object",
///             "properties": {
///                 "name": {"type": "string"},
///                 "age": {"type": "integer", "minimum": 0}
///             },
///             "required": ["name", "age"]
///         }))
///     }
/// }
/// ```
///
/// Trait encapsulating JSON Schema information for the given serializer/deserializer.
///
/// This trait allows types to provide JSON Schema information that can be used for
/// documentation, validation, and client generation.
///
/// ## Behavior with `schemars` Feature Flag
///
/// When the `schemars` feature is enabled, implementations for complex types use
/// the `schemars` crate to automatically generate rich, JSON Schema 2020-12 conforming schemas.
/// When the feature is disabled, primitive types still provide basic schemas,
/// but complex types return empty schemas, unless manually implemented.
pub trait PayloadMetadata {
    /// Generate a JSON Schema for this type.
    ///
    /// Returns a JSON value representing the schema for this type. When the `schemars`
    /// feature is enabled, this returns an auto-generated JSON Schema 2020-12 conforming schema. When the feature is disabled,
    /// this returns an empty schema for complex types, but basic schemas for primitives.
    ///
    /// If returns none, no schema is provided. This should be used when the payload is not expected to be json
    fn json_schema() -> Option<serde_json::Value> {
        Some(serde_json::Value::Object(serde_json::Map::default()))
    }

    /// Returns the [InputMetadata]. The default implementation returns metadata suitable for JSON payloads.
    fn input_metadata() -> InputMetadata {
        InputMetadata::default()
    }

    /// Returns the [OutputMetadata]. The default implementation returns metadata suitable for JSON payloads.
    fn output_metadata() -> OutputMetadata {
        OutputMetadata::default()
    }
}

/// This struct encapsulates input payload metadata used by discovery.
///
/// The default implementation works well with Json payloads.
pub struct InputMetadata {
    /// Content type of the input. It can accept wildcards, in the same format as the 'Accept' header.
    ///
    /// By default, is `application/json`.
    pub accept_content_type: &'static str,
    /// If true, Restate itself will reject requests **without content-types**.
    pub is_required: bool,
}

impl Default for InputMetadata {
    fn default() -> Self {
        Self {
            accept_content_type: APPLICATION_JSON,
            is_required: true,
        }
    }
}

/// This struct encapsulates output payload metadata used by discovery.
///
/// The default implementation works for Json payloads.
pub struct OutputMetadata {
    /// Content type of the output.
    ///
    /// By default, is `application/json`.
    pub content_type: &'static str,
    /// If true, the specified content-type is set even if the output is empty. This should be set to `true` only for encodings that can return a serialized empty byte array (e.g. Protobuf).
    pub set_content_type_if_empty: bool,
}

impl Default for OutputMetadata {
    fn default() -> Self {
        Self {
            content_type: APPLICATION_JSON,
            set_content_type_if_empty: false,
        }
    }
}

// --- Default implementation for Unit type

impl Serialize for () {
    type Error = Infallible;

    fn serialize(&self) -> Result<Bytes, Self::Error> {
        Ok(Bytes::new())
    }
}

impl Deserialize for () {
    type Error = Infallible;

    fn deserialize(_: &mut Bytes) -> Result<Self, Self::Error> {
        Ok(())
    }
}

// --- Passthrough implementation

impl Serialize for Vec<u8> {
    type Error = Infallible;

    fn serialize(&self) -> Result<Bytes, Self::Error> {
        Ok(Bytes::copy_from_slice(self))
    }
}

impl Deserialize for Vec<u8> {
    type Error = Infallible;

    fn deserialize(b: &mut Bytes) -> Result<Self, Self::Error> {
        Ok(b.to_vec())
    }
}

impl PayloadMetadata for Vec<u8> {
    fn json_schema() -> Option<serde_json::Value> {
        None
    }

    fn input_metadata() -> InputMetadata {
        InputMetadata {
            accept_content_type: "*/*",
            is_required: true,
        }
    }

    fn output_metadata() -> OutputMetadata {
        OutputMetadata {
            content_type: APPLICATION_OCTET_STREAM,
            set_content_type_if_empty: false,
        }
    }
}

impl Serialize for Bytes {
    type Error = Infallible;

    fn serialize(&self) -> Result<Bytes, Self::Error> {
        Ok(self.clone())
    }
}

impl Deserialize for Bytes {
    type Error = Infallible;

    fn deserialize(b: &mut Bytes) -> Result<Self, Self::Error> {
        Ok(b.clone())
    }
}

impl PayloadMetadata for Bytes {
    fn json_schema() -> Option<serde_json::Value> {
        None
    }

    fn input_metadata() -> InputMetadata {
        InputMetadata {
            accept_content_type: "*/*",
            is_required: true,
        }
    }

    fn output_metadata() -> OutputMetadata {
        OutputMetadata {
            content_type: APPLICATION_OCTET_STREAM,
            set_content_type_if_empty: false,
        }
    }
}
// --- Option implementation

impl<T: Serialize> Serialize for Option<T> {
    type Error = T::Error;

    fn serialize(&self) -> Result<Bytes, Self::Error> {
        if self.is_none() {
            return Ok(Bytes::new());
        }
        T::serialize(self.as_ref().unwrap())
    }
}

impl<T: Deserialize> Deserialize for Option<T> {
    type Error = T::Error;

    fn deserialize(b: &mut Bytes) -> Result<Self, Self::Error> {
        if b.is_empty() {
            return Ok(None);
        }
        T::deserialize(b).map(Some)
    }
}

impl<T: PayloadMetadata> PayloadMetadata for Option<T> {
    fn input_metadata() -> InputMetadata {
        InputMetadata {
            accept_content_type: T::input_metadata().accept_content_type,
            is_required: false,
        }
    }

    fn output_metadata() -> OutputMetadata {
        OutputMetadata {
            content_type: T::output_metadata().content_type,
            set_content_type_if_empty: false,
        }
    }
}

// --- Primitives

macro_rules! impl_integer_primitives {
    ($ty:ty) => {
        impl Serialize for $ty {
            type Error = serde_json::Error;

            fn serialize(&self) -> Result<Bytes, Self::Error> {
                serde_json::to_vec(&self).map(Bytes::from)
            }
        }

        impl Deserialize for $ty {
            type Error = serde_json::Error;

            fn deserialize(bytes: &mut Bytes) -> Result<Self, Self::Error> {
                serde_json::from_slice(&bytes)
            }
        }

        impl PayloadMetadata for $ty {
            fn json_schema() -> Option<serde_json::Value> {
                let min = <$ty>::MIN;
                let max = <$ty>::MAX;
                Some(serde_json::json!({ "type": "integer", "minimum": min, "maximum": max }))
            }
        }
    };
}

impl_integer_primitives!(u8);
impl_integer_primitives!(u16);
impl_integer_primitives!(u32);
impl_integer_primitives!(u64);
impl_integer_primitives!(u128);
impl_integer_primitives!(i8);
impl_integer_primitives!(i16);
impl_integer_primitives!(i32);
impl_integer_primitives!(i64);
impl_integer_primitives!(i128);

macro_rules! impl_serde_primitives {
    ($ty:ty) => {
        impl Serialize for $ty {
            type Error = serde_json::Error;

            fn serialize(&self) -> Result<Bytes, Self::Error> {
                serde_json::to_vec(&self).map(Bytes::from)
            }
        }

        impl Deserialize for $ty {
            type Error = serde_json::Error;

            fn deserialize(bytes: &mut Bytes) -> Result<Self, Self::Error> {
                serde_json::from_slice(&bytes)
            }
        }
    };
}

impl_serde_primitives!(String);
impl_serde_primitives!(bool);
impl_serde_primitives!(f32);
impl_serde_primitives!(f64);

impl PayloadMetadata for String {
    fn json_schema() -> Option<serde_json::Value> {
        Some(serde_json::json!({ "type": "string" }))
    }
}

impl PayloadMetadata for bool {
    fn json_schema() -> Option<serde_json::Value> {
        Some(serde_json::json!({ "type": "boolean" }))
    }
}

impl PayloadMetadata for f32 {
    fn json_schema() -> Option<serde_json::Value> {
        Some(serde_json::json!({ "type": "number" }))
    }
}

impl PayloadMetadata for f64 {
    fn json_schema() -> Option<serde_json::Value> {
        Some(serde_json::json!({ "type": "number" }))
    }
}

// --- Json wrapper

/// Wrapper type to use [`serde_json`] with Restate's [`Serialize`]/[`Deserialize`] traits.
pub struct Json<T>(pub T);

impl<T> Json<T> {
    pub fn into_inner(self) -> T {
        self.0
    }
}

impl<T> From<T> for Json<T> {
    fn from(value: T) -> Self {
        Self(value)
    }
}

impl<T> Serialize for Json<T>
where
    T: serde::Serialize,
{
    type Error = serde_json::Error;

    fn serialize(&self) -> Result<Bytes, Self::Error> {
        serde_json::to_vec(&self.0).map(Bytes::from)
    }
}

impl<T> Deserialize for Json<T>
where
    for<'a> T: serde::Deserialize<'a>,
{
    type Error = serde_json::Error;

    fn deserialize(bytes: &mut Bytes) -> Result<Self, Self::Error> {
        serde_json::from_slice(bytes).map(Json)
    }
}

impl<T: Default> Default for Json<T> {
    fn default() -> Self {
        Self(T::default())
    }
}

// When schemars is disabled - works with any T
#[cfg(not(feature = "schemars"))]
impl<T> PayloadMetadata for Json<T> {
    fn json_schema() -> Option<serde_json::Value> {
        Some(serde_json::json!({}))
    }
}

// When schemars is enabled - requires T: JsonSchema
#[cfg(feature = "schemars")]
impl<T: schemars::JsonSchema> PayloadMetadata for Json<T> {
    fn json_schema() -> Option<serde_json::Value> {
        Some(schemars::schema_for!(T).to_value())
    }
}