helios-sof 0.2.1

This crate provides a complete implementation of the SQL-on-FHIR specification for Rust, enabling the transformation of FHIR resources into tabular data using declarative ViewDefinitions. It supports all major FHIR versions (R4, R4B, R5, R6) through a version-agnostic abstraction layer.
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
//! Parse a SQLQuery Library (FHIR Library profile) into the parts the engine
//! needs: the SQL string, parameter declarations, and depends-on ViewDefinitions.
//!
//! See <https://build.fhir.org/ig/FHIR/sql-on-fhir-v2/StructureDefinition-SQLQuery.html>

use base64::Engine;
use serde_json::Value;

use super::SqlQueryError;

/// `Library.type.coding.system` value the SQLQuery profile fixes.
pub const LIBRARY_TYPE_SYSTEM: &str = "https://sql-on-fhir.org/ig/CodeSystem/LibraryTypesCodes";
/// `Library.type.coding.code` value the SQLQuery profile fixes.
pub const LIBRARY_TYPE_CODE: &str = "sql-query";

/// SQL dialect the engine speaks. Used to pick the most specific
/// `application/sql;dialect=…` content attachment.
pub const ENGINE_DIALECT: &str = "sqlite";

/// One row's worth of metadata from `Library.parameter`. `use=in` only.
#[derive(Debug, Clone)]
pub struct LibraryParameter {
    pub name: String,
    /// FHIR `code` element — `string`, `integer`, `integer64`, `boolean`,
    /// `decimal`, `date`, `dateTime`, `instant`, `time`, etc. Required by the
    /// SQLQuery profile (1..1) — missing here is a malformed Library.
    pub type_code: String,
    /// Was the parameter declared with a `default[X]` value? If so we treat
    /// it as optional. The SQLQuery profile does not document defaults; this
    /// is a forward-compatible read for any `default*` field on the entry.
    pub has_default: bool,
    /// Default value as raw JSON, if any.
    pub default_value: Option<Value>,
}

/// A `depends-on` entry in the Library's `relatedArtifact`. The SQLQuery
/// profile requires `relatedArtifact.resource` to be a canonical URL — inline
/// ViewDefinition resources are **not** part of the profile and are rejected.
#[derive(Debug, Clone)]
pub struct DependsOnView {
    /// Table alias the SQL references. Constrained to `^[A-Za-z][A-Za-z0-9_]*$`
    /// by the profile (`sql-name` invariant).
    pub label: String,
    /// Canonical URL pointing to a ViewDefinition the server resolves.
    pub url: String,
}

/// A parsed SQLQuery Library.
#[derive(Debug, Clone)]
pub struct SqlQueryLibrary {
    pub sql: String,
    pub parameters: Vec<LibraryParameter>,
    pub depends_on: Vec<DependsOnView>,
}

/// Parses a Library resource JSON into a `SqlQueryLibrary`.
///
/// Enforces the SQLQuery profile constraints:
/// - `resourceType == "Library"`
/// - `Library.type.coding[*]` contains `{system: LibraryTypesCodes, code: sql-query}`
/// - At least one `content` attachment with `contentType` starting with `application/sql`
/// - All `relatedArtifact[type="depends-on"]` entries have a canonical URL `resource`
///   and a `label` matching `^[A-Za-z][A-Za-z0-9_]*$`
/// - All `Library.parameter[use="in"]` entries declare a `type`
pub fn parse_sqlquery_library(library_json: &Value) -> Result<SqlQueryLibrary, SqlQueryError> {
    if library_json.get("resourceType").and_then(|v| v.as_str()) != Some("Library") {
        return Err(SqlQueryError::MalformedLibrary(
            "resourceType must be 'Library'".to_string(),
        ));
    }

    validate_library_type(library_json)?;

    let sql = extract_sql(library_json)?;
    let parameters = extract_parameters(library_json)?;
    let depends_on = extract_depends_on(library_json)?;

    Ok(SqlQueryLibrary {
        sql,
        parameters,
        depends_on,
    })
}

/// Spec: `Library.type` SHALL carry `LibraryTypesCodes#sql-query`.
fn validate_library_type(library_json: &Value) -> Result<(), SqlQueryError> {
    let codings = library_json
        .get("type")
        .and_then(|t| t.get("coding"))
        .and_then(|c| c.as_array())
        .ok_or_else(|| {
            SqlQueryError::MalformedLibrary(
                "Library.type.coding[] is required and must include LibraryTypesCodes#sql-query"
                    .to_string(),
            )
        })?;
    let ok = codings.iter().any(|c| {
        let code = c.get("code").and_then(|v| v.as_str());
        let system = c.get("system").and_then(|v| v.as_str());
        code == Some(LIBRARY_TYPE_CODE) && (system.is_none() || system == Some(LIBRARY_TYPE_SYSTEM))
    });
    if !ok {
        return Err(SqlQueryError::MalformedLibrary(format!(
            "Library.type must include coding {{system: {LIBRARY_TYPE_SYSTEM}, code: {LIBRARY_TYPE_CODE}}}"
        )));
    }
    Ok(())
}

/// Spec dialect-selection: prefer an `application/sql;dialect=<ENGINE_DIALECT>`
/// attachment, fall back to bare `application/sql`, then any other
/// `application/sql*` variant.
fn extract_sql(library_json: &Value) -> Result<String, SqlQueryError> {
    let content = library_json
        .get("content")
        .and_then(|c| c.as_array())
        .ok_or(SqlQueryError::MissingSql)?;

    // Bucket attachments by specificity so we can pick deterministically.
    let mut dialect_match: Option<&Value> = None;
    let mut bare: Option<&Value> = None;
    let mut other: Option<&Value> = None;

    for entry in content {
        let ct = entry
            .get("contentType")
            .and_then(|v| v.as_str())
            .unwrap_or("");
        if !ct.starts_with("application/sql") {
            // Profile constraint `sql-must-be-sql-expressions` says every
            // content.contentType SHALL start with `application/sql`. We're
            // tolerant for now (skip), since the entire content array isn't
            // required to be pure SQL in practice; but we don't pick this one.
            continue;
        }
        if let Some(rest) = ct.strip_prefix("application/sql").map(str::trim_start) {
            if rest.is_empty() {
                if bare.is_none() {
                    bare = Some(entry);
                }
            } else if parses_dialect(rest, ENGINE_DIALECT) {
                dialect_match = Some(entry);
            } else if other.is_none() {
                other = Some(entry);
            }
        }
    }

    let chosen = dialect_match
        .or(bare)
        .or(other)
        .ok_or(SqlQueryError::MissingSql)?;
    read_sql_from_attachment(chosen)
}

/// Returns `true` if a contentType suffix like `;dialect=sqlite` matches
/// `dialect`. Handles whitespace and quoted values.
fn parses_dialect(suffix: &str, dialect: &str) -> bool {
    // `;dialect=sqlite`, `; dialect=SQLite`, `;dialect="sqlite"`
    let suffix = suffix.trim_start_matches(';').trim();
    for part in suffix.split(';') {
        let kv = part.trim();
        if let Some(value) = kv.strip_prefix("dialect=") {
            let v = value.trim_matches('"').trim();
            if v.eq_ignore_ascii_case(dialect) {
                return true;
            }
        }
    }
    false
}

fn read_sql_from_attachment(entry: &Value) -> Result<String, SqlQueryError> {
    // Preferred per profile: base64 `data`.
    if let Some(data_b64) = entry.get("data").and_then(|v| v.as_str()) {
        let bytes = base64::engine::general_purpose::STANDARD
            .decode(data_b64)
            .map_err(|e| {
                SqlQueryError::MalformedLibrary(format!(
                    "Library.content[].data is not valid base64: {e}"
                ))
            })?;
        return String::from_utf8(bytes).map_err(|e| {
            SqlQueryError::MalformedLibrary(format!("Library.content[].data is not UTF-8: {e}"))
        });
    }
    // Fallback: sql-text extension carrying plain-text SQL.
    if let Some(extensions) = entry.get("extension").and_then(|v| v.as_array()) {
        for ext in extensions {
            let url = ext.get("url").and_then(|v| v.as_str()).unwrap_or("");
            // Accept either the official URL or a relative form.
            let is_sql_text = url.ends_with("/sql-text") || url == "sql-text";
            if is_sql_text {
                if let Some(s) = ext.get("valueString").and_then(|v| v.as_str()) {
                    return Ok(s.to_string());
                }
            }
        }
    }
    Err(SqlQueryError::MissingSql)
}

fn extract_parameters(library_json: &Value) -> Result<Vec<LibraryParameter>, SqlQueryError> {
    let Some(arr) = library_json.get("parameter").and_then(|v| v.as_array()) else {
        return Ok(Vec::new());
    };

    let mut out = Vec::new();
    for p in arr {
        if p.get("use").and_then(|v| v.as_str()) != Some("in") {
            // SQLQuery profile only defines `use=in` parameters. `out` (and
            // any other value) has no defined semantics for $sqlquery-run,
            // so silently skip rather than reject — keeps us forward-
            // compatible with future profile additions.
            continue;
        }
        let name = p.get("name").and_then(|v| v.as_str()).ok_or_else(|| {
            SqlQueryError::MalformedLibrary(
                "Library.parameter[*].name is required for use=in entries".to_string(),
            )
        })?;
        let type_code = p.get("type").and_then(|v| v.as_str()).ok_or_else(|| {
            SqlQueryError::MalformedLibrary(format!(
                "Library.parameter[name='{name}'].type is required (profile cardinality 1..1)"
            ))
        })?;
        let (has_default, default_value) = read_default(p);
        out.push(LibraryParameter {
            name: name.to_string(),
            type_code: type_code.to_string(),
            has_default,
            default_value,
        });
    }
    Ok(out)
}

fn read_default(entry: &Value) -> (bool, Option<Value>) {
    if let Some(obj) = entry.as_object() {
        for (k, v) in obj {
            if let Some(rest) = k.strip_prefix("default") {
                if !rest.is_empty() {
                    return (true, Some(v.clone()));
                }
            }
        }
    }
    (false, None)
}

fn extract_depends_on(library_json: &Value) -> Result<Vec<DependsOnView>, SqlQueryError> {
    let Some(rels) = library_json
        .get("relatedArtifact")
        .and_then(|v| v.as_array())
    else {
        return Ok(Vec::new());
    };

    let mut out = Vec::new();
    let mut seen_labels = std::collections::HashSet::new();
    for entry in rels {
        if entry.get("type").and_then(|v| v.as_str()) != Some("depends-on") {
            continue;
        }
        let label = entry
            .get("label")
            .and_then(|v| v.as_str())
            .ok_or(SqlQueryError::MissingDependsOnLabel)?;
        if !is_valid_sql_label(label) {
            return Err(SqlQueryError::MalformedLibrary(format!(
                "relatedArtifact.label '{label}' violates the sql-name constraint \
                 (^[A-Za-z][A-Za-z0-9_]*$)"
            )));
        }
        if !seen_labels.insert(label.to_string()) {
            return Err(SqlQueryError::MalformedLibrary(format!(
                "duplicate depends-on label '{label}'"
            )));
        }
        // Profile pins `relatedArtifact.resource` to canonical([Resource]).
        let url = entry
            .get("resource")
            .and_then(|v| v.as_str())
            .ok_or_else(|| {
                SqlQueryError::MalformedLibrary(format!(
                    "relatedArtifact label='{label}' must carry a canonical URL in 'resource'; \
                     inline ViewDefinition resources are not part of the SQLQuery profile"
                ))
            })?;
        if url.is_empty() {
            return Err(SqlQueryError::MalformedLibrary(format!(
                "relatedArtifact label='{label}' has an empty 'resource' canonical URL"
            )));
        }
        out.push(DependsOnView {
            label: label.to_string(),
            url: url.to_string(),
        });
    }
    Ok(out)
}

/// Spec invariant `sql-name`: `^[A-Za-z][A-Za-z0-9_]*$`.
pub fn is_valid_sql_label(name: &str) -> bool {
    let mut chars = name.chars();
    let Some(first) = chars.next() else {
        return false;
    };
    if !first.is_ascii_alphabetic() {
        return false;
    }
    chars.all(|c| c.is_ascii_alphanumeric() || c == '_')
}

#[cfg(test)]
mod tests {
    use super::*;
    use base64::engine::general_purpose::STANDARD;
    use serde_json::json;

    fn library_skeleton(sql: &str) -> Value {
        let data = STANDARD.encode(sql.as_bytes());
        json!({
            "resourceType": "Library",
            "type": {"coding": [{"system": LIBRARY_TYPE_SYSTEM, "code": LIBRARY_TYPE_CODE}]},
            "content": [{ "contentType": "application/sql", "data": data }]
        })
    }

    #[test]
    fn parses_minimal_library() {
        let lib = library_skeleton("SELECT 1");
        let parsed = parse_sqlquery_library(&lib).unwrap();
        assert_eq!(parsed.sql, "SELECT 1");
        assert!(parsed.parameters.is_empty());
        assert!(parsed.depends_on.is_empty());
    }

    #[test]
    fn parses_sql_text_extension() {
        let mut lib = library_skeleton("ignored");
        lib["content"] = json!([{
            "contentType": "application/sql",
            "extension": [{
                "url": "https://sql-on-fhir.org/ig/StructureDefinition/sql-text",
                "valueString": "SELECT 2"
            }]
        }]);
        let parsed = parse_sqlquery_library(&lib).unwrap();
        assert_eq!(parsed.sql, "SELECT 2");
    }

    #[test]
    fn picks_engine_dialect_over_default() {
        let lib_sqlite = STANDARD.encode("SELECT sqlite_version()");
        let lib_default = STANDARD.encode("SELECT 'default'");
        let lib_pg = STANDARD.encode("SELECT pg_version()");
        let mut lib = library_skeleton("placeholder");
        lib["content"] = json!([
            { "contentType": "application/sql;dialect=postgresql", "data": lib_pg },
            { "contentType": "application/sql", "data": lib_default },
            { "contentType": "application/sql;dialect=sqlite", "data": lib_sqlite },
        ]);
        let parsed = parse_sqlquery_library(&lib).unwrap();
        assert_eq!(parsed.sql, "SELECT sqlite_version()");
    }

    #[test]
    fn falls_back_to_bare_when_no_dialect_match() {
        let lib_default = STANDARD.encode("SELECT 'default'");
        let lib_pg = STANDARD.encode("SELECT pg_version()");
        let mut lib = library_skeleton("placeholder");
        lib["content"] = json!([
            { "contentType": "application/sql;dialect=postgresql", "data": lib_pg },
            { "contentType": "application/sql", "data": lib_default },
        ]);
        let parsed = parse_sqlquery_library(&lib).unwrap();
        assert_eq!(parsed.sql, "SELECT 'default'");
    }

    #[test]
    fn rejects_non_library() {
        let err = parse_sqlquery_library(&json!({"resourceType": "Bundle"})).unwrap_err();
        assert!(matches!(err, SqlQueryError::MalformedLibrary(_)));
    }

    #[test]
    fn rejects_library_without_sql_query_type() {
        let mut lib = library_skeleton("SELECT 1");
        lib["type"] = json!({"coding": [{"code": "logic-library"}]});
        let err = parse_sqlquery_library(&lib).unwrap_err();
        assert!(matches!(err, SqlQueryError::MalformedLibrary(_)));
    }

    #[test]
    fn rejects_library_without_type() {
        let mut lib = library_skeleton("SELECT 1");
        lib.as_object_mut().unwrap().remove("type");
        let err = parse_sqlquery_library(&lib).unwrap_err();
        assert!(matches!(err, SqlQueryError::MalformedLibrary(_)));
    }

    #[test]
    fn rejects_no_sql() {
        let mut lib = library_skeleton("ignored");
        lib.as_object_mut().unwrap().remove("content");
        let err = parse_sqlquery_library(&lib).unwrap_err();
        assert!(matches!(err, SqlQueryError::MissingSql));
    }

    #[test]
    fn parses_parameters_and_depends_on() {
        let mut lib = library_skeleton("SELECT * FROM t");
        lib["parameter"] = json!([
            {"name": "p1", "use": "in", "type": "integer"},
            {"name": "p2", "use": "out", "type": "string"} // skipped silently
        ]);
        lib["relatedArtifact"] = json!([
            {"type": "depends-on", "label": "t", "resource": "http://example.org/VD"},
            {"type": "documentation", "label": "ignored"}
        ]);
        let parsed = parse_sqlquery_library(&lib).unwrap();
        assert_eq!(parsed.parameters.len(), 1);
        assert_eq!(parsed.parameters[0].name, "p1");
        assert_eq!(parsed.parameters[0].type_code, "integer");
        assert_eq!(parsed.depends_on.len(), 1);
        assert_eq!(parsed.depends_on[0].label, "t");
        assert_eq!(parsed.depends_on[0].url, "http://example.org/VD");
    }

    #[test]
    fn rejects_parameter_without_type() {
        let mut lib = library_skeleton("SELECT 1");
        lib["parameter"] = json!([{"name": "p1", "use": "in"}]);
        let err = parse_sqlquery_library(&lib).unwrap_err();
        assert!(matches!(err, SqlQueryError::MalformedLibrary(_)));
    }

    #[test]
    fn rejects_depends_on_without_label() {
        let mut lib = library_skeleton("SELECT 1");
        lib["relatedArtifact"] = json!([
            {"type": "depends-on", "resource": "http://example.org/VD"}
        ]);
        let err = parse_sqlquery_library(&lib).unwrap_err();
        assert!(matches!(err, SqlQueryError::MissingDependsOnLabel));
    }

    #[test]
    fn rejects_label_violating_sql_name_invariant() {
        let mut lib = library_skeleton("SELECT 1");
        lib["relatedArtifact"] = json!([
            {"type": "depends-on", "label": "1bad", "resource": "http://example.org/VD"}
        ]);
        let err = parse_sqlquery_library(&lib).unwrap_err();
        assert!(matches!(err, SqlQueryError::MalformedLibrary(_)));
    }

    #[test]
    fn rejects_duplicate_label() {
        let mut lib = library_skeleton("SELECT 1");
        lib["relatedArtifact"] = json!([
            {"type": "depends-on", "label": "t", "resource": "http://example.org/A"},
            {"type": "depends-on", "label": "t", "resource": "http://example.org/B"}
        ]);
        let err = parse_sqlquery_library(&lib).unwrap_err();
        assert!(matches!(err, SqlQueryError::MalformedLibrary(_)));
    }

    #[test]
    fn rejects_inline_view_definition() {
        let mut lib = library_skeleton("SELECT 1");
        lib["relatedArtifact"] = json!([
            {"type": "depends-on", "label": "t", "resource": {"resourceType": "ViewDefinition"}}
        ]);
        let err = parse_sqlquery_library(&lib).unwrap_err();
        assert!(matches!(err, SqlQueryError::MalformedLibrary(_)));
    }

    #[test]
    fn label_invariant_helper() {
        assert!(is_valid_sql_label("abc"));
        assert!(is_valid_sql_label("A1_b"));
        assert!(!is_valid_sql_label(""));
        assert!(!is_valid_sql_label("1abc"));
        assert!(!is_valid_sql_label("_abc"));
        assert!(!is_valid_sql_label("a-b"));
        assert!(!is_valid_sql_label("a b"));
        assert!(!is_valid_sql_label("a\"b"));
    }
}