Expand description
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
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);
Traits§
- AsJson
json
- Marker trait for auto serialization/deserialization.
- From
Reader - Trait for conversion from raw incoming invocation to event type
- Into
Bytes - Trait for converting response into raw bytes.