clincalc 0.2.2

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
// SPDX-FileCopyrightText: 2026 Marcus Baw and Baw Medical Ltd
// SPDX-License-Identifier: AGPL-3.0-or-later

//! Charlson Comorbidity Index (CCI).
//!
//! Nineteen weighted comorbidities predicting 10-year mortality.
//! Charlson et al. (1987) original weights. Optional age adjustment
//! adds 1 point per decade above 50 (Charlson 1994 update).
//!
//! Weights:
//!  1 point: MI, CHF, PVD, CVD/TIA, dementia, COPD, connective tissue disease,
//!           peptic ulcer, mild liver disease, diabetes (uncomplicated)
//!  2 points: diabetes with end-organ damage, hemiplegia/paraplegia,
//!            CKD moderate-severe, solid tumour (non-metastatic),
//!            leukaemia, lymphoma
//!  3 points: moderate/severe liver disease
//!  6 points: metastatic solid tumour, AIDS/HIV

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

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

pub const NAME: &str = "charlson";

pub const LICENSE: CalculatorLicense = CalculatorLicense {
    license: "Public-domain method - implemented from the primary literature",
    source_url: "https://doi.org/10.1016/0021-9681(87)90171-8",
};

pub const REFERENCE: &str = "Charlson ME, Pompei P, Ales KL, MacKenzie CR. A new method of classifying \
prognostic comorbidity in longitudinal studies: development and validation. J Chronic Dis. \
1987;40(5):373-383. doi:10.1016/0021-9681(87)90171-8";

#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct CharlsonInput {
    /// Age in years (used for optional age adjustment: +1 per decade over 50)
    pub age: u8,
    /// Include the age adjustment (+1 per decade over age 50)
    pub include_age_adjustment: bool,

    // --- 1-point conditions ---
    pub myocardial_infarction: bool,
    pub congestive_heart_failure: bool,
    pub peripheral_vascular_disease: bool,
    /// Cerebrovascular disease or TIA
    pub cerebrovascular_disease: bool,
    pub dementia: bool,
    /// Chronic obstructive pulmonary disease
    pub copd: bool,
    /// Connective tissue / rheumatologic disease
    pub connective_tissue_disease: bool,
    pub peptic_ulcer_disease: bool,
    /// Mild liver disease (cirrhosis without portal hypertension, chronic hepatitis)
    pub mild_liver_disease: bool,
    /// Diabetes mellitus without end-organ damage
    pub diabetes_uncomplicated: bool,

    // --- 2-point conditions ---
    /// Diabetes with end-organ damage (retinopathy, neuropathy, nephropathy)
    pub diabetes_complicated: bool,
    /// Hemiplegia or paraplegia
    pub hemiplegia_paraplegia: bool,
    /// Renal disease, moderate or severe (CKD stage 3+, dialysis, transplant)
    pub renal_disease_moderate_severe: bool,
    /// Solid tumour, non-metastatic (treated within 5 years counts)
    pub solid_tumour_non_metastatic: bool,
    pub leukaemia: bool,
    pub lymphoma: bool,

    // --- 3-point condition ---
    /// Moderate or severe liver disease (portal hypertension, varices, encephalopathy)
    pub liver_disease_moderate_severe: bool,

    // --- 6-point conditions ---
    pub metastatic_solid_tumour: bool,
    /// AIDS (not merely HIV-positive)
    pub aids: bool,
}

#[derive(Debug, Clone, PartialEq)]
pub struct CharlsonOutcome {
    pub score: u8,
    pub age_adjustment: u8,
    pub total: u8,
    pub interpretation: String,
}

pub fn compute(input: &CharlsonInput) -> Result<CharlsonOutcome, CalcError> {
    let mut score: u8 = 0;

    // 1-point
    if input.myocardial_infarction {
        score += 1;
    }
    if input.congestive_heart_failure {
        score += 1;
    }
    if input.peripheral_vascular_disease {
        score += 1;
    }
    if input.cerebrovascular_disease {
        score += 1;
    }
    if input.dementia {
        score += 1;
    }
    if input.copd {
        score += 1;
    }
    if input.connective_tissue_disease {
        score += 1;
    }
    if input.peptic_ulcer_disease {
        score += 1;
    }
    if input.mild_liver_disease && !input.liver_disease_moderate_severe {
        score += 1;
    }
    if input.diabetes_uncomplicated && !input.diabetes_complicated {
        score += 1;
    }

    // 2-point
    if input.diabetes_complicated {
        score += 2;
    }
    if input.hemiplegia_paraplegia {
        score += 2;
    }
    if input.renal_disease_moderate_severe {
        score += 2;
    }
    if input.solid_tumour_non_metastatic && !input.metastatic_solid_tumour {
        score += 2;
    }
    if input.leukaemia {
        score += 2;
    }
    if input.lymphoma {
        score += 2;
    }

    // 3-point
    if input.liver_disease_moderate_severe {
        score += 3;
    }

    // 6-point
    if input.metastatic_solid_tumour {
        score += 6;
    }
    if input.aids {
        score += 6;
    }

    // Age adjustment: +1 per decade over 50
    let age_adjustment = if input.include_age_adjustment && input.age > 50 {
        (input.age - 51) / 10 + 1
    } else {
        0
    };

    let total = score + age_adjustment;

    let ten_year_survival = match total {
        0 => "~98%",
        1..=2 => "~89%",
        3..=4 => "~77%",
        _ => "~21%",
    };

    let adjustment_note = if input.include_age_adjustment && age_adjustment > 0 {
        format!(" (comorbidity score {score} + age adjustment +{age_adjustment})")
    } else {
        String::new()
    };

    let interpretation = format!(
        "Charlson Comorbidity Index {total}{adjustment_note}. \
Estimated 10-year survival: {ten_year_survival} (original Charlson 1987 cohort). \
CCI is a summary measure; absolute survival estimates should be interpreted with caution \
in contemporary populations."
    );

    Ok(CharlsonOutcome {
        score,
        age_adjustment,
        total,
        interpretation,
    })
}

pub fn build_response(input: &CharlsonInput) -> Result<CalculationResponse, CalcError> {
    let o = compute(input)?;

    let mut working = Map::new();
    working.insert("comorbidity_score".into(), json!(o.score));
    working.insert("age_adjustment".into(), json!(o.age_adjustment));
    working.insert("cci_total".into(), json!(o.total));

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

pub struct Charlson;

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

    fn title(&self) -> &'static str {
        "Charlson Comorbidity Index (CCI)"
    }

    fn description(&self) -> &'static str {
        "Predicts 10-year mortality from 19 weighted comorbidities, with optional age adjustment."
    }

    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": "CharlsonInput",
            "type": "object",
            "additionalProperties": false,
            "required": [
                "age", "include_age_adjustment",
                "myocardial_infarction", "congestive_heart_failure",
                "peripheral_vascular_disease", "cerebrovascular_disease",
                "dementia", "copd", "connective_tissue_disease",
                "peptic_ulcer_disease", "mild_liver_disease", "diabetes_uncomplicated",
                "diabetes_complicated", "hemiplegia_paraplegia",
                "renal_disease_moderate_severe", "solid_tumour_non_metastatic",
                "leukaemia", "lymphoma", "liver_disease_moderate_severe",
                "metastatic_solid_tumour", "aids"
            ],
            "properties": {
                "age": { "type": "integer", "minimum": 18, "maximum": 120 },
                "include_age_adjustment": {
                    "type": "boolean",
                    "description": "Add +1 per decade over age 50 (Charlson 1994 update)"
                },
                "myocardial_infarction": { "type": "boolean", "description": "History of MI (1 pt)" },
                "congestive_heart_failure": { "type": "boolean", "description": "CHF (1 pt)" },
                "peripheral_vascular_disease": { "type": "boolean", "description": "PVD or claudication (1 pt)" },
                "cerebrovascular_disease": { "type": "boolean", "description": "Stroke or TIA (1 pt)" },
                "dementia": { "type": "boolean", "description": "Dementia (1 pt)" },
                "copd": { "type": "boolean", "description": "COPD (1 pt)" },
                "connective_tissue_disease": { "type": "boolean", "description": "Connective tissue / rheumatologic disease (1 pt)" },
                "peptic_ulcer_disease": { "type": "boolean", "description": "Peptic ulcer disease (1 pt)" },
                "mild_liver_disease": { "type": "boolean", "description": "Mild liver disease (chronic hepatitis, cirrhosis without portal hypertension) (1 pt; superseded by moderate/severe)" },
                "diabetes_uncomplicated": { "type": "boolean", "description": "Diabetes without end-organ damage (1 pt; superseded by complicated)" },
                "diabetes_complicated": { "type": "boolean", "description": "Diabetes with end-organ damage (retinopathy, neuropathy, nephropathy) (2 pts)" },
                "hemiplegia_paraplegia": { "type": "boolean", "description": "Hemiplegia or paraplegia (2 pts)" },
                "renal_disease_moderate_severe": { "type": "boolean", "description": "Moderate/severe CKD, dialysis, or transplant (2 pts)" },
                "solid_tumour_non_metastatic": { "type": "boolean", "description": "Solid tumour (non-metastatic, treated within 5 years) (2 pts; superseded by metastatic)" },
                "leukaemia": { "type": "boolean", "description": "Leukaemia (2 pts)" },
                "lymphoma": { "type": "boolean", "description": "Lymphoma (2 pts)" },
                "liver_disease_moderate_severe": { "type": "boolean", "description": "Moderate/severe liver disease (portal hypertension, varices, encephalopathy) (3 pts)" },
                "metastatic_solid_tumour": { "type": "boolean", "description": "Metastatic solid tumour (6 pts)" },
                "aids": { "type": "boolean", "description": "AIDS (not HIV+ alone) (6 pts)" }
            }
        })
    }

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

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

    fn healthy(age: u8) -> CharlsonInput {
        CharlsonInput {
            age,
            include_age_adjustment: false,
            myocardial_infarction: false,
            congestive_heart_failure: false,
            peripheral_vascular_disease: false,
            cerebrovascular_disease: false,
            dementia: false,
            copd: false,
            connective_tissue_disease: false,
            peptic_ulcer_disease: false,
            mild_liver_disease: false,
            diabetes_uncomplicated: false,
            diabetes_complicated: false,
            hemiplegia_paraplegia: false,
            renal_disease_moderate_severe: false,
            solid_tumour_non_metastatic: false,
            leukaemia: false,
            lymphoma: false,
            liver_disease_moderate_severe: false,
            metastatic_solid_tumour: false,
            aids: false,
        }
    }

    #[test]
    fn zero_score_young_healthy() {
        let o = compute(&healthy(45)).unwrap();
        assert_eq!(o.total, 0);
        assert!(o.interpretation.contains("98%"));
    }

    #[test]
    fn age_adjustment_adds_correctly() {
        // Age 60 -> decade over 50 = 1 adjustment point
        let o = compute(&CharlsonInput {
            include_age_adjustment: true,
            ..healthy(60)
        })
        .unwrap();
        assert_eq!(o.age_adjustment, 1);
        assert_eq!(o.total, 1);

        // Age 70 -> 2 adjustment points
        let o2 = compute(&CharlsonInput {
            include_age_adjustment: true,
            ..healthy(70)
        })
        .unwrap();
        assert_eq!(o2.age_adjustment, 2);
    }

    #[test]
    fn severe_liver_supersedes_mild() {
        // mild_liver_disease=true but liver_disease_moderate_severe=true -> only 3 pts, not 4
        let o = compute(&CharlsonInput {
            mild_liver_disease: true,
            liver_disease_moderate_severe: true,
            ..healthy(50)
        })
        .unwrap();
        assert_eq!(o.score, 3);
    }

    #[test]
    fn metastatic_supersedes_non_metastatic() {
        let o = compute(&CharlsonInput {
            solid_tumour_non_metastatic: true,
            metastatic_solid_tumour: true,
            ..healthy(50)
        })
        .unwrap();
        assert_eq!(o.score, 6);
    }

    #[test]
    fn aids_scores_six() {
        let o = compute(&CharlsonInput {
            aids: true,
            ..healthy(40)
        })
        .unwrap();
        assert_eq!(o.score, 6);
    }

    #[test]
    fn dynamic_calculate_matches_typed() {
        let base = healthy(50);
        let value = json!({
            "age": 50,
            "include_age_adjustment": false,
            "myocardial_infarction": false,
            "congestive_heart_failure": false,
            "peripheral_vascular_disease": false,
            "cerebrovascular_disease": false,
            "dementia": false,
            "copd": false,
            "connective_tissue_disease": false,
            "peptic_ulcer_disease": false,
            "mild_liver_disease": false,
            "diabetes_uncomplicated": false,
            "diabetes_complicated": false,
            "hemiplegia_paraplegia": false,
            "renal_disease_moderate_severe": false,
            "solid_tumour_non_metastatic": false,
            "leukaemia": false,
            "lymphoma": false,
            "liver_disease_moderate_severe": false,
            "metastatic_solid_tumour": false,
            "aids": false
        });
        let dynamic = Charlson.calculate(&value).unwrap();
        let typed = build_response(&base).unwrap();
        assert_eq!(dynamic, typed);
    }
}