31a0b8a485
Multi-Platform CI / test-platforms (ubuntu-22.04) (push) Successful in 18s
Multi-Platform CI / test-platforms (windows-latest) (push) Successful in 18s
Multi-Platform CI / Package for Linux x86_64 (push) Has been skipped
Multi-Platform CI / Package for Windows x86_64 (push) Has been skipped
Multi-Platform CI / Create GitHub Release (push) Has been skipped
- add C FFI enums MLBladderPhase, MLMetabolicState, MLPerfusionState - add C FFI structs MLBloodstreamMetrics and MLBladderMetrics - add ml_patient_bloodstream_metrics and ml_patient_bladder_metrics to populate metrics for a patient (mirrored in src/ffi.rs) - update examples/c/ffi_example.c to print new metrics - add tests for FFI metrics (tests/ffi.rs) organ model expansions - bloodstream: add metrics() snapshot and detailed physiology: plasma proteins/oncotic pressure, lymph return, RBC cohort tracking, erythropoiesis/clearance with HIF, iron/folate/B12 stores, platelets, coagulation/fibrinolysis, immune cell counts, complement, acid–base - bladder: introduce adaptive compliance, reflex gating, cortical/voluntary modulators, safety indices; add metrics(), summary, and unit tests - brain: add homeostatic drives (respiratory, thirst, hunger, thermo, pain), brainstem nuclei (NTS/RVLM/CVLM, nAmb/DMV, RTN), sleep cycle timing, cerebrovascular autoregulation; wire drives into autonomic control - heart: add phase-based cycle (valves and atria), conduction system, RAAS regulation, improved coronary perfusion - intestines: add micronutrient absorption feeding erythropoiesis patient coupling - expose Patient::bloodstream_metrics() and ::bladder_metrics() - integrate new organ signals (kidney osmolality, spleen culling, liver proteins) and brain–lung/continence control pathways - re-export BladderMetrics and BloodstreamMetrics in lib.rs note - existing FFI remains compatible; this is a surface addition - ffi/medicallib.h kept in sync with src/ffi.rs
4.9 KiB
4.9 KiB
CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Build, Test, and Development Commands
Core workflows
- Build library:
cargo build(debug) orcargo build --release(optimized) - Build FFI shared library:
cargo build --release --features ffi(generates C header via cbindgen) - Run tests:
cargo test(all tests) orcargo test --test <name>(single integration test file) - Run examples:
cargo run --example usage,cargo run --example patient,cargo run --example tracing_demo - Run demo monitor:
cargo run --example demo_app --features demo-monitor - Benchmarks:
cargo bench(runs Criterion benchmarks; results intarget/criterion/) - Format code:
cargo fmt --all(run before committing) - Lint:
cargo clippy --all-targets -- -D warnings(enforce before committing)
Packaging binary distributions
- Linux/macOS: Run
../scripts/package.sh [target-triple]frommedicallib_rust/ - Windows: Run
..\scripts\package.ps1 [target-triple]frommedicallib_rust\ - Artifacts placed in
medicallib_rust/dist/as both.tar.gzand.zip
Architecture Overview
Core structure
src/lib.rs: Crate facade re-exporting public API (Patient,Organ, types, errors)src/types.rs: Domain types (Blood,BloodPressure,OrganType,VitalSign)src/error.rs:MedicalErrorenum usingthiserrorsrc/patient.rs:Patientstruct orchestratingVec<Box<dyn Organ>>with inter-organ signal routingsrc/organs/mod.rs:Organtrait andOrganInfo; per-organ modules (heart, lungs, brain, etc.)src/ekg/mod.rs: EKG monitoring system with configurable leadssrc/ffi.rs: C-compatible FFI layer (featureffi); header auto-generated toffi/medicallib.hbybuild.rsusing cbindgen
Organ system
- Each organ module implements the
Organtrait (id(),organ_type(),update(dt),summary(),as_any(),as_any_mut()) Patientowns organs as trait objects, callsupdate()each tick, and routes signals between organs- Inter-organ communication examples:
- Lungs SpO2 → Heart rate adjustment
- Kidneys GFR → Bladder volume accumulation
- Brain autonomic signals → Heart rate variability and respiratory drive
- Bloodstream perfusion → multiple organ metabolic states
FFI boundary
- Opaque handle:
MLPatientpointer to boxedPatient - Functions:
ml_patient_new/free/update,ml_patient_summary,ml_patient_organ_summary,medicallib_bmi - Memory: All returned strings allocated by Rust must be freed via
ml_string_free - Build script:
build.rsruns cbindgen when--features ffiis enabled, updatesffi/medicallib.hif changed - Validation: Any change to
src/ffi.rsmust be tested againstexamples/c/ffi_example.c
Testing and benchmarks
- Integration tests:
tests/directory (e.g.,tests/patient.rs,tests/ffi.rs) - Benchmarks:
benches/heart.rsuses Criterion; compare results across runs for performance regressions - Unit tests: In-module
#[cfg(test)]blocks for focused invariants
Important Development Notes
When modifying FFI
- Edit
src/ffi.rs - Run
cargo build --features ffito regenerateffi/medicallib.hvia cbindgen - Verify C example still compiles:
gcc -o ffi_example examples/c/ffi_example.c -L target/release -lmedicallib_rust - Update
cbindgen.tomlonly if header generation settings need adjustment
Adding new organs
- Create module in
src/organs/<organ_name>.rs - Define struct implementing
Organtrait - Add module declaration and public re-export in
src/organs/mod.rs - Update
Patientsignal structs andcouple_organs()method if inter-organ communication needed - Add corresponding
OrganTypevariant insrc/types.rs - Update FFI constants in
src/ffi.rsif organ needs FFI exposure
Cargo features
serde: Enables serialization on public typesffi: Exposes C ABI; triggers cbindgen inbuild.rsdemo-monitor: Required fordemo_appexample with colorized dashboard
CI workflows
- GitHub Actions:
.github/workflows/ci.yml - Gitea Actions:
.gitea/workflows/ci.yml - Both run tests, clippy, formatting checks, and cross-platform builds
Key Patterns
Patient simulation loop
let mut patient = Patient::new("case-01")?.initialize_default();
loop {
patient.update(dt_seconds);
let summary = patient.patient_summary();
// Process vitals...
}
Accessing specific organ
let heart = patient.find_organ_typed::<Heart>()?;
println!("HR: {}", heart.heart_rate_bpm());
Inter-organ signals
Patient's couple_organs() method reads state from one organ (e.g., Lungs::spo2_pct()) and injects it into another (e.g., Heart::receive_spo2_signal()). Add new signal fields to the internal *Signals structs in patient.rs when coupling new organ interactions.