Skip to main content

presolve_compiler/
form_projection.rs

1//! V2 Form and Field inspection over canonical declarations.
2use crate::{FormEntity, FormFieldEntity, FormId};
3use serde::Serialize;
4pub const FORM_PROJECTION_SCHEMA_VERSION: u32 = 1;
5#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
6pub struct FormProjectionRecordV1 {
7    pub form: String,
8    pub owner: String,
9    pub fields: Vec<String>,
10}
11#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
12pub struct FormProjectionV1 {
13    pub schema_version: u32,
14    pub forms: Vec<FormProjectionRecordV1>,
15}
16#[must_use]
17pub fn build_form_projection_v1(
18    forms: &std::collections::BTreeMap<FormId, FormEntity>,
19    fields: &std::collections::BTreeMap<crate::FieldId, FormFieldEntity>,
20) -> FormProjectionV1 {
21    let mut output = forms
22        .values()
23        .map(|f| {
24            let mut names = fields
25                .values()
26                .filter(|x| x.owner_form == f.id)
27                .map(|x| x.id.as_str().into())
28                .collect::<Vec<String>>();
29            names.sort();
30            FormProjectionRecordV1 {
31                form: f.id.as_str().into(),
32                owner: f
33                    .owner
34                    .entity_id()
35                    .map_or_else(String::new, ToString::to_string),
36                fields: names,
37            }
38        })
39        .collect::<Vec<_>>();
40    output.sort_by(|a, b| a.form.cmp(&b.form));
41    FormProjectionV1 {
42        schema_version: FORM_PROJECTION_SCHEMA_VERSION,
43        forms: output,
44    }
45}