rh-foundation
Foundation crate providing common utilities, error handling, and shared functionality for the RH workspace.
Overview
rh-foundation serves as the foundational layer for all other crates in the RH workspace, providing:
- Error Handling: Common error types with context support
- Configuration: Traits and utilities for configuration management
- I/O Operations: File reading/writing with JSON support
- HTTP Client: Optional HTTP utilities with async support (feature:
http) - Package Loader: FHIR package downloading from npm-style registries (feature:
http, module:loader) - 📖 Documentation - Snapshot Generator: StructureDefinition snapshot generation with differential merging (module:
snapshot) - 📖 Documentation - JSON Utilities: Convenient JSON parsing and serialization
- CLI Utilities: Common CLI patterns for input/output and formatting
- WASM Utilities: WebAssembly helpers (feature:
wasm)
Features
- Default: Core functionality (error handling, config, I/O, JSON, CLI, snapshot generation)
- http: Enables HTTP client, FHIR package loader, and enhanced snapshot loading (requires
reqwest,tokio,url,tar,flate2) - wasm: Enables WebAssembly utilities (requires
wasm-bindgen)
Usage
Add to your Cargo.toml:
[]
= { = "../rh-foundation" }
# Or with HTTP support
= { = "../rh-foundation", = ["http"] }
Error Handling
use ;
Configuration
use Config;
use ;
I/O Operations
use io;
// Load configuration from file
let config: MyConfig = load_config_from_file?;
// Save configuration to file
save_config_to_file?;
// Read/write JSON
let data: SomeType = read_json?;
write_json?; // true = pretty print
HTTP Client (with http feature)
use HttpClient;
async
JSON Utilities
use json;
// Parse JSON string
let data: MyType = parse?;
// Serialize to JSON string
let json = stringify?; // true = pretty print
// Parse from bytes
let data: MyType = from_bytes?;
// Serialize to bytes
let bytes = to_bytes?; // false = compact
CLI Utilities (NEW!)
use cli;
use PathBuf;
// Read input from file, inline string, or stdin
let content = read_input?;
let content = ?;
let content = ?; // from stdin
// Read from PathBuf or stdin (common in clap CLIs)
let path: = Some;
let content = read_input?;
// Read and parse JSON
let data: MyType = read_json?;
// Write output to file or stdout
write_output?;
write_output?; // to stdout
// Format output with different styles
use OutputFormat;
print_with_format?;
print_with_format?;
print_with_format?;
// Print results with error handling
let success = print_result;
// Conditional exit (useful for --strict flags)
exit_if;
Package Loader (with http feature)
Download and manage FHIR packages from npm-style registries.
📖 Full Documentation: LOADER.md - Complete guide including HL7 tooling compatibility, private registries, caching strategies, and performance optimization.
use ;
use Path;
async
Snapshot Generator
Generate complete snapshots from FHIR StructureDefinitions with differential elements.
📖 Full Documentation: SNAPSHOT.md - Complete guide including design philosophy, architecture, performance optimization, and advanced usage patterns.
use ;
use Path;
Snapshot Generation Features:
- Differential Merging: Automatically merges base and differential elements
- Cardinality Validation: Ensures differential constraints are stricter than base
- Type Restriction: Validates differential types are subsets of base types
- Binding Validation: Verifies binding strength hierarchy
- Slice Support: Handles slicing and reslicing with proper element expansion
- Circular Dependency Detection: Prevents infinite loops in inheritance chains
- Caching: Caches generated snapshots for performance
Snapshot Loader
Load StructureDefinitions from various sources:
use StructureDefinitionLoader;
use Path;
// Load from a single file
let sd = load_from_file?;
// Load all StructureDefinitions from a directory
let definitions = load_from_directory?;
// Load from a FHIR package
let package_definitions = load_from_package?;
println!;
Module Structure
rh-foundation/
├── error.rs - Error types and utilities
├── config.rs - Configuration traits
├── io.rs - File I/O operations
├── json.rs - JSON utilities
├── cli.rs - CLI utilities
├── validation.rs - FHIR validation types
├── http.rs - HTTP client (feature: http)
├── loader.rs - FHIR package loader (feature: http)
├── wasm.rs - WASM utilities (feature: wasm)
└── snapshot/ - Snapshot generation
├── error.rs - Snapshot-specific errors
├── generator.rs - Snapshot generator
├── sd_loader.rs - StructureDefinition loader
├── merger.rs - Element merging logic
├── path.rs - ElementPath utilities
└── types.rs - FHIR types
Migration from rh-common
This crate replaces the old rh-common and rh-core crates. To migrate:
- Update
Cargo.toml: Replacerh-commonwithrh-foundation - Update imports: Change
use rh_common::*touse rh_foundation::* - Update error types:
CommonError→FoundationError
License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.