Skip to main content

dre_core/config/
lookup.rs

1//! The shape of a lookup's config, `lookups/<name>.yml`.
2
3use schemars::JsonSchema;
4use serde::Deserialize;
5
6use super::de::{Located, Loose, Map, UnknownKeys};
7
8/// The optional config of a lookup, `lookups/<name>.yml` next to a csv, xlsx, xls, json or jsonl file. (A `.yml` lookup that holds the rows themselves can also set `rows`.)
9#[derive(Deserialize, JsonSchema)]
10#[schemars(title = "DRE lookup config", deny_unknown_fields)]
11pub struct LookupFile {
12    /// Types for columns; without it every value is text.
13    pub columns: Option<Located<Loose<Map<Loose<ColumnType>>>>>,
14    /// For xlsx and xls lookups: the sheet to read. Default: the first.
15    pub sheet: Option<Located<Loose<String>>>,
16    /// How the lookup reaches the database: inlined into the SQL, loaded into a temp table, or chosen by size (`auto`).
17    #[schemars(extend("default" = "auto"))]
18    pub load: Option<Located<Loose<LoadName>>>,
19    /// For a `.yml` lookup that holds its own data: the rows.
20    #[serde(default)]
21    #[schemars(schema_with = "rows")]
22    pub rows: Option<Located<serde_json::Value>>,
23    #[serde(rename = "$unknown", default)]
24    #[schemars(skip)]
25    pub unknown: UnknownKeys,
26}
27
28/// One of `string`, `integer`, `number`, `boolean`, `date`.
29#[derive(Deserialize, JsonSchema, Clone, Copy)]
30#[serde(rename_all = "lowercase")]
31#[schemars(inline)]
32pub enum ColumnType {
33    String,
34    Integer,
35    Number,
36    Boolean,
37    Date,
38}
39
40#[derive(Deserialize, JsonSchema, Clone, Copy)]
41#[serde(rename_all = "snake_case")]
42#[schemars(inline)]
43pub enum LoadName {
44    Auto,
45    Inline,
46    TempTable,
47}
48
49fn rows(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
50    schemars::json_schema!({"type": "array", "items": {"type": "object"}})
51}