With schemars, it is possible to generate a JSON schema from Rust code.
Consider this JSON example:
{
"cause" : {
"type" : "EVT1",
"payload" : {
"cause_id" : "ABC-123"
}
}
}
and the Rust code used to generate the schema:
use schemar::JsonSchema;
use serde::Serialize;
#[derive(Serialize, JsonSchema)]
struct Cause {
_type: EventType,
payload: EventPayload,
}
#[derive(Serialize, JsonSchema)]
enum EventType {
EVT1 { param1: String, param2: String },
EVT2 { param1: String },
}
#[derive(Serialize, JsonSchema)]
enum EventPayload {
// ?
}
EventType
contains the type of event in the name of the variant, and the payload is specified by EventType
as well. EventPayload
is superfluous.
Is there any possibility to configure serde so that the name of the EventType
variant will be used as type
, the contents of it as payload
? The payload
depends on EventType
.
What do I expect?
_type
shall be restricted to the variants ofEventType
- the
payload
ofcause
is directly dependant of_type
- Ideally, the schema generation would use the name of
EventType
variant as_type
, and take thepayload
from the variants contents.