Expand description
§cgp-serde: Serde’s traits as CGP components
cgp-serde defines Serde’s Serialize and Deserialize as components of
Context-Generic Programming (CGP), a language extension for Rust with
pluggable trait implementations at compile-time. Each application is a context type whose wiring
chooses how every type it serializes is encoded, so two applications can write the same value
differently. This crate holds the two components, the adapters that hand a context to Serde, and the
core providers.
Project page · Reference · Repository
Here two contexts write the same order, one with its id as a JSON number and one with the id as a string, as some JavaScript clients need for large ids:
use cgp::prelude::*;
use cgp_serde::components::{CanSerializeValue, ValueSerializerComponent};
use cgp_serde::providers::{SerializeRecordFields, SerializeWithDisplay, UseSerde};
use cgp_serde::types::SerializeWithContext;
#[derive(CgpData)]
pub struct Order {
pub id: u64,
pub item: String,
}
pub struct Api;
pub struct WebClient;
delegate_components! {
Api {
open ValueSerializerComponent;
@ValueSerializerComponent.[u64, String]: UseSerde,
@ValueSerializerComponent.Order: SerializeRecordFields,
}
}
delegate_components! {
WebClient {
open ValueSerializerComponent;
@ValueSerializerComponent.u64: SerializeWithDisplay,
@ValueSerializerComponent.String: UseSerde,
@ValueSerializerComponent.Order: SerializeRecordFields,
}
}
check_components! { Api { ValueSerializerComponent: Order } }
check_components! { WebClient { ValueSerializerComponent: Order } }
fn to_json<Context>(context: &Context, order: &Order) -> String
where
Context: CanSerializeValue<Order>,
{
serde_json::to_string(&SerializeWithContext::new(context, order)).unwrap()
}
fn main() {
let order = Order { id: 7, item: "book".to_owned() };
assert_eq!(to_json(&Api, &order), r#"{"id":7,"item":"book"}"#);
assert_eq!(to_json(&WebClient, &order), r#"{"id":"7","item":"book"}"#);
}Order derives only CGP’s general-purpose CgpData, and SerializeRecordFields writes it as a map
by asking the context how to encode each field. SerializeWithDisplay formats the id with its
Display impl and asks the context to encode the resulting String, which both contexts wire to
UseSerde. The check_components! lines make the compiler verify each context’s whole table.
§What the crate holds
The crate is no_std and links alloc. Its items sit in three modules:
components:CanSerializeValue<Value>andCanDeserializeValue<'de, Value>, the consumer traits callers use, with the provider traitsValueSerializerandValueDeserializerand the wiring keysValueSerializerComponentandValueDeserializerComponent.types:SerializeWithContext, aSerializevalue, andDeserializeWithContext, aDeserializeSeed, which pass a value and its context to any Serde format.providers: the implementations a context wires for each type.
The providers fall into these families:
- A type’s own Serde impls:
UseSerde. - Strings and bytes:
SerializeString,SerializeBytes, andTryDeserializeBytes. - Conversions:
SerializeWithDisplay,DeserializeWithFromStr,SerializeFrom,TrySerializeFrom, andSerializeDeref. - Collections:
SerializeIteratorandDeserializeExtend. - Structs with named fields:
SerializeRecordFieldsandDeserializeRecordFields. - Enums:
SerializeVariantFields,DeserializeVariantFields, andSerializeUnitfor a variant with no fields. - Defaults:
DeserializeDefault, which reads a null as the type’s default.
Four more crates build on this one: cgp-serde-extra for byte
and date encodings, cgp-serde-json for JSON operations a context
wires, and cgp-serde-alloc and
cgp-serde-typed-arena for deserializing borrowed values
into an arena. The project page explains the
design and its costs, with a reference page for each provider.