clincalc 0.2.1

Open, auditable clinical calculators: a pure scoring engine plus the `clincalc` CLI in one crate. The engine is a serde-only leaf (build with default-features = false); the default `cli` feature adds the `clincalc` binary.
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
// SPDX-FileCopyrightText: 2026 Marcus Baw and Baw Medical Ltd
// SPDX-License-Identifier: AGPL-3.0-or-later

//! TIMI Risk Score for UA/NSTEMI - 14-day adverse-event risk in unstable
//! angina / non-ST-elevation MI.
//!
//! This is the TIMI risk score for unstable angina / NSTEMI (Antman EM et al,
//! JAMA 2000), NOT the separate TIMI score for STEMI. Seven equally-weighted
//! criteria each score 1 point (total 0-7); the score predicts the 14-day risk
//! of the composite endpoint of all-cause death, new or recurrent myocardial
//! infarction, or severe recurrent ischaemia requiring urgent revascularisation.
//!
//! Two criteria carry input definitions because they are easy to get subtly
//! wrong: "3+ CAD risk factors" (which factors count, and that it is a count of
//! at least 3, not their mere presence) and "known CAD" (which means documented
//! coronary stenosis of at least 50%, not simply a history of angina).
//!
//! Age is taken as an integer and the ">=65" point is derived, so a
//! contradictory age/flag pair is impossible.

use serde::{Deserialize, Serialize};
use serde_json::{Map, Value, json};

use crate::calculator::{CalcError, Calculator};
use crate::license::CalculatorLicense;
use crate::response::CalculationResponse;

/// Machine name.
pub const NAME: &str = "timi";

/// Primary citation.
pub const REFERENCE: &str = "Antman EM, Cohen M, Bernink PJLM, et al. The TIMI risk score for unstable angina/non-ST \
elevation MI: a method for prognostication and therapeutic decision making. JAMA. \
2000;284(7):835-842. This is the UA/NSTEMI score, distinct from the TIMI score for STEMI.";

/// Distribution licence: the score is a published clinical method, implemented
/// here from the primary literature.
pub const LICENSE: CalculatorLicense = CalculatorLicense {
    license: "Public-domain method - implemented from the primary literature",
    source_url: "https://doi.org/10.1001/jama.284.7.835",
};

/// TIMI UA/NSTEMI inputs. Age is numeric; the ">=65" point is derived.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct TimiInput {
    /// Age in years (>=65 scores 1 point).
    pub age: u8,
    /// At least 3 risk factors for coronary artery disease (hypertension,
    /// hypercholesterolaemia, diabetes, family history, or current smoker).
    pub three_or_more_cad_risk_factors: bool,
    /// Known coronary artery disease: documented coronary stenosis >=50%.
    pub known_cad: bool,
    /// Aspirin use in the past 7 days.
    pub aspirin_use_past_7_days: bool,
    /// >=2 anginal episodes in the past 24 hours.
    pub severe_angina: bool,
    /// ST-segment deviation >=0.5 mm on the admission ECG.
    pub st_deviation: bool,
    /// Positive cardiac biomarker (e.g. troponin).
    pub positive_biomarker: bool,
}

/// Risk band for the 14-day composite endpoint.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RiskBand {
    /// Score 0-2.
    Low,
    /// Score 3-4.
    Intermediate,
    /// Score 5-7.
    High,
}

impl RiskBand {
    fn slug(self) -> &'static str {
        match self {
            RiskBand::Low => "low",
            RiskBand::Intermediate => "intermediate",
            RiskBand::High => "high",
        }
    }
}

/// The computed outcome.
#[derive(Debug, Clone, PartialEq)]
pub struct TimiOutcome {
    /// Total score (0-7).
    pub score: u8,
    /// Point contributed by age (0 or 1).
    pub age_point: u8,
    pub band: RiskBand,
    /// Approximate 14-day composite event rate, in percent (Antman 2000).
    pub event_rate_percent: f64,
    pub interpretation: String,
}

fn age_point(age: u8) -> u8 {
    u8::from(age >= 65)
}

fn band(score: u8) -> RiskBand {
    if score <= 2 {
        RiskBand::Low
    } else if score <= 4 {
        RiskBand::Intermediate
    } else {
        RiskBand::High
    }
}

/// Approximate 14-day composite event rate (death, MI, or urgent
/// revascularisation) from Antman et al, JAMA 2000, Table 3.
fn event_rate_percent(score: u8) -> f64 {
    match score {
        0 | 1 => 4.7,
        2 => 8.3,
        3 => 13.2,
        4 => 19.9,
        5 => 26.2,
        // Scores 6 and 7 are reported together.
        _ => 40.9,
    }
}

/// Pure scoring.
pub fn compute(input: &TimiInput) -> Result<TimiOutcome, CalcError> {
    let age_point = age_point(input.age);

    let score = age_point
        + u8::from(input.three_or_more_cad_risk_factors)
        + u8::from(input.known_cad)
        + u8::from(input.aspirin_use_past_7_days)
        + u8::from(input.severe_angina)
        + u8::from(input.st_deviation)
        + u8::from(input.positive_biomarker);

    let band = band(score);
    let event_rate_percent = event_rate_percent(score);

    let interpretation = format!(
        "TIMI score {score}/7: {} risk. Approximately {event_rate_percent}% 14-day risk of the \
composite endpoint (all-cause death, new or recurrent MI, or severe recurrent ischaemia requiring \
urgent revascularisation) (Antman et al, JAMA 2000). This is the UA/NSTEMI score, not the STEMI \
score.",
        match band {
            RiskBand::Low => "low",
            RiskBand::Intermediate => "intermediate",
            RiskBand::High => "high",
        }
    );

    Ok(TimiOutcome {
        score,
        age_point,
        band,
        event_rate_percent,
        interpretation,
    })
}

/// Build the dispatchable [`CalculationResponse`] from typed inputs.
pub fn build_response(input: &TimiInput) -> Result<CalculationResponse, CalcError> {
    let o = compute(input)?;

    let mut working = Map::new();
    working.insert("total_score".into(), json!(o.score));
    working.insert("age_point".into(), json!(o.age_point));
    working.insert(
        "three_or_more_cad_risk_factors".into(),
        json!(u8::from(input.three_or_more_cad_risk_factors)),
    );
    working.insert("known_cad".into(), json!(u8::from(input.known_cad)));
    working.insert(
        "aspirin_use_past_7_days".into(),
        json!(u8::from(input.aspirin_use_past_7_days)),
    );
    working.insert("severe_angina".into(), json!(u8::from(input.severe_angina)));
    working.insert("st_deviation".into(), json!(u8::from(input.st_deviation)));
    working.insert(
        "positive_biomarker".into(),
        json!(u8::from(input.positive_biomarker)),
    );
    working.insert("risk_band".into(), json!(o.band.slug()));
    working.insert(
        "approximate_event_rate_percent".into(),
        json!(o.event_rate_percent),
    );

    Ok(CalculationResponse {
        calculator: NAME.to_string(),
        result: json!(o.score),
        interpretation: o.interpretation,
        working,
        reference: REFERENCE.to_string(),
    })
}

/// Unit struct implementing the dynamic [`Calculator`] surface.
pub struct Timi;

impl Calculator for Timi {
    fn name(&self) -> &'static str {
        NAME
    }

    fn title(&self) -> &'static str {
        "TIMI Risk Score for UA/NSTEMI"
    }

    fn description(&self) -> &'static str {
        "14-day risk of death, MI, or urgent revascularisation in unstable angina / NSTEMI \
(Antman et al, JAMA 2000). Not the STEMI score."
    }

    fn reference(&self) -> &'static str {
        REFERENCE
    }

    fn license(&self) -> CalculatorLicense {
        LICENSE
    }

    fn input_schema(&self) -> Value {
        json!({
            "$schema": "https://json-schema.org/draft/2020-12/schema",
            "title": "TimiInput",
            "type": "object",
            "additionalProperties": false,
            "required": [
                "age", "three_or_more_cad_risk_factors", "known_cad",
                "aspirin_use_past_7_days", "severe_angina", "st_deviation",
                "positive_biomarker"
            ],
            "properties": {
                "age": {
                    "type": "integer",
                    "minimum": 18,
                    "maximum": 120,
                    "description": "Age in years (>=65 scores 1 point)"
                },
                "three_or_more_cad_risk_factors": {
                    "type": "boolean",
                    "description": "At least 3 risk factors for CAD (hypertension, hypercholesterolaemia, diabetes, family history, current smoker)",
                    "definition": {
                        "concept": "3+ CAD risk factors",
                        "statement": "Three or more of the following coronary artery disease risk factors are present: hypertension, hypercholesterolaemia, diabetes mellitus, family history of premature CAD, or current cigarette smoking.",
                        "includes": [
                            "Counting requires THREE OR MORE of the five factors - one or two does NOT score",
                            "Hypertension",
                            "Hypercholesterolaemia / dyslipidaemia",
                            "Diabetes mellitus",
                            "Family history of premature coronary artery disease",
                            "Current cigarette smoking"
                        ],
                        "excludes": [
                            "Fewer than 3 of the listed factors",
                            "Known CAD itself is scored separately and is not one of these risk factors"
                        ],
                        "caveats": "This is a COUNT of risk factors (>=3), not a single risk factor. Score only when at least three are present.",
                        "source": { "citation": "Antman EM et al. JAMA. 2000;284(7):835-842.", "url": "https://doi.org/10.1001/jama.284.7.835" },
                        "status": "draft"
                    }
                },
                "known_cad": {
                    "type": "boolean",
                    "description": "Known coronary artery disease: documented coronary stenosis >=50%",
                    "definition": {
                        "concept": "Known CAD (stenosis >=50%)",
                        "statement": "Prior documentation, on angiography or equivalent imaging, of at least one coronary artery stenosis of 50% or more.",
                        "includes": [
                            "Documented coronary stenosis >=50% on angiography",
                            "Known prior significant coronary disease confirmed by imaging"
                        ],
                        "excludes": [
                            "Risk factors for CAD without documented stenosis (those are counted separately)",
                            "A history of chest pain or angina symptoms alone, without documented >=50% stenosis"
                        ],
                        "caveats": "Requires documented stenosis >=50%, not merely suspected or risk-factor-based CAD.",
                        "snomedEcl": "<< 53741008 |Coronary arteriosclerosis (disorder)|",
                        "source": { "citation": "Antman EM et al. JAMA. 2000;284(7):835-842.", "url": "https://doi.org/10.1001/jama.284.7.835" },
                        "status": "draft"
                    }
                },
                "aspirin_use_past_7_days": {
                    "type": "boolean",
                    "description": "Aspirin use in the past 7 days"
                },
                "severe_angina": {
                    "type": "boolean",
                    "description": "Severe angina: >=2 anginal episodes in the past 24 hours"
                },
                "st_deviation": {
                    "type": "boolean",
                    "description": "ST-segment deviation >=0.5 mm on the admission ECG"
                },
                "positive_biomarker": {
                    "type": "boolean",
                    "description": "Positive cardiac biomarker (e.g. troponin or CK-MB)"
                }
            }
        })
    }

    fn calculate(&self, input: &Value) -> Result<CalculationResponse, CalcError> {
        let parsed: TimiInput = serde_json::from_value(input.clone())
            .map_err(|e| CalcError::InvalidInput(e.to_string()))?;
        build_response(&parsed)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn base(age: u8) -> TimiInput {
        TimiInput {
            age,
            three_or_more_cad_risk_factors: false,
            known_cad: false,
            aspirin_use_past_7_days: false,
            severe_angina: false,
            st_deviation: false,
            positive_biomarker: false,
        }
    }

    #[test]
    fn age_derivation() {
        assert_eq!(age_point(64), 0);
        assert_eq!(age_point(65), 1);
        assert_eq!(age_point(90), 1);
    }

    #[test]
    fn all_false_under_65_is_zero() {
        let o = compute(&base(50)).unwrap();
        assert_eq!(o.score, 0);
        assert_eq!(o.band, RiskBand::Low);
        assert_eq!(o.event_rate_percent, 4.7);
    }

    #[test]
    fn age_alone_scores_one() {
        let o = compute(&base(70)).unwrap();
        assert_eq!(o.score, 1);
        assert_eq!(o.age_point, 1);
        assert_eq!(o.band, RiskBand::Low);
        assert_eq!(o.event_rate_percent, 4.7);
    }

    #[test]
    fn worked_example_score_four() {
        // 68yo with 3+ risk factors, ST deviation, positive troponin:
        // age 1 + risk factors 1 + ST 1 + biomarker 1 = 4 -> intermediate, ~19.9%.
        let mut i = base(68);
        i.three_or_more_cad_risk_factors = true;
        i.st_deviation = true;
        i.positive_biomarker = true;
        let o = compute(&i).unwrap();
        assert_eq!(o.score, 4);
        assert_eq!(o.band, RiskBand::Intermediate);
        assert_eq!(o.event_rate_percent, 19.9);
    }

    #[test]
    fn maximum_score_is_seven() {
        let i = TimiInput {
            age: 80,
            three_or_more_cad_risk_factors: true,
            known_cad: true,
            aspirin_use_past_7_days: true,
            severe_angina: true,
            st_deviation: true,
            positive_biomarker: true,
        };
        let o = compute(&i).unwrap();
        assert_eq!(o.score, 7);
        assert_eq!(o.band, RiskBand::High);
        assert_eq!(o.event_rate_percent, 40.9);
    }

    #[test]
    fn band_boundaries() {
        assert_eq!(band(0), RiskBand::Low);
        assert_eq!(band(2), RiskBand::Low);
        assert_eq!(band(3), RiskBand::Intermediate);
        assert_eq!(band(4), RiskBand::Intermediate);
        assert_eq!(band(5), RiskBand::High);
        assert_eq!(band(7), RiskBand::High);
    }

    #[test]
    fn event_rates_match_antman_table() {
        assert_eq!(event_rate_percent(0), 4.7);
        assert_eq!(event_rate_percent(1), 4.7);
        assert_eq!(event_rate_percent(2), 8.3);
        assert_eq!(event_rate_percent(3), 13.2);
        assert_eq!(event_rate_percent(4), 19.9);
        assert_eq!(event_rate_percent(5), 26.2);
        assert_eq!(event_rate_percent(6), 40.9);
        assert_eq!(event_rate_percent(7), 40.9);
    }

    #[test]
    fn validation_rejects_missing_field() {
        // Missing positive_biomarker.
        let value = json!({
            "age": 70,
            "three_or_more_cad_risk_factors": true,
            "known_cad": false,
            "aspirin_use_past_7_days": false,
            "severe_angina": false,
            "st_deviation": false
        });
        let err = Timi.calculate(&value).unwrap_err();
        assert!(matches!(err, CalcError::InvalidInput(_)));
    }

    #[test]
    fn validation_rejects_unknown_field() {
        let value = json!({
            "age": 70,
            "three_or_more_cad_risk_factors": false,
            "known_cad": false,
            "aspirin_use_past_7_days": false,
            "severe_angina": false,
            "st_deviation": false,
            "positive_biomarker": false,
            "unexpected": true
        });
        // serde rejects unknown fields only when deny_unknown is set; here the
        // struct simply ignores extras, so this must still parse. Confirm it
        // does not error and produces a valid score (age 70 -> 1 point).
        let resp = Timi.calculate(&value).unwrap();
        assert_eq!(resp.result, json!(1));
    }

    #[test]
    fn known_cad_definition_requires_documented_stenosis() {
        let schema = Timi.input_schema();
        let excludes = &schema["properties"]["known_cad"]["definition"]["excludes"];
        assert!(
            excludes
                .as_array()
                .unwrap()
                .iter()
                .any(|e| e.as_str().unwrap().contains("without documented"))
        );
    }

    #[test]
    fn risk_factor_definition_requires_three() {
        let schema = Timi.input_schema();
        let stmt =
            schema["properties"]["three_or_more_cad_risk_factors"]["definition"]["statement"]
                .as_str()
                .unwrap();
        assert!(stmt.contains("Three or more"));
    }

    #[test]
    fn dynamic_calculate_matches_typed() {
        let value = json!({
            "age": 68,
            "three_or_more_cad_risk_factors": true,
            "known_cad": false,
            "aspirin_use_past_7_days": false,
            "severe_angina": false,
            "st_deviation": true,
            "positive_biomarker": true
        });
        let mut typed = base(68);
        typed.three_or_more_cad_risk_factors = true;
        typed.st_deviation = true;
        typed.positive_biomarker = true;
        let dynamic = Timi.calculate(&value).unwrap();
        assert_eq!(dynamic, build_response(&typed).unwrap());
        assert_eq!(dynamic.result, json!(4));
    }
}