Skip to main content

ferrobridge_operator_api/
operator.rs

1// SPDX-FileCopyrightText: Cadasto B.V.
2// SPDX-License-Identifier: BUSL-1.1
3
4//! The operator documents: the running load `GET /operator/state` answers,
5//! the `GET /operator/mappings` listing, and the report of a check, a save
6//! and a reload.
7//!
8//! A path in a document is a wire path (`fhirconnect/...`, `omocl/...`) or
9//! a file name, never a host directory, and every diagnostic is the mapping
10//! foundation's own [`Diagnostic`] in its serde form, its `file` the wire
11//! path.
12
13use openehr_mapping_core::diagnostic::Diagnostic;
14use serde::{Deserialize, Serialize};
15
16/// How much one load read and compiled.
17#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
18#[serde(deny_unknown_fields)]
19pub struct Counts {
20    /// The FHIRconnect files read.
21    pub fhirconnect_files: usize,
22    /// The FHIRconnect context files of a set that loaded.
23    pub contexts: usize,
24    /// The contexts that compiled into a program.
25    pub programs: usize,
26    /// The templates the contexts compiled against.
27    pub templates: usize,
28    /// The OMOCL files read.
29    pub omocl_files: usize,
30}
31
32/// What `GET /operator/state` answers with: the running load.
33#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
34#[serde(deny_unknown_fields)]
35pub struct EngineState {
36    /// Each context's program.
37    pub contexts: Vec<Program>,
38    /// The templates the contexts compiled against.
39    pub templates: Vec<Template>,
40    /// The OMOCL files.
41    pub omocl: Omocl,
42    /// How much the load read and compiled.
43    pub counts: Counts,
44    /// Every diagnostic the load raised.
45    pub diagnostics: Vec<Diagnostic>,
46}
47
48/// One context's program.
49#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
50#[serde(deny_unknown_fields)]
51pub struct Program {
52    /// The context's mapping name.
53    pub context: String,
54    /// The file it was read from, when the set names one.
55    pub file: Option<String>,
56    /// The template the program compiled against.
57    pub template: String,
58    /// The profile's canonical URL.
59    pub profile: String,
60    /// The profile's version.
61    pub profile_version: String,
62    /// The resource type the program writes.
63    pub resource_type: String,
64    /// The models the program reads with.
65    pub models: Vec<Model>,
66    /// The search declaration answering searches of the context, when one
67    /// is declared.
68    pub search: Option<String>,
69}
70
71/// One model of a program.
72#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
73#[serde(deny_unknown_fields)]
74pub struct Model {
75    /// The model's mapping name.
76    pub name: String,
77    /// The file it was read from, when the set names one.
78    pub file: Option<String>,
79    /// The extensions applied to it, in declaration order.
80    pub extensions: Vec<Extension>,
81}
82
83/// One extension of a model.
84#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
85#[serde(deny_unknown_fields)]
86pub struct Extension {
87    /// The extension's mapping name.
88    pub name: String,
89    /// The file it was read from, when the set names one.
90    pub file: Option<String>,
91}
92
93/// One template, by where it came from.
94#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
95#[serde(tag = "source", rename_all = "lowercase", deny_unknown_fields)]
96pub enum Template {
97    /// Read from the configured template directory.
98    Directory {
99        /// The template id.
100        id: String,
101        /// The file it was read from.
102        file: String,
103    },
104    /// Fetched from the CDR.
105    Cdr {
106        /// The template id.
107        id: String,
108    },
109}
110
111/// The OMOCL files of the load.
112#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
113#[serde(deny_unknown_fields)]
114pub struct Omocl {
115    /// Their wire paths.
116    pub files: Vec<String>,
117    /// Whether none of them raised an error.
118    pub valid: bool,
119}
120
121/// The language of the files under one mapping root.
122#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
123#[serde(rename_all = "lowercase")]
124pub enum Language {
125    /// FHIRconnect model, extension, context and search files.
126    Fhirconnect,
127    /// OMOCL files.
128    Omocl,
129}
130
131/// Who owns a mapping file.
132#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
133#[serde(rename_all = "lowercase")]
134pub enum Owner {
135    /// The deployment provisioned it; the operator API never writes it.
136    Provisioned,
137    /// The console saved it under `[operator] mapping_dir`.
138    Console,
139}
140
141/// One mapping file as `GET /operator/mappings` lists it.
142#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
143#[serde(deny_unknown_fields)]
144pub struct Listed {
145    /// The wire path.
146    pub path: String,
147    /// The language of its root.
148    pub language: Language,
149    /// The mapping type its header declares, `search` for a search
150    /// declaration and `mapping` for an OMOCL file; absent for a file the
151    /// running load did not read.
152    pub kind: Option<String>,
153    /// The lowercase hexadecimal SHA-256 of its bytes.
154    pub sha256: String,
155    /// Who owns it.
156    pub owner: Owner,
157    /// Whether the operator API refuses to write it.
158    pub read_only: bool,
159    /// The diagnostics the running load raised about it.
160    pub diagnostics: Vec<Diagnostic>,
161}
162
163/// What a check, a save and a reload answer with: whether the set compiled,
164/// how much it read, and every diagnostic.
165#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
166#[serde(deny_unknown_fields)]
167pub struct LoadReport {
168    /// Whether the set compiled with no error.
169    pub clean: bool,
170    /// How much the load read and compiled.
171    pub counts: Counts,
172    /// Every diagnostic, each file by its wire path.
173    pub diagnostics: Vec<Diagnostic>,
174}
175
176#[cfg(test)]
177mod tests {
178    use super::{EngineState, LoadReport, Template};
179
180    #[test]
181    fn a_template_is_tagged_by_its_source() {
182        let directory = Template::Directory {
183            id: String::from("synthetic.v1"),
184            file: String::from("t.opt"),
185        };
186        assert_eq!(
187            serde_json::json!({"id": "synthetic.v1", "source": "directory", "file": "t.opt"}),
188            serde_json::to_value(&directory).expect("serializes")
189        );
190        let cdr: Template =
191            serde_json::from_value(serde_json::json!({"id": "synthetic.v2", "source": "cdr"}))
192                .expect("reads");
193        assert_eq!(
194            Template::Cdr {
195                id: String::from("synthetic.v2")
196            },
197            cdr
198        );
199    }
200
201    #[test]
202    fn a_document_reads_back_and_an_unknown_field_is_refused() {
203        let text = serde_json::json!({
204            "contexts": [], "templates": [], "omocl": {"files": [], "valid": true},
205            "counts": {"fhirconnect_files": 0, "contexts": 0, "programs": 0, "templates": 0, "omocl_files": 0},
206            "diagnostics": [{"file": "fhirconnect/a.yml", "line": 1, "column": 2, "severity": "error",
207                "code": "missing-key", "message": "synthetic", "mapping": null, "model_path": []}]
208        });
209        let state: EngineState = serde_json::from_value(text.clone()).expect("reads");
210        assert_eq!(text, serde_json::to_value(&state).expect("serializes"));
211        let mut extra = text;
212        extra["unknown"] = serde_json::json!(1);
213        assert!(serde_json::from_value::<EngineState>(extra).is_err());
214        let report = serde_json::json!({"clean": true, "counts": {"fhirconnect_files": 1, "contexts": 1, "programs": 1, "templates": 1, "omocl_files": 0}, "diagnostics": []});
215        assert!(serde_json::from_value::<LoadReport>(report).is_ok());
216    }
217}