|
| 1 | +use crate::schema::v2::{ComponentSpec, Map, OneOrManyComponentSpecs}; |
| 2 | +use schemars::JsonSchema; |
| 3 | + |
| 4 | +// The structs here allow dead code because they exist only |
| 5 | +// to represent JSON schemas, and are never instantiated. |
| 6 | + |
| 7 | +#[allow(dead_code)] |
| 8 | +#[derive(JsonSchema)] |
| 9 | +pub struct TriggerSchema { |
| 10 | + /// HTTP triggers |
| 11 | + #[schemars(default)] |
| 12 | + http: Vec<HttpTriggerSchema>, |
| 13 | + /// Redis triggers |
| 14 | + #[schemars(default)] |
| 15 | + redis: Vec<RedisTriggerSchema>, |
| 16 | +} |
| 17 | + |
| 18 | +#[allow(dead_code)] |
| 19 | +#[derive(JsonSchema)] |
| 20 | +#[schemars(deny_unknown_fields)] |
| 21 | +pub struct HttpTriggerSchema { |
| 22 | + /// `id = "trigger-id"` |
| 23 | + #[serde(default, skip_serializing_if = "String::is_empty")] |
| 24 | + pub id: String, |
| 25 | + /// `component = ...` |
| 26 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 27 | + pub component: Option<ComponentSpec>, |
| 28 | + /// `components = { ... }` |
| 29 | + #[serde(default, skip_serializing_if = "Map::is_empty")] |
| 30 | + pub components: Map<String, OneOrManyComponentSpecs>, |
| 31 | + /// `route = "/user/:name/..."` |
| 32 | + route: HttpRouteSchema, |
| 33 | + /// `executor = { type = "wagi" } |
| 34 | + #[schemars(default, schema_with = "toml_table")] |
| 35 | + executor: Option<toml::Table>, |
| 36 | +} |
| 37 | + |
| 38 | +#[allow(dead_code)] |
| 39 | +#[derive(JsonSchema)] |
| 40 | +#[schemars(untagged)] |
| 41 | +pub enum HttpRouteSchema { |
| 42 | + /// `route = "/user/:name/..."` |
| 43 | + Route(String), |
| 44 | + /// `route = { private = true }` |
| 45 | + Private(HttpPrivateEndpoint), |
| 46 | +} |
| 47 | + |
| 48 | +#[allow(dead_code)] |
| 49 | +#[derive(JsonSchema)] |
| 50 | +#[schemars(deny_unknown_fields)] |
| 51 | +pub struct HttpPrivateEndpoint { |
| 52 | + /// Whether the private endpoint is private. This must be true. |
| 53 | + pub private: bool, |
| 54 | +} |
| 55 | + |
| 56 | +#[allow(dead_code)] |
| 57 | +#[derive(JsonSchema)] |
| 58 | +#[schemars(deny_unknown_fields)] |
| 59 | +pub struct RedisTriggerSchema { |
| 60 | + /// `id = "trigger-id"` |
| 61 | + #[serde(default, skip_serializing_if = "String::is_empty")] |
| 62 | + pub id: String, |
| 63 | + /// `component = ...` |
| 64 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 65 | + pub component: Option<ComponentSpec>, |
| 66 | + /// `components = { ... }` |
| 67 | + #[serde(default, skip_serializing_if = "Map::is_empty")] |
| 68 | + pub components: Map<String, OneOrManyComponentSpecs>, |
| 69 | + /// `channel = "my-messages"` |
| 70 | + channel: String, |
| 71 | + /// `address = "redis://redis.example.com:6379"` |
| 72 | + #[serde(default, skip_serializing_if = "Option::is_none")] |
| 73 | + address: Option<String>, |
| 74 | +} |
| 75 | + |
| 76 | +pub fn toml_table(_gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema { |
| 77 | + schemars::schema::Schema::Object(schemars::schema::SchemaObject { |
| 78 | + instance_type: Some(schemars::schema::SingleOrVec::Single(Box::new( |
| 79 | + schemars::schema::InstanceType::Object, |
| 80 | + ))), |
| 81 | + ..Default::default() |
| 82 | + }) |
| 83 | +} |
| 84 | + |
| 85 | +pub fn map_of_toml_tables(_gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema { |
| 86 | + schemars::schema::Schema::Object(schemars::schema::SchemaObject { |
| 87 | + instance_type: Some(schemars::schema::SingleOrVec::Single(Box::new( |
| 88 | + schemars::schema::InstanceType::Object, |
| 89 | + ))), |
| 90 | + ..Default::default() |
| 91 | + }) |
| 92 | +} |
| 93 | + |
| 94 | +pub fn one_or_many<T: schemars::JsonSchema>( |
| 95 | + gen: &mut schemars::gen::SchemaGenerator, |
| 96 | +) -> schemars::schema::Schema { |
| 97 | + schemars::schema::Schema::Object(schemars::schema::SchemaObject { |
| 98 | + subschemas: Some(Box::new(schemars::schema::SubschemaValidation { |
| 99 | + one_of: Some(vec![ |
| 100 | + gen.subschema_for::<T>(), |
| 101 | + gen.subschema_for::<Vec<T>>(), |
| 102 | + ]), |
| 103 | + ..Default::default() |
| 104 | + })), |
| 105 | + ..Default::default() |
| 106 | + }) |
| 107 | +} |
0 commit comments