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
//! Module that contains traits for converting incoming payload to `Event` and `Response` to
//! outgoing payload.
//!
//! # Overview
//! In order for the compiler to automatically deserialize an incoming event to some type, such
//! type must implement [`FromReader`]. Similarly a type must implement [`IntoBytes`] if one wishes
//! to serialize the type as a response.
//!
//! ## Example
//!
//! Here is an example of a webservice that capitalize all the letters of the input
//! ```no_run
//! use tencent_scf::{
//! convert::{FromReader, IntoBytes},
//! make_scf, start, Context,
//! };
//!
//! // Our custom event type
//! struct Event(String);
//!
//! // Simply read everything from the reader.
//! impl FromReader for Event {
//! type Error = std::io::Error;
//!
//! fn from_reader<Reader: std::io::Read + Send>(
//! mut reader: Reader,
//! ) -> Result<Self, Self::Error> {
//! let mut buf = String::new();
//! reader.read_to_string(&mut buf)?;
//! Ok(Event(buf))
//! }
//! }
//!
//! // Our custom response type
//! struct Response(String);
//!
//! // Simply turn a string into a byte array.
//! impl IntoBytes for Response {
//! type Error = std::convert::Infallible;
//!
//! fn into_bytes(self) -> Result<Vec<u8>, Self::Error> {
//! Ok(String::into_bytes(self.0))
//! }
//! }
//!
//! let scf = make_scf(
//! |event: Event, _context: Context| -> Result<Response, std::convert::Infallible> {
//! // capitalize the string
//! Ok(Response(event.0.to_uppercase()))
//! },
//! );
//! start(scf);
//! ```
use ;
pub use AsJson;
/// Marker trait for auto serialization/deserialization.
///
/// Many types may implement [`serde::Deserialize`] and/or [`serde::Serialize`] but it may not
/// always be the case that such type should be serialized/deserialized to/from JSON when runtime
/// receives a new event or to send a response. So user can have the runtime to do the
/// JSON conversion by marking the type as `AsJson`.
///
/// # Derivable
/// This trait can be used with `#[derive]`. When `derived`, no extra method is added, but the
/// compiler knows this type should be auto serialized/deserialized.
///
/// # Example
/// ```no_run
/// # #[cfg(feature = "json")]
/// # {
/// use std::convert::Infallible;
///
/// use serde::{Deserialize, Serialize};
/// use tencent_scf::{convert::AsJson, make_scf, start, Context};
///
/// #[derive(Deserialize, AsJson)]
/// struct MyEvent {
/// name: String,
/// width: usize,
/// height: usize,
/// }
///
/// #[derive(Serialize, AsJson)]
/// struct MyResponse {
/// area: usize,
/// }
///
/// let scf = make_scf(
/// |event: MyEvent, _context: Context| -> Result<MyResponse, Infallible> {
/// Ok(MyResponse {
/// area: event.width * event.height,
/// })
/// },
/// );
/// start(scf);
/// # }
/// ```
/// [`serde_json::Value`] can be used as event and response.
///
/// # Example
/// ```no_run
/// # #[cfg(feature = "json")]
/// # {
/// use serde_json::{json, Value};
/// use tencent_scf::{convert::AsJson, make_scf, Context};
///
/// type Error = Box<dyn std::error::Error + Send + Sync + 'static>;
///
/// fn main() {
/// let func = make_scf(func);
/// tencent_scf::start(func);
/// }
///
/// fn func(event: Value, _: Context) -> Result<Value, Error> {
/// let first_name = event["firstName"].as_str().unwrap_or("world");
///
/// Ok(json!({ "message": format!("Hello, {}!", first_name) }))
/// }
/// # }
/// ```
/// Trait for conversion from raw incoming invocation to event type
///
/// Custom event should implement this trait so that the runtime can deserialize the incoming
/// invocation into the desired event.
///
/// # Implement the Trait
/// One needs to provide a method that can consume a raw byte reader into the desired type.
/// ```
/// use tencent_scf::convert::FromReader;
///
/// // Our custom event type
/// struct Event(String);
///
/// // Simply read everything from the reader.
/// impl FromReader for Event {
/// type Error = std::io::Error;
///
/// fn from_reader<Reader: std::io::Read + Send>(
/// mut reader: Reader,
/// ) -> Result<Self, Self::Error> {
/// let mut buf = String::new();
/// reader.read_to_string(&mut buf)?;
/// Ok(Event(buf))
/// }
/// }
/// ```
/// Auto deserilization for types that are [`serde::Deserialize`] and marked as [`AsJson`].
/// Auto deserialization into a byte array.
/// Auto deserialization into a string.
/// Trait for converting response into raw bytes.
///
/// Custom response should implement this trait so that runtime can send raw bytes to the upstream
/// cloud.
///
/// # Implement the Trait
/// One needs to provide a method that can consume the object and produce a byte array.
/// ```
/// use tencent_scf::convert::IntoBytes;
///
/// // Our custom response type
/// struct Response(String);
///
/// // Simply turn a string into a byte array.
/// impl IntoBytes for Response {
/// type Error = std::convert::Infallible;
///
/// fn into_bytes(self) -> Result<Vec<u8>, Self::Error> {
/// Ok(String::into_bytes(self.0))
/// }
/// }
/// ```
/// Auto serilization for types that are [`serde::Serialize`] and marked as [`AsJson`].
/// Auto serialization for byte array (identity map).
/// Auto serilization for string.