#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Settings {
pub form_id: Option<String>,
pub title: Option<String>,
pub version: Option<String>,
pub default_language: Option<String>,
pub instance_name: Option<String>,
pub style: Option<String>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Localized {
pub default: Option<String>,
pub langs: Vec<(String, String)>,
}
impl Localized {
#[must_use]
pub fn is_empty(&self) -> bool {
self.default.is_none() && self.langs.is_empty()
}
#[must_use]
pub fn is_multilingual(&self) -> bool {
!self.langs.is_empty()
}
#[must_use]
pub fn single(&self) -> Option<&str> {
self.default.as_deref()
}
#[must_use]
pub fn for_lang(&self, lang: &str) -> Option<&str> {
self.langs
.iter()
.find(|(l, _)| l == lang)
.map(|(_, t)| t.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Kind {
Builtin(Builtin),
Select {
select: SelectType,
list: String,
file: Option<String>,
},
Osm {
tagset: String,
},
Unknown(String),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SelectType {
One,
Multiple,
Rank,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Control {
Input,
Range,
Trigger,
Upload {
mediatype: &'static str,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Preload {
pub preload: &'static str,
pub params: &'static str,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Media {
pub form: String,
pub files: Localized,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Builtin {
pub control: Option<Control>,
pub bind_type: &'static str,
pub readonly: bool,
pub preload: Option<Preload>,
pub action: Option<Action>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Action {
pub element: &'static str,
pub event: &'static str,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Question {
pub kind: Kind,
pub name: String,
pub label: Localized,
pub hint: Localized,
pub appearance: Option<String>,
pub calculation: Option<String>,
pub relevant: Option<String>,
pub constraint: Option<String>,
pub required: Option<String>,
pub readonly: Option<String>,
pub constraint_message: Localized,
pub required_message: Localized,
pub parameters: Vec<(String, String)>,
pub media: Vec<Media>,
pub default: Option<String>,
pub choice_filter: Option<String>,
pub save_to: Option<String>,
}
impl Question {
#[must_use]
pub fn parameter(&self, key: &str) -> Option<&str> {
self.parameters
.iter()
.find(|(k, _)| k == key)
.map(|(_, v)| v.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Choice {
pub name: String,
pub label: Localized,
pub extra: Vec<(String, String)>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ChoiceList {
pub name: String,
pub items: Vec<Choice>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Container {
pub name: String,
pub label: Option<String>,
pub appearance: Option<String>,
pub count: Option<String>,
pub children: Vec<Node>,
}
#[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 {
Question(Question),
Group(Container),
Repeat(Container),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Entity {
pub dataset: String,
pub label: Option<String>,
pub entity_id: Option<String>,
pub create_if: Option<String>,
pub update_if: Option<String>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Survey {
pub settings: Settings,
pub children: Vec<Node>,
pub choices: Vec<ChoiceList>,
pub languages: Vec<String>,
pub entity: Option<Entity>,
pub audit: bool,
pub external_instances: Vec<String>,
pub osm_tags: Vec<ChoiceList>,
}
impl Survey {
#[must_use]
pub fn choice_list(&self, name: &str) -> Option<&ChoiceList> {
self.choices.iter().find(|c| c.name == name)
}
}
#[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),
"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",
}),
},
_ => return None,
})
}