rustxform-core 0.2.0

Core data model (Survey, Question, Settings) for rustxform.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! Core data model shared across the rustxform pipeline.
//!
//! These types are the normalized, backend-agnostic representation of an
//! XLSForm after parsing and before XForm emission. They grow as later phases
//! add groups, repeats, translations and parameters.

/// Form-level settings, sourced from the XLSForm `settings` sheet.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Settings {
    /// Form identifier (`form_id`); defaults to `data` when absent.
    pub form_id: Option<String>,
    /// Human-readable form title.
    pub title: Option<String>,
    /// Form version string.
    pub version: Option<String>,
    /// Default language label, e.g. `French (fr)`.
    pub default_language: Option<String>,
    /// `instance_name` expression (populates `meta/instanceName`), if any.
    pub instance_name: Option<String>,
    /// `style` setting (becomes the body `class`), if any.
    pub style: Option<String>,
    /// `public_key` for encrypted submissions (`base64RsaPublicKey`), if any.
    pub public_key: Option<String>,
    /// `submission_url` (the `<submission action>`), if any.
    pub submission_url: Option<String>,
    /// `auto_send` setting (`orx:auto-send`), if any.
    pub auto_send: Option<String>,
    /// `auto_delete` setting (`orx:auto-delete`), if any.
    pub auto_delete: Option<String>,
}

/// Localizable text from a `label`/`hint` column family.
///
/// A plain `label` column fills [`default`](Self::default); language-qualified
/// `label::Lang` columns fill [`langs`](Self::langs). The two are mutually
/// exclusive in practice.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Localized {
    /// Text from an unqualified column, if any.
    pub default: Option<String>,
    /// Per-language texts `(language, text)`, in language order.
    pub langs: Vec<(String, String)>,
}

impl Localized {
    /// `true` when no text is present at all.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.default.is_none() && self.langs.is_empty()
    }

    /// `true` when language-qualified texts are present.
    #[must_use]
    pub fn is_multilingual(&self) -> bool {
        !self.langs.is_empty()
    }

    /// The unqualified single-language text, if any.
    #[must_use]
    pub fn single(&self) -> Option<&str> {
        self.default.as_deref()
    }

    /// The text for a given language, if any.
    #[must_use]
    pub fn for_lang(&self, lang: &str) -> Option<&str> {
        self.langs
            .iter()
            .find(|(l, _)| l == lang)
            .map(|(_, t)| t.as_str())
    }
}

/// The resolved semantics of a question's `type` column.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Kind {
    /// A built-in type with a fixed XForm mapping.
    Builtin(Builtin),
    /// A `select_one` / `select_multiple` / `rank` over a choice list or file.
    Select {
        /// Which selection control this is.
        select: SelectType,
        /// The instance id: a choice-list name, or an external file's stem.
        list: String,
        /// External source filename for `select_*_from_file`; `None` inline.
        file: Option<String>,
    },
    /// An `osm <tagset>` capture: an `osm/*` upload with tag children.
    Osm {
        /// Name of the tag list (from the `osm` sheet).
        tagset: String,
    },
    /// A type token not yet supported; emitted as a plain string bind.
    Unknown(String),
}

/// Which selection control a [`Kind::Select`] renders as.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SelectType {
    /// `select_one` → `<select1>`.
    One,
    /// `select_multiple` → `<select>`.
    Multiple,
    /// `rank` → `<odk:rank>`.
    Rank,
}

/// The XForm body control a built-in question renders as.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Control {
    /// `<input>`.
    Input,
    /// `<range>` (uses `start`/`end`/`step` from `parameters`).
    Range,
    /// `<trigger>` (e.g. `acknowledge`).
    Trigger,
    /// `<upload>` with the given `mediatype`.
    Upload {
        /// The `mediatype` attribute, e.g. `image/*`.
        mediatype: &'static str,
    },
}

/// A metadata preload binding (`jr:preload` + `jr:preloadParams`).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Preload {
    /// The `jr:preload` value, e.g. `timestamp` or `property`.
    pub preload: &'static str,
    /// The `jr:preloadParams` value, e.g. `start` or `deviceid`.
    pub params: &'static str,
}

/// A label media attachment: a form (`image`/`audio`/`video`) and its file(s),
/// which may be single- or multi-language.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Media {
    /// The media form: `image`, `audio` or `video`.
    pub form: String,
    /// Filenames, single (unqualified) or per-language.
    pub files: Localized,
}

/// The XForm mapping for a built-in question type.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Builtin {
    /// Body control, or `None` for data-only types (`calculate`, preloads).
    pub control: Option<Control>,
    /// XForm bind data type (e.g. `string`, `int`, `binary`).
    pub bind_type: &'static str,
    /// Whether the bind is read-only.
    pub readonly: bool,
    /// Metadata preload, if this type is a preload.
    pub preload: Option<Preload>,
    /// A model-level action this type emits (e.g. `odk:recordaudio`).
    pub action: Option<Action>,
}

/// A model-level action element bound to a question.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Action {
    /// Element name, e.g. `odk:recordaudio` or `odk:setgeopoint`.
    pub element: &'static str,
    /// The `event` attribute, e.g. `odk-instance-load`.
    pub event: &'static str,
}

/// A single survey question from the `survey` sheet.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Question {
    /// Resolved type semantics.
    pub kind: Kind,
    /// Node name; becomes the instance element name.
    pub name: String,
    /// Label text (single- or multi-language).
    pub label: Localized,
    /// Hint text (single- or multi-language).
    pub hint: Localized,
    /// `guidance_hint` text: a secondary hint rendered via an itext
    /// `<value form="guidance">` (single- or multi-language).
    pub guidance_hint: Localized,
    /// `appearance` column value, if any.
    pub appearance: Option<String>,
    /// Expression from the `calculation` column, if any.
    pub calculation: Option<String>,
    /// Expression from the `relevant` column, if any.
    pub relevant: Option<String>,
    /// Expression from the `constraint` column, if any.
    pub constraint: Option<String>,
    /// Raw `required` column value (e.g. `yes` or an expression), if any.
    pub required: Option<String>,
    /// Raw `read_only` column value, if any.
    pub readonly: Option<String>,
    /// `constraint_message` column (single- or multi-language).
    pub constraint_message: Localized,
    /// `required_message` column (single- or multi-language).
    pub required_message: Localized,
    /// Parsed `parameters` column, as `(key, value)` pairs.
    pub parameters: Vec<(String, String)>,
    /// Label media (images/audio/video), possibly per-language.
    pub media: Vec<Media>,
    /// Default value (`default` column), placed in the primary instance.
    pub default: Option<String>,
    /// `choice_filter` expression for cascading selects, if any.
    pub choice_filter: Option<String>,
    /// `save_to` column: the entity property this answer populates, if any.
    pub save_to: Option<String>,
    /// `trigger` column: names a node whose change drives this question's
    /// `calculation` via an `odk:setvalue` action instead of a calculate bind.
    pub trigger: Option<String>,
}

impl Question {
    /// The value of a parameter from the `parameters` column, if present.
    #[must_use]
    pub fn parameter(&self, key: &str) -> Option<&str> {
        self.parameters
            .iter()
            .find(|(k, _)| k == key)
            .map(|(_, v)| v.as_str())
    }
}

/// One option within a choice list.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Choice {
    /// Stored value (`name` column).
    pub name: String,
    /// Display label (single- or multi-language).
    pub label: Localized,
    /// Extra columns `(name, value)` for cascading filters, in header order.
    pub extra: Vec<(String, String)>,
}

/// A named list of choices from the `choices` sheet.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ChoiceList {
    /// List name referenced by `select_*` questions.
    pub name: String,
    /// Options in sheet order.
    pub items: Vec<Choice>,
}

/// A container of child nodes: a group (`<group>`) or a repeat (`<repeat>`).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Container {
    /// Node name; becomes the enclosing instance element.
    pub name: String,
    /// Default-language label, if any.
    pub label: Option<String>,
    /// `appearance` column value (e.g. `field-list`), if any.
    pub appearance: Option<String>,
    /// `repeat_count` expression (repeats only), if any.
    pub count: Option<String>,
    /// Ordered child nodes.
    pub children: Vec<Node>,
}

/// A node in the survey tree: a question, a group, or a repeat.
#[expect(
    clippy::large_enum_variant,
    reason = "Question is by far the most common node; boxing it would add an allocation on the common path"
)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Node {
    /// A leaf question.
    Question(Question),
    /// A non-repeating group of nodes.
    Group(Container),
    /// A repeating group of nodes.
    Repeat(Container),
}

/// An ODK Entities declaration (the `entities` sheet): the survey creates an
/// entity in a dataset when submitted.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Entity {
    /// Target dataset name.
    pub dataset: String,
    /// Label expression for the entity, if any.
    pub label: Option<String>,
    /// `entity_id` expression: present ⇒ update mode, absent ⇒ create.
    pub entity_id: Option<String>,
    /// `create_if` condition expression, if any.
    pub create_if: Option<String>,
    /// `update_if` condition expression, if any.
    pub update_if: Option<String>,
}

/// A parsed XLSForm survey: settings, a node tree and choice lists.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Survey {
    /// Form-level settings.
    pub settings: Settings,
    /// Ordered top-level nodes.
    pub children: Vec<Node>,
    /// Choice lists from the `choices` sheet.
    pub choices: Vec<ChoiceList>,
    /// Declared languages, in order; empty for a single-language form.
    pub languages: Vec<String>,
    /// Entities declaration from the `entities` sheet, if any.
    pub entity: Option<Entity>,
    /// Whether an `audit` metadata question is present.
    pub audit: bool,
    /// `xml-external` instance ids (each loads `jr://file/<id>.xml`).
    pub external_instances: Vec<String>,
    /// OSM tag lists from the `osm` sheet.
    pub osm_tags: Vec<ChoiceList>,
}

impl Survey {
    /// Return the choice list with the given name, if present.
    #[must_use]
    pub fn choice_list(&self, name: &str) -> Option<&ChoiceList> {
        self.choices.iter().find(|c| c.name == name)
    }
}

/// Resolve a single XLSForm type token to its built-in XForm mapping.
///
/// Returns `None` for `select_*` (handled separately) and unknown tokens.
/// The table grows as later phases add question kinds.
#[must_use]
pub fn resolve_builtin(kind: &str) -> Option<Builtin> {
    let b = |control, bind_type, readonly, preload| Builtin {
        control,
        bind_type,
        readonly,
        preload,
        action: None,
    };
    let input = Some(Control::Input);
    let preload = |preload, params| Some(Preload { preload, params });

    Some(match kind {
        "text" => b(input, "string", false, None),
        "integer" => b(input, "int", false, None),
        "decimal" => b(input, "decimal", false, None),
        "range" => b(Some(Control::Range), "int", false, None),
        "date" => b(input, "date", false, None),
        "time" => b(input, "time", false, None),
        "dateTime" => b(input, "dateTime", false, None),
        "geopoint" => b(input, "geopoint", false, None),
        "geotrace" => b(input, "geotrace", false, None),
        "geoshape" => b(input, "geoshape", false, None),
        "barcode" => b(input, "barcode", false, None),
        "note" => b(input, "string", true, None),
        "acknowledge" => b(Some(Control::Trigger), "string", false, None),
        "image" => b(
            Some(Control::Upload {
                mediatype: "image/*",
            }),
            "binary",
            false,
            None,
        ),
        "audio" => b(
            Some(Control::Upload {
                mediatype: "audio/*",
            }),
            "binary",
            false,
            None,
        ),
        "video" => b(
            Some(Control::Upload {
                mediatype: "video/*",
            }),
            "binary",
            false,
            None,
        ),
        "file" => b(
            Some(Control::Upload {
                mediatype: "application/*",
            }),
            "binary",
            false,
            None,
        ),
        "calculate" => b(None, "string", false, None),
        "hidden" => b(None, "string", false, None),
        "start" => b(None, "dateTime", false, preload("timestamp", "start")),
        "end" => b(None, "dateTime", false, preload("timestamp", "end")),
        "today" => b(None, "date", false, preload("date", "today")),
        "deviceid" => b(None, "string", false, preload("property", "deviceid")),
        "username" => b(None, "string", false, preload("property", "username")),
        "phonenumber" => b(None, "string", false, preload("property", "phonenumber")),
        "email" => b(None, "string", false, preload("property", "email")),
        "simserial" => b(None, "string", false, preload("property", "simserial")),
        "subscriberid" => b(None, "string", false, preload("property", "subscriberid")),
        "background-audio" => Builtin {
            control: None,
            bind_type: "binary",
            readonly: false,
            preload: None,
            action: Some(Action {
                element: "odk:recordaudio",
                event: "odk-instance-load",
            }),
        },
        "start-geopoint" => Builtin {
            control: None,
            bind_type: "geopoint",
            readonly: false,
            preload: None,
            action: Some(Action {
                element: "odk:setgeopoint",
                event: "odk-instance-first-load",
            }),
        },
        // Passive geopoint capture triggered by another field's change; the
        // `odk:setgeopoint` action is emitted on that trigger node, not here.
        "background-geopoint" => b(None, "geopoint", false, None),
        _ => return None,
    })
}