1use schemars::JsonSchema;
8use serde::Deserialize;
9use serde::de::IgnoredAny;
10
11use super::de::{Located, Loose, Map, Maybe, OneOf, UnknownKeys};
12
13type JsonMap = serde_json::Map<String, serde_json::Value>;
14
15pub type QueryItem = Located<Loose<OneOf<String, QueryEntry>>>;
17
18#[derive(Default, Deserialize, JsonSchema)]
20#[schemars(title = "DRE report", deny_unknown_fields)]
21pub struct ReportFile {
22 #[schemars(length(min = 1))]
24 pub name: Option<Located<Loose<String>>>,
25 pub description: Option<Located<Loose<String>>>,
27 pub meta: Option<Located<Loose<JsonMap>>>,
29 pub tags: Option<Located<Loose<Vec<String>>>>,
31 #[serde(default)]
33 #[schemars(schema_with = "queries")]
34 pub queries: Option<Located<Loose<Vec<QueryItem>>>>,
35 #[serde(default)]
37 #[schemars(schema_with = "output_or_list")]
38 pub output: Option<IgnoredAny>,
39 pub profile: Option<Located<Loose<String>>>,
41 pub sets: Option<Located<Loose<Vec<SetItem>>>>,
43 pub default_set: Option<Located<Loose<String>>>,
45 pub vars: Option<Located<Loose<JsonMap>>>,
47 pub var_schema: Option<Located<Loose<crate::variables::Definitions>>>,
50 pub timezone: Option<Located<Loose<String>>>,
52 pub locale: Option<Located<Loose<String>>>,
54 #[serde(default)]
56 #[schemars(schema_with = "super::dependencies::plugins")]
57 pub plugins: Option<IgnoredAny>,
58 #[serde(default)]
60 #[schemars(schema_with = "super::project::sources_ref")]
61 pub sources: Option<IgnoredAny>,
62 #[schemars(skip)]
64 pub schedule: Option<Located<IgnoredAny>>,
65 #[serde(rename = "$unknown", default)]
66 #[schemars(skip)]
67 pub unknown: UnknownKeys,
68}
69
70#[derive(Deserialize, JsonSchema)]
72#[schemars(rename = "queryEntry", deny_unknown_fields)]
73pub struct QueryEntry {
74 #[schemars(required)]
76 pub query: Option<Loose<String>>,
77 pub profile: Option<Loose<String>>,
79 #[schemars(extend("default" = true))]
81 pub tab: Option<Loose<bool>>,
82 pub tab_name: Option<Loose<String>>,
84 #[schemars(regex(pattern = "^[A-Za-z]{1,3}[0-9]+$"))]
86 pub anchor: Option<Loose<String>>,
87 pub header: Option<Loose<bool>>,
89 #[serde(default)]
91 #[schemars(schema_with = "columns")]
92 pub columns: Option<serde_json::Value>,
93 pub on_empty: Option<Loose<crate::no_data::OnEmpty>>,
95 pub autofit: Option<Loose<bool>>,
97 #[serde(default)]
99 #[schemars(schema_with = "sheet_style")]
100 pub style: Option<serde_json::Value>,
101 #[serde(rename = "$unknown", default)]
102 #[schemars(skip)]
103 pub unknown: UnknownKeys,
104}
105
106fn sheet_style(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
107 schemars::json_schema!({
108 "type": "object",
109 "description": "How an xlsx tab looks. Cell keys apply to every data cell; `header` and `totals` take cell keys for those rows.",
110 "properties": {
111 "header": {"type": "object", "description": "Cell keys for the header row (default: bold)."},
112 "totals": {"type": "object", "description": "Cell keys for the totals row (default: bold, a thin top border)."},
113 "banded_rows": {"oneOf": [{"type": "string", "pattern": "^#?[0-9A-Fa-f]{6}$"}, {"const": false}], "description": "The fill of every other data row, `\"#RRGGBB\"`, or `false`."},
114 "borders": {"enum": ["none", "thin", "medium"], "description": "A border around every cell of the table."},
115 "bold": {"type": "boolean", "description": "Bold text in every data cell."},
116 "italic": {"type": "boolean", "description": "Italic text in every data cell."},
117 "underline": {"type": "boolean", "description": "Underlined text in every data cell."},
118 "font": {"type": "object", "description": "The font: `{name: Calibri, size: 11}`.", "properties": {"name": {"type": "string", "description": "The font's name."}, "size": {"type": "number", "description": "The size in points."}}, "additionalProperties": false},
119 "font_color": {"type": "string", "pattern": "^#?[0-9A-Fa-f]{6}$", "description": "The text colour, `\"#RRGGBB\"`."},
120 "fill": {"type": "string", "pattern": "^#?[0-9A-Fa-f]{6}$", "description": "The background colour, `\"#RRGGBB\"`."},
121 "align": {"enum": ["left", "center", "right"], "description": "Horizontal alignment."},
122 "border": {"enum": ["none", "thin", "medium"], "description": "A border around each data cell."}
123 },
124 "additionalProperties": false
125 })
126}
127
128fn columns(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
129 schemars::json_schema!({
130 "type": "object",
131 "additionalProperties": {
132 "type": "object",
133 "description": "Settings for one column of an xlsx tab.",
134 "properties": {
135 "format": {"type": "string", "description": "The Excel number format of the column, e.g. `#,##0.00` or `dd/mm/yyyy`. See the xlsx column formats in the plugins reference."},
136 "formula": {"type": "string", "description": "An Excel formula for each row of this column; `{name}` stands for that column's cell on the same row, e.g. `=ROUND({qty}*{unit_price},2)`. The SQL selects a placeholder column where the formula goes."},
137 "total": {"type": "string", "description": "Puts a total under the column: one of `sum`, `count`, `average`, `min`, `max`, or a formula such as `=SUM({net:*})`."},
138 "style": {"type": "object", "description": "How the column's data cells look: `bold`, `italic`, `underline`, `font` (`{name, size}`), `font_color`, `fill` (`\"#RRGGBB\"`), `align` (`left`, `center`, `right`), `border` (`none`, `thin`, `medium`). Over the tab's and output's `style`."},
139 "width": {"oneOf": [{"const": "auto"}, {"type": "number", "exclusiveMinimum": 0, "maximum": 255}], "description": "The column's width: `auto` (sized from its content, at most 60 characters) or a number of characters. Over the tab's and output's `autofit`."}
140 },
141 "additionalProperties": false
142 },
143 "propertyNames": {"minLength": 1}
144 })
145}
146
147pub type SetItem = Located<Loose<OneOf<String, SetEntry>>>;
149
150#[derive(Deserialize, JsonSchema)]
152#[schemars(rename = "setEntry", deny_unknown_fields)]
153pub struct SetEntry {
154 #[schemars(required)]
156 pub name: Option<Loose<String>>,
157 pub profile: Option<Located<Loose<String>>>,
159 #[serde(default)]
161 #[schemars(schema_with = "any_map")]
162 pub vars: Option<Loose<JsonMap>>,
163 pub exclude: Option<Located<Loose<Vec<String>>>>,
165 #[serde(default)]
167 #[schemars(schema_with = "set_queries")]
168 pub queries: Option<Located<Loose<Vec<SetQueryItem>>>>,
169 #[serde(default)]
171 #[schemars(schema_with = "tab_names")]
172 pub tab_names: Option<Loose<JsonMap>>,
173 #[serde(default)]
175 #[schemars(schema_with = "output_or_list")]
176 pub output: Option<serde_json::Value>,
177 pub locale: Option<Loose<String>>,
179 #[schemars(skip)]
181 pub schedule: Option<IgnoredAny>,
182 #[serde(rename = "$unknown", default)]
183 #[schemars(skip)]
184 pub unknown: UnknownKeys,
185}
186
187pub type SetQueryItem = Loose<OneOf<String, SetQuery>>;
189
190#[derive(Deserialize)]
192pub struct SetQuery {
193 pub query: Option<Loose<String>>,
194 pub tab: Option<Loose<bool>>,
195 pub tab_name: Option<Loose<String>>,
196 pub on_empty: Option<Loose<crate::no_data::OnEmpty>>,
197}
198
199fn any_map(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
200 schemars::json_schema!({"type": "object", "additionalProperties": true})
201}
202
203fn tab_names(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
204 schemars::json_schema!({"type": "object", "additionalProperties": {"type": "string"}})
205}
206
207fn output_or_list(g: &mut schemars::SchemaGenerator) -> schemars::Schema {
208 let output = g.subschema_for::<Output>();
209 schemars::json_schema!({
210 "oneOf": [output, {"type": "array", "minItems": 1, "items": output}]
211 })
212}
213
214fn set_queries(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
215 schemars::json_schema!({
216 "type": "array",
217 "items": {
218 "description": "A query of this Set, by name, or with its own settings.",
219 "oneOf": [
220 {"type": "string"},
221 {
222 "type": "object",
223 "description": "A query with settings.",
224 "properties": {
225 "query": {"type": "string", "description": "The query's name."},
226 "tab": {"type": "boolean", "description": "Whether the query makes a tab."},
227 "tab_name": {"type": "string", "description": "The tab name."},
228 "on_empty": {"enum": ["continue", "skip", "fail"], "description": "When this query returns no rows: keep its empty tab, leave it out, or fail the run."}
229 },
230 "required": ["query"],
231 "additionalProperties": true
232 }
233 ]
234 }
235 })
236}
237
238#[derive(Deserialize, JsonSchema)]
242#[schemars(rename = "output", extend("additionalProperties" = true))]
243pub struct Output {
244 #[schemars(regex(pattern = "^[A-Za-z_][A-Za-z0-9_]*$"))]
246 pub name: Option<Loose<String>>,
247 pub format: Option<Loose<String>>,
249 #[schemars(length(min = 1))]
251 pub queries: Option<Loose<Vec<String>>>,
252 #[serde(default)]
254 #[schemars(schema_with = "string_or_bool")]
255 pub when: Option<Loose<OneOf<String, bool>>>,
256 #[serde(default)]
258 #[schemars(schema_with = "destination_or_list")]
259 pub destination: Option<Loose<OneOf<Destination, Vec<Loose<Destination>>>>>,
260 pub template: Option<Loose<Template>>,
261 #[serde(default)]
263 #[schemars(schema_with = "for_each_or_false")]
264 pub for_each: Option<Loose<OneOf<ForEach, bool>>>,
265 pub file_name: Option<Loose<String>>,
267 pub on_empty: Option<Loose<crate::no_data::OnEmpty>>,
269 #[serde(default)]
271 #[schemars(schema_with = "extension")]
272 pub extension: Maybe<Loose<Option<OneOf<String, bool>>>>,
273 #[serde(rename = "$unknown", default)]
275 #[schemars(skip)]
276 pub options: Map<serde_json::Value>,
277}
278
279#[derive(Deserialize, JsonSchema)]
281#[schemars(rename = "destination", extend("additionalProperties" = true))]
282pub struct Destination {
283 #[schemars(required)]
285 pub profile: Option<Loose<String>>,
286 pub path: Option<Loose<String>>,
288 #[serde(default)]
290 #[schemars(schema_with = "attach")]
291 pub attach: Option<Loose<OneOf<String, Vec<String>>>>,
292 pub hold: Option<Loose<bool>>,
294 #[schemars(regex(pattern = "^\\s*[0-9_]+\\s*/\\s*[A-Za-z]+\\s*$"))]
296 pub rate: Option<Loose<String>>,
297 #[schemars(range(min = 1))]
299 pub burst: Option<Loose<u64>>,
300 #[serde(rename = "$unknown", default)]
302 #[schemars(skip)]
303 pub options: Map<serde_json::Value>,
304}
305
306#[derive(Deserialize, JsonSchema)]
308#[schemars(rename = "for_each", deny_unknown_fields)]
309pub struct ForEach {
310 #[schemars(required)]
312 pub query: Option<Loose<String>>,
313 #[schemars(required)]
315 #[serde(default)]
316 #[schemars(schema_with = "string_or_strings")]
317 pub key: Option<Loose<OneOf<String, Vec<String>>>>,
318 #[schemars(range(min = 1))]
320 pub concurrency: Option<Loose<u64>>,
321 pub on_row_error: Option<Loose<crate::project::RowErrors>>,
323 pub on_orphan_rows: Option<Loose<crate::project::OrphanRows>>,
325 #[schemars(range(min = 1))]
327 pub max_population_rows: Option<Loose<u64>>,
328 pub keep_results: Option<Loose<bool>>,
330 pub mask_row_values: Option<Loose<bool>>,
332}
333
334fn string_or_strings(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
335 schemars::json_schema!({
336 "oneOf": [
337 {"type": "string"},
338 {"type": "array", "items": {"type": "string"}, "minItems": 1}
339 ]
340 })
341}
342
343fn for_each_or_false(g: &mut schemars::SchemaGenerator) -> schemars::Schema {
344 let f = g.subschema_for::<ForEach>();
345 schemars::json_schema!({"oneOf": [f, {"const": false}]})
346}
347
348#[derive(Deserialize, JsonSchema)]
350#[schemars(rename = "template", deny_unknown_fields)]
351pub struct Template {
352 #[schemars(required)]
354 pub file: Option<Loose<String>>,
355 pub bindings: Option<Loose<Vec<Loose<TemplateBinding>>>>,
357}
358
359#[derive(Deserialize, JsonSchema)]
361#[schemars(deny_unknown_fields, inline)]
362pub struct TemplateBinding {
363 #[schemars(required)]
365 pub sheet: Option<Loose<String>>,
366 pub query: Option<Loose<String>>,
368 #[schemars(range(min = 1))]
370 pub result_index: Option<Loose<u64>>,
371 #[schemars(regex(pattern = "^[A-Za-z]{1,3}[0-9]+$"))]
373 pub anchor: Option<Loose<String>>,
374 pub header: Option<Loose<bool>>,
376 pub columns: Option<Loose<Vec<String>>>,
378 #[schemars(regex(pattern = "^[A-Za-z]{1,3}[0-9]+$"))]
380 pub cell: Option<Loose<String>>,
381 pub value: Option<Loose<String>>,
383 pub column: Option<Loose<String>>,
385 #[serde(rename = "$unknown", default)]
386 #[schemars(skip)]
387 pub unknown: UnknownKeys,
388}
389
390fn string_or_bool(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
391 schemars::json_schema!({"type": ["string", "boolean"]})
392}
393
394fn extension(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
395 schemars::json_schema!({"type": ["string", "boolean", "null"], "x-dre-null": true})
397}
398
399fn destination_or_list(g: &mut schemars::SchemaGenerator) -> schemars::Schema {
400 let destination = g.subschema_for::<Destination>();
401 schemars::json_schema!({
402 "oneOf": [destination, {"type": "array", "minItems": 1, "items": destination}]
403 })
404}
405
406fn attach(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
407 schemars::json_schema!({
408 "oneOf": [
409 {"type": "string"},
410 {"type": "array", "items": {"type": "string"}, "minItems": 1}
411 ]
412 })
413}
414
415fn queries(g: &mut schemars::SchemaGenerator) -> schemars::Schema {
416 let entry = g.subschema_for::<QueryEntry>();
417 schemars::json_schema!({
418 "type": "array",
419 "items": {
420 "oneOf": [
421 {"type": "string", "description": "The name of a `.sql` file under `reports/`, without extension."},
422 entry
423 ]
424 }
425 })
426}