Skip to main content

helios_fhir/
lib.rs

1//! # FHIR Model Infrastructure
2//!
3//! This module provides the foundational types and infrastructure that support the
4//! generated FHIR specification implementations. It contains hand-coded types that
5//! enable the generated code to handle FHIR's complex requirements for precision,
6//! extensions, and cross-version compatibility.
7
8//!
9//! ## Architecture
10//!
11//! The FHIR crate is organized as follows:
12//! - **Generated modules** (`r4.rs`, `r4b.rs`, `r5.rs`, `r6.rs`): Complete FHIR type implementations
13//! - **Infrastructure module** (`lib.rs`): Foundational types used by generated code
14//! - **Test modules**: Validation against official FHIR examples
15//!
16//! ## Key Infrastructure Types
17//!
18//! - [`PreciseDecimal`] - High-precision decimal arithmetic preserving original string format
19//! - [`Element<T, Extension>`] - Base container for FHIR elements with extension support
20//! - [`DecimalElement<Extension>`] - Specialized element for decimal values
21//! - [`FhirVersion`] - Version enumeration for multi-version support
22//!
23//! ## Usage Example
24//!
25//! ```rust
26//! use helios_fhir::r4::{Patient, HumanName};
27//! use helios_fhir::PreciseDecimal;
28//! use rust_decimal::Decimal;
29//!
30//! // Create a patient with precise decimal handling
31//! let patient = Patient {
32//!     name: Some(vec![HumanName {
33//!         family: Some("Doe".to_string().into()),
34//!         given: Some(vec!["John".to_string().into()]),
35//!         ..Default::default()
36//!     }]),
37//!     ..Default::default()
38//! };
39//!
40//! // Work with precise decimals
41//! let precise = PreciseDecimal::from(Decimal::new(12340, 3)); // 12.340
42//! ```
43
44use chrono::{DateTime as ChronoDateTime, NaiveDate, NaiveTime, Utc};
45use helios_fhirpath_support::{EvaluationResult, IntoEvaluationResult, TypeInfoResult};
46#[cfg(feature = "xml")]
47use helios_serde_support::SingleOrVec;
48
49use rust_decimal::Decimal;
50use serde::{
51    Deserialize, Serialize,
52    de::{self, Deserializer, MapAccess, Visitor},
53    ser::{SerializeStruct, Serializer},
54};
55use std::cmp::Ordering;
56use std::fmt;
57use std::marker::PhantomData;
58use std::sync::Arc;
59
60/// Custom deserializer that is more forgiving of null values in JSON.
61///
62/// This creates a custom `Option<T>` deserializer that will return None for null values
63/// but also for any deserialization errors. This makes it possible to skip over
64/// malformed or unexpected values in FHIR JSON.
65pub fn deserialize_forgiving_option<'de, T, D>(deserializer: D) -> Result<Option<T>, D::Error>
66where
67    T: Deserialize<'de>,
68    D: Deserializer<'de>,
69{
70    // Use the intermediate Value approach to check for null first
71    let json_value = serde_json::Value::deserialize(deserializer)?;
72
73    match json_value {
74        serde_json::Value::Null => Ok(None),
75        _ => {
76            // Try to deserialize the value, but return None if it fails
77            match T::deserialize(json_value) {
78                Ok(value) => Ok(Some(value)),
79                Err(_) => Ok(None), // Ignore errors and return None
80            }
81        }
82    }
83}
84
85/// High-precision decimal type that preserves original string representation.
86///
87/// FHIR requires that decimal values maintain their original precision and format
88/// when serialized back to JSON. This type stores both the parsed `Decimal` value
89/// for mathematical operations and the original string for serialization.
90///
91/// # FHIR Precision Requirements
92///
93/// FHIR decimal values must:
94/// - Preserve trailing zeros (e.g., "12.340" vs "12.34")
95/// - Maintain original precision during round-trip serialization
96/// - Support high-precision arithmetic without floating-point errors
97/// - Handle edge cases like very large or very small numbers
98///
99/// # Examples
100///
101/// ```rust
102/// use helios_fhir::PreciseDecimal;
103/// use rust_decimal::Decimal;
104///
105/// // Create from Decimal (derives string representation)
106/// let precise = PreciseDecimal::from(Decimal::new(12340, 3)); // 12.340
107/// assert_eq!(precise.original_string(), "12.340");
108///
109/// // Create with specific string format
110/// let precise = PreciseDecimal::from_parts(
111///     Some(Decimal::new(1000, 2)),
112///     "10.00".to_string()
113/// );
114/// assert_eq!(precise.original_string(), "10.00");
115/// ```
116#[derive(Debug, Clone)]
117pub struct PreciseDecimal {
118    /// The parsed decimal value, `None` if parsing failed (e.g., out of range)
119    value: Option<Decimal>,
120    /// The original string representation preserving format and precision
121    original_string: Arc<str>,
122}
123
124/// Implements equality comparison based on the parsed decimal value.
125///
126/// Two `PreciseDecimal` values are equal if their parsed `Decimal` values are equal,
127/// regardless of their original string representations. This enables mathematical
128/// equality while preserving string format for serialization.
129///
130/// # Examples
131///
132/// ```rust
133/// use helios_fhir::PreciseDecimal;
134/// use rust_decimal::Decimal;
135///
136/// let a = PreciseDecimal::from_parts(Some(Decimal::new(100, 1)), "10.0".to_string());
137/// let b = PreciseDecimal::from_parts(Some(Decimal::new(1000, 2)), "10.00".to_string());
138/// assert_eq!(a, b); // Same decimal value (10.0 == 10.00)
139/// ```
140impl PartialEq for PreciseDecimal {
141    fn eq(&self, other: &Self) -> bool {
142        // Compare parsed decimal values for mathematical equality
143        self.value == other.value
144    }
145}
146
147/// Marker trait implementation indicating total equality for `PreciseDecimal`.
148impl Eq for PreciseDecimal {}
149
150/// Implements partial ordering based on the parsed decimal value.
151///
152/// Ordering is based on the mathematical value of the decimal, not the string
153/// representation. `None` values (unparseable decimals) are considered less than
154/// any valid decimal value.
155impl PartialOrd for PreciseDecimal {
156    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
157        Some(self.cmp(other))
158    }
159}
160
161/// Implements total ordering for `PreciseDecimal`.
162///
163/// Provides a consistent ordering for sorting operations. The ordering is based
164/// on the mathematical value: `None` < `Some(smaller_decimal)` < `Some(larger_decimal)`.
165impl Ord for PreciseDecimal {
166    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
167        self.value.cmp(&other.value)
168    }
169}
170
171// === PreciseDecimal Methods ===
172
173impl PreciseDecimal {
174    /// Creates a new `PreciseDecimal` from its constituent parts.
175    ///
176    /// This constructor allows explicit control over both the parsed value and the
177    /// original string representation. Use this when you need to preserve a specific
178    /// string format or when parsing has already been attempted.
179    ///
180    /// # Arguments
181    ///
182    /// * `value` - The parsed decimal value, or `None` if parsing failed
183    /// * `original_string` - The original string representation to preserve
184    ///
185    /// # Examples
186    ///
187    /// ```rust
188    /// use helios_fhir::PreciseDecimal;
189    /// use rust_decimal::Decimal;
190    ///
191    /// // Create with successful parsing
192    /// let precise = PreciseDecimal::from_parts(
193    ///     Some(Decimal::new(12340, 3)),
194    ///     "12.340".to_string()
195    /// );
196    ///
197    /// // Create with failed parsing (preserves original string)
198    /// let invalid = PreciseDecimal::from_parts(
199    ///     None,
200    ///     "invalid_decimal".to_string()
201    /// );
202    /// ```
203    pub fn from_parts(value: Option<Decimal>, original_string: String) -> Self {
204        Self {
205            value,
206            original_string: Arc::from(original_string.as_str()),
207        }
208    }
209
210    /// Helper method to parse a decimal string with support for scientific notation.
211    ///
212    /// This method handles the complexity of parsing decimal strings that may be in
213    /// scientific notation (with 'E' or 'e' exponents) or regular decimal format.
214    /// It normalizes 'E' to 'e' for consistent parsing while preserving the original
215    /// string representation for serialization.
216    ///
217    /// # Arguments
218    ///
219    /// * `s` - The string to parse as a decimal
220    ///
221    /// # Returns
222    ///
223    /// `Some(Decimal)` if parsing succeeds, `None` if the string is not a valid decimal.
224    ///
225    /// # Examples
226    ///
227    /// ```ignore
228    /// use helios_fhir::PreciseDecimal;
229    /// use rust_decimal::Decimal;
230    ///
231    /// // Regular decimal format
232    /// assert!(PreciseDecimal::parse_decimal_string("123.45").is_some());
233    ///
234    /// // Scientific notation with 'e'
235    /// assert!(PreciseDecimal::parse_decimal_string("1.23e2").is_some());
236    ///
237    /// // Scientific notation with 'E' (normalized to 'e')
238    /// assert!(PreciseDecimal::parse_decimal_string("1.23E2").is_some());
239    ///
240    /// // Invalid format
241    /// assert!(PreciseDecimal::parse_decimal_string("invalid").is_none());
242    /// ```
243    fn parse_decimal_string(s: &str) -> Option<Decimal> {
244        // Normalize 'E' to 'e' for consistent parsing
245        let normalized = s.replace('E', "e");
246
247        if normalized.contains('e') {
248            // Use scientific notation parsing
249            Decimal::from_scientific(&normalized).ok()
250        } else {
251            // Use regular decimal parsing
252            normalized.parse::<Decimal>().ok()
253        }
254    }
255
256    /// Returns the parsed decimal value if parsing was successful.
257    ///
258    /// This method provides access to the mathematical value for arithmetic
259    /// operations and comparisons. Returns `None` if the original string
260    /// could not be parsed as a valid decimal.
261    ///
262    /// # Examples
263    ///
264    /// ```rust
265    /// use helios_fhir::PreciseDecimal;
266    /// use rust_decimal::Decimal;
267    ///
268    /// let precise = PreciseDecimal::from(Decimal::new(1234, 2)); // 12.34
269    /// assert_eq!(precise.value(), Some(Decimal::new(1234, 2)));
270    ///
271    /// let invalid = PreciseDecimal::from_parts(None, "invalid".to_string());
272    /// assert_eq!(invalid.value(), None);
273    /// ```
274    pub fn value(&self) -> Option<Decimal> {
275        self.value
276    }
277
278    /// Returns the original string representation.
279    ///
280    /// This method provides access to the exact string format that was used
281    /// to create this `PreciseDecimal`. This string is used during serialization
282    /// to maintain FHIR's precision requirements.
283    ///
284    /// # Examples
285    ///
286    /// ```rust
287    /// use helios_fhir::PreciseDecimal;
288    /// use rust_decimal::Decimal;
289    ///
290    /// let precise = PreciseDecimal::from_parts(
291    ///     Some(Decimal::new(100, 2)),
292    ///     "1.00".to_string()
293    /// );
294    /// assert_eq!(precise.original_string(), "1.00");
295    /// ```
296    pub fn original_string(&self) -> &str {
297        &self.original_string
298    }
299}
300
301/// Converts a `Decimal` to `PreciseDecimal` with derived string representation.
302///
303/// This implementation allows easy conversion from `rust_decimal::Decimal` values
304/// by automatically generating the string representation using the decimal's
305/// `Display` implementation.
306///
307/// # Examples
308///
309/// ```rust
310/// use helios_fhir::PreciseDecimal;
311/// use rust_decimal::Decimal;
312///
313/// let decimal = Decimal::new(12345, 3); // 12.345
314/// let precise: PreciseDecimal = decimal.into();
315/// assert_eq!(precise.value(), Some(decimal));
316/// assert_eq!(precise.original_string(), "12.345");
317/// ```
318impl From<Decimal> for PreciseDecimal {
319    fn from(value: Decimal) -> Self {
320        // Generate string representation from the decimal value
321        let original_string = Arc::from(value.to_string());
322        Self {
323            value: Some(value),
324            original_string,
325        }
326    }
327}
328
329/// Implements serialization for `PreciseDecimal` preserving original format.
330///
331/// This implementation ensures that the exact original string representation
332/// is preserved during JSON serialization, maintaining FHIR's precision
333/// requirements including trailing zeros and specific formatting.
334///
335/// # FHIR Compliance
336///
337/// FHIR requires that decimal values maintain their original precision when
338/// round-tripped through JSON. This implementation uses `serde_json::RawValue`
339/// to serialize the original string directly as a JSON number.
340///
341/// # Examples
342///
343/// ```rust
344/// use helios_fhir::PreciseDecimal;
345/// use rust_decimal::Decimal;
346/// use serde_json;
347///
348/// let precise = PreciseDecimal::from_parts(
349///     Some(Decimal::new(1230, 2)),
350///     "12.30".to_string()
351/// );
352///
353/// let json = serde_json::to_string(&precise).unwrap();
354/// assert_eq!(json, "12.30"); // Preserves trailing zero
355/// ```
356impl Serialize for PreciseDecimal {
357    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
358    where
359        S: Serializer,
360    {
361        // Use RawValue to preserve exact string format in JSON
362        match serde_json::value::RawValue::from_string(self.original_string.to_string()) {
363            Ok(raw_value) => raw_value.serialize(serializer),
364            Err(e) => Err(serde::ser::Error::custom(format!(
365                "Failed to serialize PreciseDecimal '{}': {}",
366                self.original_string, e
367            ))),
368        }
369    }
370}
371
372/// Implements deserialization for `PreciseDecimal` preserving original format.
373///
374/// This implementation deserializes JSON numbers and strings into `PreciseDecimal`
375/// while preserving the exact original string representation. It handles various
376/// JSON formats including scientific notation and nested object structures.
377///
378/// # Supported Formats
379///
380/// - Direct numbers: `12.340`
381/// - String numbers: `"12.340"`
382/// - Scientific notation: `1.234e2` or `1.234E2`
383/// - Nested objects: `{"value": 12.340}` (for macro-generated structures)
384///
385/// # Examples
386///
387/// ```rust
388/// use helios_fhir::PreciseDecimal;
389/// use serde_json;
390///
391/// // Deserialize from JSON number (trailing zeros are normalized)
392/// let precise: PreciseDecimal = serde_json::from_str("12.340").unwrap();
393/// assert_eq!(precise.original_string(), "12.340"); // JSON number format
394///
395/// // Deserialize from JSON string (preserves exact format)
396/// let precise: PreciseDecimal = serde_json::from_str("\"12.340\"").unwrap();
397/// assert_eq!(precise.original_string(), "12.340"); // Preserves string format
398/// ```
399impl<'de> Deserialize<'de> for PreciseDecimal {
400    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
401    where
402        D: Deserializer<'de>,
403    {
404        // Use intermediate Value to capture exact string representation
405        let json_value = serde_json::Value::deserialize(deserializer)?;
406
407        match json_value {
408            serde_json::Value::Number(n) => {
409                // Extract string representation from JSON number
410                let original_string = n.to_string();
411                let parsed_value = Self::parse_decimal_string(&original_string);
412                Ok(PreciseDecimal::from_parts(parsed_value, original_string))
413            }
414            serde_json::Value::String(s) => {
415                // Use string value directly (preserves exact format)
416                let parsed_value = Self::parse_decimal_string(&s);
417                Ok(PreciseDecimal::from_parts(parsed_value, s))
418            }
419            // Handle nested object format (for macro-generated structures)
420            serde_json::Value::Object(map) => match map.get("value") {
421                Some(serde_json::Value::Number(n)) => {
422                    let original_string = n.to_string();
423                    let parsed_value = Self::parse_decimal_string(&original_string);
424                    Ok(PreciseDecimal::from_parts(parsed_value, original_string))
425                }
426                Some(serde_json::Value::String(s)) => {
427                    let original_string = s.clone();
428                    let parsed_value = Self::parse_decimal_string(&original_string);
429                    Ok(PreciseDecimal::from_parts(parsed_value, original_string))
430                }
431                Some(serde_json::Value::Null) => Err(de::Error::invalid_value(
432                    de::Unexpected::Unit,
433                    &"a number or string for decimal value",
434                )),
435                None => Err(de::Error::missing_field("value")),
436                _ => Err(de::Error::invalid_type(
437                    de::Unexpected::Map,
438                    &"a map with a 'value' field containing a number or string",
439                )),
440            },
441            // Handle remaining unexpected types
442            other => Err(de::Error::invalid_type(
443                match other {
444                    serde_json::Value::Null => de::Unexpected::Unit, // Or Unexpected::Option if mapping null to None
445                    serde_json::Value::Bool(b) => de::Unexpected::Bool(b),
446                    serde_json::Value::Array(_) => de::Unexpected::Seq,
447                    _ => de::Unexpected::Other("unexpected JSON type for PreciseDecimal"),
448                },
449                &"a number, string, or object with a 'value' field",
450            )),
451        }
452    }
453}
454
455// --- End PreciseDecimal ---
456
457/// Precision levels for FHIR Date values.
458///
459/// FHIR dates support partial precision, allowing year-only, year-month,
460/// or full date specifications. This enum tracks which components are present.
461#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
462pub enum DatePrecision {
463    /// Year only (YYYY)
464    Year,
465    /// Year and month (YYYY-MM)
466    YearMonth,
467    /// Full date (YYYY-MM-DD)
468    Full,
469}
470
471/// Precision levels for FHIR Time values.
472///
473/// FHIR times support partial precision from hour-only through
474/// sub-second precision. This enum tracks which components are present.
475#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
476pub enum TimePrecision {
477    /// Hour only (HH)
478    Hour,
479    /// Hour and minute (HH:MM)
480    HourMinute,
481    /// Hour, minute, and second (HH:MM:SS)
482    HourMinuteSecond,
483    /// Full time with sub-second precision (HH:MM:SS.sss)
484    Millisecond,
485}
486
487/// Precision levels for FHIR DateTime values.
488///
489/// FHIR datetimes support partial precision from year-only through
490/// sub-second precision with optional timezone information.
491#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
492pub enum DateTimePrecision {
493    /// Year only (YYYY)
494    Year,
495    /// Year and month (YYYY-MM)
496    YearMonth,
497    /// Date only (YYYY-MM-DD)
498    Date,
499    /// Date with hour (YYYY-MM-DDTHH)
500    DateHour,
501    /// Date with hour and minute (YYYY-MM-DDTHH:MM)
502    DateHourMinute,
503    /// Date with time to seconds (YYYY-MM-DDTHH:MM:SS)
504    DateHourMinuteSecond,
505    /// Full datetime with sub-second precision (YYYY-MM-DDTHH:MM:SS.sss)
506    Full,
507}
508
509impl Default for PrecisionDate {
510    fn default() -> Self {
511        // Default to epoch date 1970-01-01
512        Self::from_ymd(1970, 1, 1)
513    }
514}
515
516/// Precision-aware FHIR Date type.
517///
518/// This type preserves the original precision and string representation
519/// of FHIR date values while providing typed access to date components.
520///
521/// # FHIR Date Formats
522/// - `YYYY` - Year only
523/// - `YYYY-MM` - Year and month  
524/// - `YYYY-MM-DD` - Full date
525///
526/// # Examples
527/// ```rust
528/// use helios_fhir::{PrecisionDate, DatePrecision};
529///
530/// // Create a year-only date
531/// let year_date = PrecisionDate::from_year(2023);
532/// assert_eq!(year_date.precision(), DatePrecision::Year);
533/// assert_eq!(year_date.original_string(), "2023");
534///
535/// // Create a full date
536/// let full_date = PrecisionDate::from_ymd(2023, 3, 15);
537/// assert_eq!(full_date.precision(), DatePrecision::Full);
538/// assert_eq!(full_date.original_string(), "2023-03-15");
539/// ```
540#[derive(Debug, Clone, PartialEq, Eq)]
541pub struct PrecisionDate {
542    /// Year component (always present)
543    year: i32,
544    /// Month component (1-12, None for year-only precision)
545    month: Option<u32>,
546    /// Day component (1-31, None for year or year-month precision)
547    day: Option<u32>,
548    /// Precision level of this date
549    precision: DatePrecision,
550    /// Original string representation
551    original_string: Arc<str>,
552}
553
554impl PrecisionDate {
555    /// Creates a year-only precision date.
556    pub fn from_year(year: i32) -> Self {
557        Self {
558            year,
559            month: None,
560            day: None,
561            precision: DatePrecision::Year,
562            original_string: Arc::from(format!("{:04}", year)),
563        }
564    }
565
566    /// Creates a year-month precision date.
567    pub fn from_year_month(year: i32, month: u32) -> Self {
568        Self {
569            year,
570            month: Some(month),
571            day: None,
572            precision: DatePrecision::YearMonth,
573            original_string: Arc::from(format!("{:04}-{:02}", year, month)),
574        }
575    }
576
577    /// Creates a full precision date.
578    pub fn from_ymd(year: i32, month: u32, day: u32) -> Self {
579        Self {
580            year,
581            month: Some(month),
582            day: Some(day),
583            precision: DatePrecision::Full,
584            original_string: Arc::from(format!("{:04}-{:02}-{:02}", year, month, day)),
585        }
586    }
587
588    /// Parses a FHIR date string, preserving precision.
589    pub fn parse(s: &str) -> Option<Self> {
590        // Remove @ prefix if present
591        let s = s.strip_prefix('@').unwrap_or(s);
592
593        let parts: Vec<&str> = s.split('-').collect();
594        match parts.len() {
595            1 => {
596                // Year only
597                let year = parts[0].parse::<i32>().ok()?;
598                Some(Self {
599                    year,
600                    month: None,
601                    day: None,
602                    precision: DatePrecision::Year,
603                    original_string: Arc::from(s),
604                })
605            }
606            2 => {
607                // Year-month
608                let year = parts[0].parse::<i32>().ok()?;
609                let month = parts[1].parse::<u32>().ok()?;
610                if !(1..=12).contains(&month) {
611                    return None;
612                }
613                Some(Self {
614                    year,
615                    month: Some(month),
616                    day: None,
617                    precision: DatePrecision::YearMonth,
618                    original_string: Arc::from(s),
619                })
620            }
621            3 => {
622                // Full date
623                let year = parts[0].parse::<i32>().ok()?;
624                let month = parts[1].parse::<u32>().ok()?;
625                let day = parts[2].parse::<u32>().ok()?;
626                if !(1..=12).contains(&month) || !(1..=31).contains(&day) {
627                    return None;
628                }
629                Some(Self {
630                    year,
631                    month: Some(month),
632                    day: Some(day),
633                    precision: DatePrecision::Full,
634                    original_string: Arc::from(s),
635                })
636            }
637            _ => None,
638        }
639    }
640
641    /// Returns the precision level of this date.
642    pub fn precision(&self) -> DatePrecision {
643        self.precision
644    }
645
646    /// Returns the original string representation.
647    pub fn original_string(&self) -> &str {
648        &self.original_string
649    }
650
651    /// Returns the year component.
652    pub fn year(&self) -> i32 {
653        self.year
654    }
655
656    /// Returns the month component if present.
657    pub fn month(&self) -> Option<u32> {
658        self.month
659    }
660
661    /// Returns the day component if present.
662    pub fn day(&self) -> Option<u32> {
663        self.day
664    }
665
666    /// Converts to a NaiveDate, using defaults for missing components.
667    pub fn to_naive_date(&self) -> NaiveDate {
668        NaiveDate::from_ymd_opt(self.year, self.month.unwrap_or(1), self.day.unwrap_or(1))
669            .expect("Valid date components")
670    }
671
672    /// Compares two dates considering precision.
673    /// Returns None if comparison is indeterminate due to precision differences.
674    pub fn compare(&self, other: &Self) -> Option<Ordering> {
675        // Compare years first
676        match self.year.cmp(&other.year) {
677            Ordering::Equal => {
678                // Years are equal, check month precision
679                match (self.month, other.month) {
680                    (None, None) => Some(Ordering::Equal),
681                    (None, Some(_)) | (Some(_), None) => {
682                        // Different precisions - comparison may be indeterminate
683                        // For < and > we can still determine, but for = it's indeterminate
684                        None
685                    }
686                    (Some(m1), Some(m2)) => match m1.cmp(&m2) {
687                        Ordering::Equal => {
688                            // Months are equal, check day precision
689                            match (self.day, other.day) {
690                                (None, None) => Some(Ordering::Equal),
691                                (None, Some(_)) | (Some(_), None) => {
692                                    // Different precisions - indeterminate
693                                    None
694                                }
695                                (Some(d1), Some(d2)) => Some(d1.cmp(&d2)),
696                            }
697                        }
698                        other => Some(other),
699                    },
700                }
701            }
702            other => Some(other),
703        }
704    }
705}
706
707impl Default for PrecisionTime {
708    fn default() -> Self {
709        // Default to midnight 00:00:00
710        Self::from_hms(0, 0, 0)
711    }
712}
713
714/// Precision-aware FHIR Time type.
715///
716/// This type preserves the original precision and string representation
717/// of FHIR time values. Note that FHIR times do not support timezone information.
718///
719/// # FHIR Time Formats
720/// - `HH` - Hour only
721/// - `HH:MM` - Hour and minute
722/// - `HH:MM:SS` - Hour, minute, and second
723/// - `HH:MM:SS.sss` - Full time with milliseconds
724///
725/// # Examples
726/// ```rust
727/// use helios_fhir::{PrecisionTime, TimePrecision};
728///
729/// // Create an hour-only time
730/// let hour_time = PrecisionTime::from_hour(14);
731/// assert_eq!(hour_time.precision(), TimePrecision::Hour);
732/// assert_eq!(hour_time.original_string(), "14");
733///
734/// // Create a full precision time
735/// let full_time = PrecisionTime::from_hms_milli(14, 30, 45, 123);
736/// assert_eq!(full_time.precision(), TimePrecision::Millisecond);
737/// assert_eq!(full_time.original_string(), "14:30:45.123");
738/// ```
739#[derive(Debug, Clone, PartialEq, Eq)]
740pub struct PrecisionTime {
741    /// Hour component (0-23, always present)
742    hour: u32,
743    /// Minute component (0-59)
744    minute: Option<u32>,
745    /// Second component (0-59)
746    second: Option<u32>,
747    /// Millisecond component (0-999)
748    millisecond: Option<u32>,
749    /// Precision level of this time
750    precision: TimePrecision,
751    /// Original string representation
752    original_string: Arc<str>,
753}
754
755impl PrecisionTime {
756    /// Creates an hour-only precision time.
757    pub fn from_hour(hour: u32) -> Self {
758        Self {
759            hour,
760            minute: None,
761            second: None,
762            millisecond: None,
763            precision: TimePrecision::Hour,
764            original_string: Arc::from(format!("{:02}", hour)),
765        }
766    }
767
768    /// Creates an hour-minute precision time.
769    pub fn from_hm(hour: u32, minute: u32) -> Self {
770        Self {
771            hour,
772            minute: Some(minute),
773            second: None,
774            millisecond: None,
775            precision: TimePrecision::HourMinute,
776            original_string: Arc::from(format!("{:02}:{:02}", hour, minute)),
777        }
778    }
779
780    /// Creates an hour-minute-second precision time.
781    pub fn from_hms(hour: u32, minute: u32, second: u32) -> Self {
782        Self {
783            hour,
784            minute: Some(minute),
785            second: Some(second),
786            millisecond: None,
787            precision: TimePrecision::HourMinuteSecond,
788            original_string: Arc::from(format!("{:02}:{:02}:{:02}", hour, minute, second)),
789        }
790    }
791
792    /// Creates a full precision time with milliseconds.
793    pub fn from_hms_milli(hour: u32, minute: u32, second: u32, millisecond: u32) -> Self {
794        Self {
795            hour,
796            minute: Some(minute),
797            second: Some(second),
798            millisecond: Some(millisecond),
799            precision: TimePrecision::Millisecond,
800            original_string: Arc::from(format!(
801                "{:02}:{:02}:{:02}.{:03}",
802                hour, minute, second, millisecond
803            )),
804        }
805    }
806
807    /// Parses a FHIR time string, preserving precision.
808    pub fn parse(s: &str) -> Option<Self> {
809        // Remove @ and T prefixes if present
810        let s = s.strip_prefix('@').unwrap_or(s);
811        let s = s.strip_prefix('T').unwrap_or(s);
812
813        // Check for timezone (not allowed in FHIR time)
814        if s.contains('+') || s.contains('-') || s.ends_with('Z') {
815            return None;
816        }
817
818        let parts: Vec<&str> = s.split(':').collect();
819        match parts.len() {
820            1 => {
821                // Hour only
822                let hour = parts[0].parse::<u32>().ok()?;
823                if hour > 23 {
824                    return None;
825                }
826                Some(Self {
827                    hour,
828                    minute: None,
829                    second: None,
830                    millisecond: None,
831                    precision: TimePrecision::Hour,
832                    original_string: Arc::from(s),
833                })
834            }
835            2 => {
836                // Hour:minute
837                let hour = parts[0].parse::<u32>().ok()?;
838                let minute = parts[1].parse::<u32>().ok()?;
839                if hour > 23 || minute > 59 {
840                    return None;
841                }
842                Some(Self {
843                    hour,
844                    minute: Some(minute),
845                    second: None,
846                    millisecond: None,
847                    precision: TimePrecision::HourMinute,
848                    original_string: Arc::from(s),
849                })
850            }
851            3 => {
852                // Hour:minute:second[.millisecond]
853                let hour = parts[0].parse::<u32>().ok()?;
854                let minute = parts[1].parse::<u32>().ok()?;
855
856                // Check for milliseconds
857                let (second, millisecond, precision) = if parts[2].contains('.') {
858                    let sec_parts: Vec<&str> = parts[2].split('.').collect();
859                    if sec_parts.len() != 2 {
860                        return None;
861                    }
862                    let second = sec_parts[0].parse::<u32>().ok()?;
863                    // Parse milliseconds, padding or truncating as needed
864                    let ms_str = sec_parts[1];
865                    let ms = if ms_str.len() <= 3 {
866                        // Pad with zeros if needed
867                        let padded = format!("{:0<3}", ms_str);
868                        padded.parse::<u32>().ok()?
869                    } else {
870                        // Truncate to 3 digits
871                        ms_str[..3].parse::<u32>().ok()?
872                    };
873                    (second, Some(ms), TimePrecision::Millisecond)
874                } else {
875                    let second = parts[2].parse::<u32>().ok()?;
876                    (second, None, TimePrecision::HourMinuteSecond)
877                };
878
879                if hour > 23 || minute > 59 || second > 59 {
880                    return None;
881                }
882
883                Some(Self {
884                    hour,
885                    minute: Some(minute),
886                    second: Some(second),
887                    millisecond,
888                    precision,
889                    original_string: Arc::from(s),
890                })
891            }
892            _ => None,
893        }
894    }
895
896    /// Returns the precision level of this time.
897    pub fn precision(&self) -> TimePrecision {
898        self.precision
899    }
900
901    /// Returns the original string representation.
902    pub fn original_string(&self) -> &str {
903        &self.original_string
904    }
905
906    /// Converts to a NaiveTime, using defaults for missing components.
907    pub fn to_naive_time(&self) -> NaiveTime {
908        let milli = self.millisecond.unwrap_or(0);
909        let micro = milli * 1000; // Convert milliseconds to microseconds
910        NaiveTime::from_hms_micro_opt(
911            self.hour,
912            self.minute.unwrap_or(0),
913            self.second.unwrap_or(0),
914            micro,
915        )
916        .expect("Valid time components")
917    }
918
919    /// Compares two times considering precision.
920    /// Per FHIRPath spec: seconds and milliseconds are considered the same precision level
921    pub fn compare(&self, other: &Self) -> Option<Ordering> {
922        match self.hour.cmp(&other.hour) {
923            Ordering::Equal => {
924                match (self.minute, other.minute) {
925                    (None, None) => Some(Ordering::Equal),
926                    (None, Some(_)) | (Some(_), None) => None,
927                    (Some(m1), Some(m2)) => match m1.cmp(&m2) {
928                        Ordering::Equal => {
929                            match (self.second, other.second) {
930                                (None, None) => Some(Ordering::Equal),
931                                (None, Some(_)) | (Some(_), None) => None,
932                                (Some(s1), Some(s2)) => {
933                                    // Per FHIRPath spec: second and millisecond precisions are
934                                    // considered a single precision using decimal comparison
935                                    let ms1 = self.millisecond.unwrap_or(0);
936                                    let ms2 = other.millisecond.unwrap_or(0);
937                                    let total1 = s1 * 1000 + ms1;
938                                    let total2 = s2 * 1000 + ms2;
939                                    Some(total1.cmp(&total2))
940                                }
941                            }
942                        }
943                        other => Some(other),
944                    },
945                }
946            }
947            other => Some(other),
948        }
949    }
950}
951
952impl Default for PrecisionDateTime {
953    fn default() -> Self {
954        // Default to Unix epoch 1970-01-01T00:00:00
955        Self::from_date(1970, 1, 1)
956    }
957}
958
959/// Precision-aware FHIR DateTime type.
960///
961/// This type preserves the original precision and string representation
962/// of FHIR datetime values, including timezone information when present.
963///
964/// # FHIR DateTime Formats
965/// - `YYYY` - Year only
966/// - `YYYY-MM` - Year and month
967/// - `YYYY-MM-DD` - Date only
968/// - `YYYY-MM-DDTHH` - Date with hour
969/// - `YYYY-MM-DDTHH:MM` - Date with hour and minute
970/// - `YYYY-MM-DDTHH:MM:SS` - Date with time to seconds
971/// - `YYYY-MM-DDTHH:MM:SS.sss` - Full datetime with milliseconds
972/// - All time formats can include timezone: `Z`, `+HH:MM`, `-HH:MM`
973///
974/// # Examples
975/// ```rust
976/// use helios_fhir::{PrecisionDateTime, DateTimePrecision};
977///
978/// // Create a date-only datetime
979/// let date_dt = PrecisionDateTime::from_date(2023, 3, 15);
980/// assert_eq!(date_dt.precision(), DateTimePrecision::Date);
981/// assert_eq!(date_dt.original_string(), "2023-03-15");
982///
983/// // Create a full datetime with timezone
984/// let full_dt = PrecisionDateTime::parse("2023-03-15T14:30:45.123Z").unwrap();
985/// assert_eq!(full_dt.precision(), DateTimePrecision::Full);
986/// ```
987#[derive(Debug, Clone, PartialEq, Eq)]
988pub struct PrecisionDateTime {
989    /// Date components
990    pub date: PrecisionDate,
991    /// Time components (if precision includes time)
992    time: Option<PrecisionTime>,
993    /// Timezone offset in minutes from UTC (None means local/unspecified)
994    timezone_offset: Option<i32>,
995    /// Precision level of this datetime
996    precision: DateTimePrecision,
997    /// Original string representation
998    original_string: Arc<str>,
999}
1000
1001impl PrecisionDateTime {
1002    /// Creates a year-only datetime.
1003    pub fn from_year(year: i32) -> Self {
1004        let date = PrecisionDate::from_year(year);
1005        Self {
1006            original_string: date.original_string.clone(),
1007            date,
1008            time: None,
1009            timezone_offset: None,
1010            precision: DateTimePrecision::Year,
1011        }
1012    }
1013
1014    /// Creates a year-month datetime.
1015    pub fn from_year_month(year: i32, month: u32) -> Self {
1016        let date = PrecisionDate::from_year_month(year, month);
1017        Self {
1018            original_string: date.original_string.clone(),
1019            date,
1020            time: None,
1021            timezone_offset: None,
1022            precision: DateTimePrecision::YearMonth,
1023        }
1024    }
1025
1026    /// Creates a date-only datetime.
1027    pub fn from_date(year: i32, month: u32, day: u32) -> Self {
1028        let date = PrecisionDate::from_ymd(year, month, day);
1029        Self {
1030            original_string: date.original_string.clone(),
1031            date,
1032            time: None,
1033            timezone_offset: None,
1034            precision: DateTimePrecision::Date,
1035        }
1036    }
1037
1038    /// Parses a FHIR datetime string, preserving precision and timezone.
1039    pub fn parse(s: &str) -> Option<Self> {
1040        // Remove @ prefix if present
1041        let s = s.strip_prefix('@').unwrap_or(s);
1042
1043        // Check for 'T' separator to determine if time is present
1044        if let Some(t_pos) = s.find('T') {
1045            let date_part = &s[..t_pos];
1046            let time_and_tz = &s[t_pos + 1..];
1047
1048            // Parse date part
1049            let date = PrecisionDate::parse(date_part)?;
1050
1051            // Check for timezone at the end
1052            let (time_part, timezone_offset) = if let Some(stripped) = time_and_tz.strip_suffix('Z')
1053            {
1054                (stripped, Some(0))
1055            } else if let Some(plus_pos) = time_and_tz.rfind('+') {
1056                let tz_str = &time_and_tz[plus_pos + 1..];
1057                let offset = Self::parse_timezone_offset(tz_str)?;
1058                (&time_and_tz[..plus_pos], Some(offset))
1059            } else if let Some(minus_pos) = time_and_tz.rfind('-') {
1060                // Be careful not to confuse negative timezone with date separator
1061                if minus_pos > 0 && time_and_tz[..minus_pos].contains(':') {
1062                    let tz_str = &time_and_tz[minus_pos + 1..];
1063                    let offset = Self::parse_timezone_offset(tz_str)?;
1064                    (&time_and_tz[..minus_pos], Some(-offset))
1065                } else {
1066                    (time_and_tz, None)
1067                }
1068            } else {
1069                (time_and_tz, None)
1070            };
1071
1072            // Parse time part if not empty
1073            let (time, precision) = if time_part.is_empty() {
1074                // Just "T" with no time components (partial datetime)
1075                (
1076                    None,
1077                    match date.precision {
1078                        DatePrecision::Full => DateTimePrecision::Date,
1079                        DatePrecision::YearMonth => DateTimePrecision::YearMonth,
1080                        DatePrecision::Year => DateTimePrecision::Year,
1081                    },
1082                )
1083            } else {
1084                let time = PrecisionTime::parse(time_part)?;
1085                let precision = match time.precision {
1086                    TimePrecision::Hour => DateTimePrecision::DateHour,
1087                    TimePrecision::HourMinute => DateTimePrecision::DateHourMinute,
1088                    TimePrecision::HourMinuteSecond => DateTimePrecision::DateHourMinuteSecond,
1089                    TimePrecision::Millisecond => DateTimePrecision::Full,
1090                };
1091                (Some(time), precision)
1092            };
1093
1094            Some(Self {
1095                date,
1096                time,
1097                timezone_offset,
1098                precision,
1099                original_string: Arc::from(s),
1100            })
1101        } else {
1102            // No 'T' separator, just a date
1103            let date = PrecisionDate::parse(s)?;
1104            let precision = match date.precision {
1105                DatePrecision::Year => DateTimePrecision::Year,
1106                DatePrecision::YearMonth => DateTimePrecision::YearMonth,
1107                DatePrecision::Full => DateTimePrecision::Date,
1108            };
1109
1110            Some(Self {
1111                original_string: Arc::from(s),
1112                date,
1113                time: None,
1114                timezone_offset: None,
1115                precision,
1116            })
1117        }
1118    }
1119
1120    /// Parses a timezone offset string (e.g., "05:30") into minutes.
1121    fn parse_timezone_offset(s: &str) -> Option<i32> {
1122        let parts: Vec<&str> = s.split(':').collect();
1123        match parts.len() {
1124            1 => {
1125                // Just hours
1126                let hours = parts[0].parse::<i32>().ok()?;
1127                Some(hours * 60)
1128            }
1129            2 => {
1130                // Hours and minutes
1131                let hours = parts[0].parse::<i32>().ok()?;
1132                let minutes = parts[1].parse::<i32>().ok()?;
1133                Some(hours * 60 + minutes)
1134            }
1135            _ => None,
1136        }
1137    }
1138
1139    /// Creates a PrecisionDateTime from a PrecisionDate (for date to datetime conversion).
1140    pub fn from_precision_date(date: PrecisionDate) -> Self {
1141        let precision = match date.precision {
1142            DatePrecision::Year => DateTimePrecision::Year,
1143            DatePrecision::YearMonth => DateTimePrecision::YearMonth,
1144            DatePrecision::Full => DateTimePrecision::Date,
1145        };
1146        Self {
1147            original_string: date.original_string.clone(),
1148            date,
1149            time: None,
1150            timezone_offset: None,
1151            precision,
1152        }
1153    }
1154
1155    /// Returns the precision level of this datetime.
1156    pub fn precision(&self) -> DateTimePrecision {
1157        self.precision
1158    }
1159
1160    /// Returns the original string representation.
1161    pub fn original_string(&self) -> &str {
1162        &self.original_string
1163    }
1164
1165    /// Converts to a chrono DateTime<Utc>, using defaults for missing components.
1166    pub fn to_chrono_datetime(&self) -> ChronoDateTime<Utc> {
1167        let naive_date = self.date.to_naive_date();
1168        let naive_time = self
1169            .time
1170            .as_ref()
1171            .map(|t| t.to_naive_time())
1172            .unwrap_or_else(|| NaiveTime::from_hms_opt(0, 0, 0).unwrap());
1173
1174        let naive_dt = naive_date.and_time(naive_time);
1175
1176        // Apply timezone offset if present
1177        if let Some(offset_minutes) = self.timezone_offset {
1178            // The datetime is in local time with the given offset
1179            // We need to subtract the offset to get UTC
1180            let utc_naive = naive_dt - chrono::Duration::minutes(offset_minutes as i64);
1181            ChronoDateTime::<Utc>::from_naive_utc_and_offset(utc_naive, Utc)
1182        } else {
1183            // No timezone means we assume UTC
1184            ChronoDateTime::<Utc>::from_naive_utc_and_offset(naive_dt, Utc)
1185        }
1186    }
1187
1188    /// Compares two datetimes considering precision and timezones.
1189    pub fn compare(&self, other: &Self) -> Option<Ordering> {
1190        // Check if precisions are compatible
1191        // Per FHIRPath spec: seconds and milliseconds are the same precision
1192        let self_precision_normalized = match self.precision {
1193            DateTimePrecision::Full => DateTimePrecision::DateHourMinuteSecond,
1194            p => p,
1195        };
1196        let other_precision_normalized = match other.precision {
1197            DateTimePrecision::Full => DateTimePrecision::DateHourMinuteSecond,
1198            p => p,
1199        };
1200
1201        // If precisions don't match (except for seconds/milliseconds), return None
1202        if self_precision_normalized != other_precision_normalized {
1203            // Special handling for date vs datetime with time components
1204            if self.time.is_none() != other.time.is_none() {
1205                return None;
1206            }
1207        }
1208
1209        // If both have sufficient precision and timezone info, compare as full datetimes
1210        if self.precision >= DateTimePrecision::DateHour
1211            && other.precision >= DateTimePrecision::DateHour
1212            && self.timezone_offset.is_some()
1213            && other.timezone_offset.is_some()
1214        {
1215            // Convert to UTC and compare
1216            return Some(self.to_chrono_datetime().cmp(&other.to_chrono_datetime()));
1217        }
1218
1219        // If one has timezone and the other doesn't, comparison is indeterminate
1220        if self.timezone_offset.is_some() != other.timezone_offset.is_some() {
1221            return None;
1222        }
1223
1224        // Otherwise, compare components with precision awareness
1225        match self.date.compare(&other.date) {
1226            Some(Ordering::Equal) => {
1227                // Dates are equal at their precision level
1228                match (&self.time, &other.time) {
1229                    (None, None) => Some(Ordering::Equal),
1230                    (None, Some(_)) | (Some(_), None) => None, // Different precisions
1231                    (Some(t1), Some(t2)) => t1.compare(t2),
1232                }
1233            }
1234            other => other,
1235        }
1236    }
1237}
1238
1239// === Display Implementations for Precision Types ===
1240
1241impl std::fmt::Display for PrecisionDate {
1242    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1243        write!(f, "{}", self.original_string)
1244    }
1245}
1246
1247impl std::fmt::Display for PrecisionDateTime {
1248    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1249        write!(f, "{}", self.original_string)
1250    }
1251}
1252
1253impl std::fmt::Display for PrecisionTime {
1254    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1255        write!(f, "{}", self.original_string)
1256    }
1257}
1258
1259// === Serde Implementations for Precision Types ===
1260
1261impl Serialize for PrecisionDate {
1262    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
1263    where
1264        S: Serializer,
1265    {
1266        // Serialize as a simple string
1267        serializer.serialize_str(&self.original_string)
1268    }
1269}
1270
1271impl<'de> Deserialize<'de> for PrecisionDate {
1272    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1273    where
1274        D: Deserializer<'de>,
1275    {
1276        let s = String::deserialize(deserializer)?;
1277        PrecisionDate::parse(&s)
1278            .ok_or_else(|| de::Error::custom(format!("Invalid FHIR date format: {}", s)))
1279    }
1280}
1281
1282impl Serialize for PrecisionTime {
1283    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
1284    where
1285        S: Serializer,
1286    {
1287        // Serialize as a simple string
1288        serializer.serialize_str(&self.original_string)
1289    }
1290}
1291
1292impl<'de> Deserialize<'de> for PrecisionTime {
1293    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1294    where
1295        D: Deserializer<'de>,
1296    {
1297        let s = String::deserialize(deserializer)?;
1298        PrecisionTime::parse(&s)
1299            .ok_or_else(|| de::Error::custom(format!("Invalid FHIR time format: {}", s)))
1300    }
1301}
1302
1303impl Serialize for PrecisionDateTime {
1304    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
1305    where
1306        S: Serializer,
1307    {
1308        // Serialize as a simple string
1309        serializer.serialize_str(&self.original_string)
1310    }
1311}
1312
1313impl<'de> Deserialize<'de> for PrecisionDateTime {
1314    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1315    where
1316        D: Deserializer<'de>,
1317    {
1318        let s = String::deserialize(deserializer)?;
1319        PrecisionDateTime::parse(&s)
1320            .ok_or_else(|| de::Error::custom(format!("Invalid FHIR datetime format: {}", s)))
1321    }
1322}
1323
1324// === PrecisionInstant Implementation ===
1325
1326/// A FHIR instant value that preserves the original string representation and precision.
1327///
1328/// Instants in FHIR must be complete date-time values with timezone information,
1329/// representing a specific moment in time. This type wraps PrecisionDateTime but
1330/// enforces instant-specific constraints.
1331#[derive(Debug, Clone, PartialEq, Eq, Default)]
1332pub struct PrecisionInstant {
1333    inner: PrecisionDateTime,
1334}
1335
1336impl PrecisionInstant {
1337    /// Parses a FHIR instant string.
1338    /// Returns None if the string is not a valid instant (must have full date, time, and timezone).
1339    pub fn parse(s: &str) -> Option<Self> {
1340        // Parse as PrecisionDateTime first
1341        let dt = PrecisionDateTime::parse(s)?;
1342
1343        // For now, accept any valid datetime as an instant
1344        // In strict mode, we could require timezone, but many FHIR resources
1345        // use instant fields without explicit timezones
1346        Some(PrecisionInstant { inner: dt })
1347    }
1348
1349    /// Returns the original string representation
1350    pub fn original_string(&self) -> &str {
1351        self.inner.original_string()
1352    }
1353
1354    /// Get the inner PrecisionDateTime
1355    pub fn as_datetime(&self) -> &PrecisionDateTime {
1356        &self.inner
1357    }
1358
1359    /// Convert to chrono DateTime<Utc>
1360    pub fn to_chrono_datetime(&self) -> ChronoDateTime<Utc> {
1361        // PrecisionDateTime::to_chrono_datetime returns ChronoDateTime<Utc>
1362        self.inner.to_chrono_datetime()
1363    }
1364}
1365
1366impl fmt::Display for PrecisionInstant {
1367    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1368        write!(f, "{}", self.inner)
1369    }
1370}
1371
1372impl Serialize for PrecisionInstant {
1373    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
1374    where
1375        S: Serializer,
1376    {
1377        self.inner.serialize(serializer)
1378    }
1379}
1380
1381impl<'de> Deserialize<'de> for PrecisionInstant {
1382    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1383    where
1384        D: Deserializer<'de>,
1385    {
1386        let s = String::deserialize(deserializer)?;
1387        PrecisionInstant::parse(&s)
1388            .ok_or_else(|| de::Error::custom(format!("Invalid FHIR instant format: {}", s)))
1389    }
1390}
1391
1392// === IntoEvaluationResult Implementations for Precision Types ===
1393
1394impl IntoEvaluationResult for PrecisionDate {
1395    fn to_evaluation_result(&self) -> EvaluationResult {
1396        EvaluationResult::date(self.original_string.to_string())
1397    }
1398}
1399
1400impl IntoEvaluationResult for PrecisionTime {
1401    fn to_evaluation_result(&self) -> EvaluationResult {
1402        EvaluationResult::time(self.original_string.to_string())
1403    }
1404}
1405
1406impl IntoEvaluationResult for PrecisionDateTime {
1407    fn to_evaluation_result(&self) -> EvaluationResult {
1408        EvaluationResult::datetime(self.original_string.to_string())
1409    }
1410}
1411
1412impl IntoEvaluationResult for PrecisionInstant {
1413    fn to_evaluation_result(&self) -> EvaluationResult {
1414        // Return as datetime with instant type info
1415        EvaluationResult::DateTime(
1416            self.inner.original_string.to_string(),
1417            Some(TypeInfoResult::new("FHIR", "instant")),
1418        )
1419    }
1420}
1421
1422// Removed DecimalElementObjectVisitor
1423
1424#[cfg(feature = "R4")]
1425pub mod r4;
1426#[cfg(feature = "R4B")]
1427pub mod r4b;
1428#[cfg(feature = "R5")]
1429pub mod r5;
1430#[cfg(feature = "R6")]
1431pub mod r6;
1432
1433pub mod parameters;
1434
1435// Re-export commonly used types from parameters module
1436pub use parameters::{ParameterValueAccessor, VersionIndependentParameters};
1437
1438// Internal helpers used by the derive macro; not part of the public API
1439#[doc(hidden)]
1440/// Multi-version FHIR resource container supporting version-agnostic operations.
1441///
1442/// This enum provides a unified interface for working with FHIR resources across
1443/// different specification versions. It enables applications to handle multiple
1444/// FHIR versions simultaneously while maintaining type safety and version-specific
1445/// behavior where needed.
1446///
1447/// # Supported Versions
1448///
1449/// - **R4**: FHIR 4.0.1 (normative)
1450/// - **R4B**: FHIR 4.3.0 (ballot)  
1451/// - **R5**: FHIR 5.0.0 (ballot)
1452/// - **R6**: FHIR 6.0.0 (draft)
1453///
1454/// # Feature Flags
1455///
1456/// Each FHIR version is controlled by a corresponding Cargo feature flag.
1457/// Only enabled versions will be available in the enum variants.
1458///
1459/// # Examples
1460///
1461/// ```rust
1462/// use helios_fhir::{FhirResource, FhirVersion};
1463/// # #[cfg(feature = "R4")]
1464/// use helios_fhir::r4::{Patient, HumanName};
1465///
1466/// # #[cfg(feature = "R4")]
1467/// {
1468///     // Create an R4 patient
1469///     let patient = Patient {
1470///         name: Some(vec![HumanName {
1471///             family: Some("Doe".to_string().into()),
1472///             given: Some(vec!["John".to_string().into()]),
1473///             ..Default::default()
1474///         }]),
1475///         ..Default::default()
1476///     };
1477///
1478///     // Wrap in version-agnostic container
1479///     let resource = FhirResource::R4(Box::new(helios_fhir::r4::Resource::Patient(Box::new(patient))));
1480///     assert_eq!(resource.version(), FhirVersion::R4);
1481/// }
1482/// ```
1483///
1484/// # Version Detection
1485///
1486/// Use the `version()` method to determine which FHIR version a resource uses:
1487///
1488/// ```rust
1489/// # use helios_fhir::{FhirResource, FhirVersion};
1490/// # #[cfg(feature = "R4")]
1491/// # {
1492/// # let resource = FhirResource::R4(Box::new(helios_fhir::r4::Resource::Patient(Default::default())));
1493/// match resource.version() {
1494///     #[cfg(feature = "R4")]
1495///     FhirVersion::R4 => println!("This is an R4 resource"),
1496///     #[cfg(feature = "R4B")]
1497///     FhirVersion::R4B => println!("This is an R4B resource"),
1498///     #[cfg(feature = "R5")]
1499///     FhirVersion::R5 => println!("This is an R5 resource"),
1500///     #[cfg(feature = "R6")]
1501///     FhirVersion::R6 => println!("This is an R6 resource"),
1502/// }
1503/// # }
1504/// ```
1505#[derive(Debug)]
1506pub enum FhirResource {
1507    /// FHIR 4.0.1 (normative) resource
1508    #[cfg(feature = "R4")]
1509    R4(Box<r4::Resource>),
1510    /// FHIR 4.3.0 (ballot) resource
1511    #[cfg(feature = "R4B")]
1512    R4B(Box<r4b::Resource>),
1513    /// FHIR 5.0.0 (ballot) resource
1514    #[cfg(feature = "R5")]
1515    R5(Box<r5::Resource>),
1516    /// FHIR 6.0.0 (draft) resource
1517    #[cfg(feature = "R6")]
1518    R6(Box<r6::Resource>),
1519}
1520
1521impl FhirResource {
1522    /// Returns the FHIR specification version of this resource.
1523    ///
1524    /// This method provides version detection for multi-version applications,
1525    /// enabling version-specific processing logic and compatibility checks.
1526    ///
1527    /// # Returns
1528    ///
1529    /// The `FhirVersion` enum variant corresponding to this resource's specification.
1530    ///
1531    /// # Examples
1532    ///
1533    /// ```rust
1534    /// use helios_fhir::{FhirResource, FhirVersion};
1535    ///
1536    /// # #[cfg(feature = "R5")]
1537    /// # {
1538    /// # let resource = FhirResource::R5(Box::new(helios_fhir::r5::Resource::Patient(Default::default())));
1539    /// let version = resource.version();
1540    /// assert_eq!(version, FhirVersion::R5);
1541    ///
1542    /// // Use version for conditional logic
1543    /// match version {
1544    ///     FhirVersion::R5 => {
1545    ///         println!("Processing R5 resource with latest features");
1546    ///     },
1547    ///     FhirVersion::R4 => {
1548    ///         println!("Processing R4 resource with normative features");
1549    ///     },
1550    ///     _ => {
1551    ///         println!("Processing other FHIR version");
1552    ///     }
1553    /// }
1554    /// # }
1555    /// ```
1556    pub fn version(&self) -> FhirVersion {
1557        match self {
1558            #[cfg(feature = "R4")]
1559            FhirResource::R4(_) => FhirVersion::R4,
1560            #[cfg(feature = "R4B")]
1561            FhirResource::R4B(_) => FhirVersion::R4B,
1562            #[cfg(feature = "R5")]
1563            FhirResource::R5(_) => FhirVersion::R5,
1564            #[cfg(feature = "R6")]
1565            FhirResource::R6(_) => FhirVersion::R6,
1566        }
1567    }
1568}
1569
1570/// Enumeration of supported FHIR specification versions.
1571///
1572/// This enum represents the different versions of the FHIR (Fast Healthcare
1573/// Interoperability Resources) specification that this library supports.
1574/// Each version represents a specific release of the FHIR standard with
1575/// its own set of features, resources, and compatibility requirements.
1576///
1577/// # Version Status
1578///
1579/// - **R4** (4.0.1): Normative version, widely adopted in production
1580/// - **R4B** (4.3.0): Ballot version with additional features
1581/// - **R5** (5.0.0): Ballot version with significant enhancements
1582/// - **R6** (6.0.0): Draft version under active development
1583///
1584/// # Feature Flags
1585///
1586/// Each version is controlled by a corresponding Cargo feature flag:
1587/// - `R4`: Enables FHIR R4 support
1588/// - `R4B`: Enables FHIR R4B support  
1589/// - `R5`: Enables FHIR R5 support
1590/// - `R6`: Enables FHIR R6 support
1591///
1592/// # Examples
1593///
1594/// ```rust
1595/// use helios_fhir::FhirVersion;
1596///
1597/// // Version comparison
1598/// # #[cfg(all(feature = "R4", feature = "R5"))]
1599/// # {
1600/// assert_ne!(FhirVersion::R4, FhirVersion::R5);
1601/// # }
1602///
1603/// // String representation
1604/// # #[cfg(feature = "R4")]
1605/// # {
1606/// let version = FhirVersion::R4;
1607/// assert_eq!(version.as_str(), "R4");
1608/// assert_eq!(version.to_string(), "R4");
1609/// # }
1610/// ```
1611///
1612/// # CLI Integration
1613///
1614/// This enum implements `clap::ValueEnum` for command-line argument parsing:
1615///
1616/// ```rust,no_run
1617/// use clap::Parser;
1618/// use helios_fhir::FhirVersion;
1619///
1620/// #[derive(Parser)]
1621/// struct Args {
1622///     #[arg(value_enum)]
1623///     version: FhirVersion,
1624/// }
1625/// ```
1626#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
1627pub enum FhirVersion {
1628    /// FHIR 4.0.1 (normative) - The current normative version
1629    #[cfg(feature = "R4")]
1630    R4,
1631    /// FHIR 4.3.0 (ballot) - Intermediate version with additional features
1632    #[cfg(feature = "R4B")]
1633    R4B,
1634    /// FHIR 5.0.0 (ballot) - Next major version with significant changes
1635    #[cfg(feature = "R5")]
1636    R5,
1637    /// FHIR 6.0.0 (draft) - Future version under development
1638    #[cfg(feature = "R6")]
1639    R6,
1640}
1641
1642impl FhirVersion {
1643    /// Returns the string representation of the FHIR version.
1644    ///
1645    /// This method provides the standard version identifier as used in
1646    /// FHIR documentation, URLs, and configuration files.
1647    ///
1648    /// # Returns
1649    ///
1650    /// A static string slice representing the version (e.g., "R4", "R5").
1651    ///
1652    /// # Examples
1653    ///
1654    /// ```rust
1655    /// use helios_fhir::FhirVersion;
1656    ///
1657    /// # #[cfg(feature = "R4")]
1658    /// assert_eq!(FhirVersion::R4.as_str(), "R4");
1659    /// # #[cfg(feature = "R5")]
1660    /// assert_eq!(FhirVersion::R5.as_str(), "R5");
1661    /// ```
1662    ///
1663    /// # Usage
1664    ///
1665    /// This method is commonly used for:
1666    /// - Logging and debugging output
1667    /// - Configuration file parsing
1668    /// - API endpoint construction
1669    /// - Version-specific resource loading
1670    pub fn as_str(&self) -> &'static str {
1671        match self {
1672            #[cfg(feature = "R4")]
1673            FhirVersion::R4 => "R4",
1674            #[cfg(feature = "R4B")]
1675            FhirVersion::R4B => "R4B",
1676            #[cfg(feature = "R5")]
1677            FhirVersion::R5 => "R5",
1678            #[cfg(feature = "R6")]
1679            FhirVersion::R6 => "R6",
1680        }
1681    }
1682
1683    /// Parse from MIME-type parameter value (e.g., "4.0", "5.0").
1684    ///
1685    /// Per FHIR spec: <https://hl7.org/fhir/http.html#version-parameter>
1686    ///
1687    /// # Arguments
1688    ///
1689    /// * `value` - The MIME-type parameter value (e.g., "4.0", "4.3", "5.0", "6.0")
1690    ///
1691    /// # Returns
1692    ///
1693    /// The corresponding `FhirVersion` if the value matches an enabled version,
1694    /// or `None` if not recognized or the version feature is not enabled.
1695    ///
1696    /// # Examples
1697    ///
1698    /// ```rust
1699    /// use helios_fhir::FhirVersion;
1700    ///
1701    /// # #[cfg(feature = "R4")]
1702    /// assert_eq!(FhirVersion::from_mime_param("4.0"), Some(FhirVersion::R4));
1703    /// # #[cfg(feature = "R5")]
1704    /// assert_eq!(FhirVersion::from_mime_param("5.0"), Some(FhirVersion::R5));
1705    /// assert_eq!(FhirVersion::from_mime_param("invalid"), None);
1706    /// ```
1707    pub fn from_mime_param(value: &str) -> Option<Self> {
1708        match value.trim() {
1709            #[cfg(feature = "R4")]
1710            "4.0" => Some(FhirVersion::R4),
1711            #[cfg(feature = "R4B")]
1712            "4.3" => Some(FhirVersion::R4B),
1713            #[cfg(feature = "R5")]
1714            "5.0" => Some(FhirVersion::R5),
1715            #[cfg(feature = "R6")]
1716            "6.0" => Some(FhirVersion::R6),
1717            _ => None,
1718        }
1719    }
1720
1721    /// Returns the MIME-type parameter value for this version.
1722    ///
1723    /// This value is used in Content-Type and Accept headers per FHIR spec.
1724    /// Example: `application/fhir+json; fhirVersion=4.0`
1725    ///
1726    /// # Examples
1727    ///
1728    /// ```rust
1729    /// use helios_fhir::FhirVersion;
1730    ///
1731    /// # #[cfg(feature = "R4")]
1732    /// assert_eq!(FhirVersion::R4.as_mime_param(), "4.0");
1733    /// # #[cfg(feature = "R5")]
1734    /// assert_eq!(FhirVersion::R5.as_mime_param(), "5.0");
1735    /// ```
1736    pub fn as_mime_param(&self) -> &'static str {
1737        match self {
1738            #[cfg(feature = "R4")]
1739            FhirVersion::R4 => "4.0",
1740            #[cfg(feature = "R4B")]
1741            FhirVersion::R4B => "4.3",
1742            #[cfg(feature = "R5")]
1743            FhirVersion::R5 => "5.0",
1744            #[cfg(feature = "R6")]
1745            FhirVersion::R6 => "6.0",
1746        }
1747    }
1748
1749    /// Returns the full version string (e.g., "4.0.1", "5.0.0").
1750    ///
1751    /// This is the complete version identifier used in CapabilityStatement.fhirVersion.
1752    ///
1753    /// # Examples
1754    ///
1755    /// ```rust
1756    /// use helios_fhir::FhirVersion;
1757    ///
1758    /// # #[cfg(feature = "R4")]
1759    /// assert_eq!(FhirVersion::R4.full_version(), "4.0.1");
1760    /// # #[cfg(feature = "R5")]
1761    /// assert_eq!(FhirVersion::R5.full_version(), "5.0.0");
1762    /// ```
1763    pub fn full_version(&self) -> &'static str {
1764        match self {
1765            #[cfg(feature = "R4")]
1766            FhirVersion::R4 => "4.0.1",
1767            #[cfg(feature = "R4B")]
1768            FhirVersion::R4B => "4.3.0",
1769            #[cfg(feature = "R5")]
1770            FhirVersion::R5 => "5.0.0",
1771            #[cfg(feature = "R6")]
1772            FhirVersion::R6 => "6.0.0",
1773        }
1774    }
1775
1776    /// Parse from database storage string.
1777    ///
1778    /// Accepts both MIME format ("4.0") and short format ("R4") for flexibility.
1779    /// This is useful when loading version information from the database.
1780    ///
1781    /// # Arguments
1782    ///
1783    /// * `value` - The storage value (e.g., "4.0", "R4", "r4")
1784    ///
1785    /// # Returns
1786    ///
1787    /// The corresponding `FhirVersion` if recognized, or `None` otherwise.
1788    ///
1789    /// # Examples
1790    ///
1791    /// ```rust
1792    /// use helios_fhir::FhirVersion;
1793    ///
1794    /// # #[cfg(feature = "R4")]
1795    /// {
1796    /// assert_eq!(FhirVersion::from_storage("4.0"), Some(FhirVersion::R4));
1797    /// assert_eq!(FhirVersion::from_storage("R4"), Some(FhirVersion::R4));
1798    /// assert_eq!(FhirVersion::from_storage("r4"), Some(FhirVersion::R4));
1799    /// }
1800    /// ```
1801    pub fn from_storage(value: &str) -> Option<Self> {
1802        // Try MIME format first
1803        Self::from_mime_param(value).or_else(|| match value.to_uppercase().as_str() {
1804            #[cfg(feature = "R4")]
1805            "R4" => Some(FhirVersion::R4),
1806            #[cfg(feature = "R4B")]
1807            "R4B" => Some(FhirVersion::R4B),
1808            #[cfg(feature = "R5")]
1809            "R5" => Some(FhirVersion::R5),
1810            #[cfg(feature = "R6")]
1811            "R6" => Some(FhirVersion::R6),
1812            _ => None,
1813        })
1814    }
1815
1816    /// Returns all enabled FHIR versions.
1817    ///
1818    /// This is useful for listing supported versions (e.g., in `$versions` operation).
1819    pub fn enabled_versions() -> &'static [FhirVersion] {
1820        &[
1821            #[cfg(feature = "R4")]
1822            FhirVersion::R4,
1823            #[cfg(feature = "R4B")]
1824            FhirVersion::R4B,
1825            #[cfg(feature = "R5")]
1826            FhirVersion::R5,
1827            #[cfg(feature = "R6")]
1828            FhirVersion::R6,
1829        ]
1830    }
1831}
1832
1833/// Implements `Display` trait for user-friendly output formatting.
1834///
1835/// This enables `FhirVersion` to be used in string formatting operations
1836/// and provides consistent output across different contexts.
1837///
1838/// # Examples
1839///
1840/// ```rust
1841/// use helios_fhir::FhirVersion;
1842///
1843/// # #[cfg(feature = "R5")]
1844/// # {
1845/// let version = FhirVersion::R5;
1846/// println!("Using FHIR version: {}", version); // Prints: "Using FHIR version: R5"
1847///
1848/// let formatted = format!("fhir-{}.json", version);
1849/// assert_eq!(formatted, "fhir-R5.json");
1850/// # }
1851/// ```
1852impl std::fmt::Display for FhirVersion {
1853    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1854        write!(f, "{}", self.as_str())
1855    }
1856}
1857
1858/// Provides a default FHIR version when R4 feature is enabled.
1859///
1860/// R4 is chosen as the default because it is the current normative version
1861/// of the FHIR specification and is widely adopted in production systems.
1862///
1863/// # Examples
1864///
1865/// ```rust
1866/// use helios_fhir::FhirVersion;
1867///
1868/// # #[cfg(feature = "R4")]
1869/// # {
1870/// let default_version = FhirVersion::default();
1871/// assert_eq!(default_version, FhirVersion::R4);
1872/// # }
1873/// ```
1874#[cfg(feature = "R4")]
1875impl Default for FhirVersion {
1876    fn default() -> Self {
1877        FhirVersion::R4
1878    }
1879}
1880
1881/// Implements `clap::ValueEnum` for command-line argument parsing.
1882///
1883/// This implementation enables `FhirVersion` to be used directly as a command-line
1884/// argument type with clap, providing automatic parsing, validation, and help text
1885/// generation.
1886///
1887/// # Examples
1888///
1889/// ```rust,no_run
1890/// use clap::Parser;
1891/// use helios_fhir::FhirVersion;
1892///
1893/// #[derive(Parser)]
1894/// struct Args {
1895///     /// FHIR specification version to use
1896///     #[arg(value_enum, default_value_t = FhirVersion::default())]
1897///     version: FhirVersion,
1898/// }
1899///
1900/// // Command line: my-app --version R5
1901/// let args = Args::parse();
1902/// println!("Using FHIR version: {}", args.version);
1903/// ```
1904///
1905/// # Generated Help Text
1906///
1907/// When using this enum with clap, the help text will automatically include
1908/// all available FHIR versions based on enabled feature flags.
1909impl clap::ValueEnum for FhirVersion {
1910    fn value_variants<'a>() -> &'a [Self] {
1911        &[
1912            #[cfg(feature = "R4")]
1913            FhirVersion::R4,
1914            #[cfg(feature = "R4B")]
1915            FhirVersion::R4B,
1916            #[cfg(feature = "R5")]
1917            FhirVersion::R5,
1918            #[cfg(feature = "R6")]
1919            FhirVersion::R6,
1920        ]
1921    }
1922
1923    fn to_possible_value(&self) -> Option<clap::builder::PossibleValue> {
1924        Some(clap::builder::PossibleValue::new(self.as_str()))
1925    }
1926}
1927
1928/// Trait for providing FHIR resource type information
1929///
1930/// This trait allows querying which resource types are available in a specific
1931/// FHIR version without hardcoding resource type lists in multiple places.
1932pub trait FhirResourceTypeProvider {
1933    /// Returns a vector of all resource type names supported in this FHIR version
1934    fn get_resource_type_names() -> Vec<&'static str>;
1935
1936    /// Checks if a given type name is a resource type in this FHIR version
1937    fn is_resource_type(type_name: &str) -> bool {
1938        Self::get_resource_type_names()
1939            .iter()
1940            .any(|&resource_type| resource_type.eq_ignore_ascii_case(type_name))
1941    }
1942}
1943
1944/// Trait for providing FHIR complex type information
1945///
1946/// This trait allows querying which complex data types are available in a specific
1947/// FHIR version without hardcoding complex type lists in multiple places.
1948pub trait FhirComplexTypeProvider {
1949    /// Returns a vector of all complex type names supported in this FHIR version
1950    fn get_complex_type_names() -> Vec<&'static str>;
1951
1952    /// Checks if a given type name is a complex type in this FHIR version
1953    fn is_complex_type(type_name: &str) -> bool {
1954        Self::get_complex_type_names()
1955            .iter()
1956            .any(|&complex_type| complex_type.eq_ignore_ascii_case(type_name))
1957    }
1958}
1959
1960// --- Internal Visitor for Element Object Deserialization ---
1961
1962/// Internal visitor struct for deserializing Element objects from JSON maps.
1963///
1964/// This visitor handles the complex deserialization logic for Element<V, E> when
1965/// the JSON input is an object containing id, extension, and value fields.
1966struct ElementObjectVisitor<V, E>(PhantomData<(V, E)>);
1967
1968impl<'de, V, E> Visitor<'de> for ElementObjectVisitor<V, E>
1969where
1970    V: Deserialize<'de>,
1971    E: Deserialize<'de>,
1972{
1973    type Value = Element<V, E>;
1974
1975    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
1976        formatter.write_str("an Element object")
1977    }
1978
1979    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
1980    where
1981        A: MapAccess<'de>,
1982    {
1983        let mut id: Option<String> = None;
1984        let mut extension: Option<Vec<E>> = None;
1985        let mut value: Option<V> = None;
1986
1987        // Manually deserialize fields from the map
1988        while let Some(key) = map.next_key::<String>()? {
1989            match key.as_str() {
1990                "id" => {
1991                    if id.is_some() {
1992                        return Err(de::Error::duplicate_field("id"));
1993                    }
1994                    id = Some(map.next_value()?);
1995                }
1996                "extension" => {
1997                    if extension.is_some() {
1998                        return Err(de::Error::duplicate_field("extension"));
1999                    }
2000                    #[cfg(feature = "xml")]
2001                    {
2002                        let single_or_vec: SingleOrVec<E> = map.next_value()?;
2003                        extension = Some(single_or_vec.into());
2004                    }
2005                    #[cfg(not(feature = "xml"))]
2006                    {
2007                        extension = Some(map.next_value()?);
2008                    }
2009                }
2010                "value" => {
2011                    if value.is_some() {
2012                        return Err(de::Error::duplicate_field("value"));
2013                    }
2014                    // Deserialize directly into Option<V>
2015                    value = Some(map.next_value()?);
2016                }
2017                // Ignore any unknown fields encountered
2018                _ => {
2019                    let _ = map.next_value::<de::IgnoredAny>()?;
2020                }
2021            }
2022        }
2023
2024        Ok(Element {
2025            id,
2026            extension,
2027            value,
2028        })
2029    }
2030}
2031
2032/// Generic element container supporting FHIR's extension mechanism.
2033///
2034/// In FHIR, most primitive elements can be extended with additional metadata
2035/// through the `id` and `extension` fields. This container type provides
2036/// the infrastructure to support this pattern across all FHIR data types.
2037///
2038/// # Type Parameters
2039///
2040/// * `V` - The value type (e.g., `String`, `i32`, `PreciseDecimal`)
2041/// * `E` - The extension type (typically the generated `Extension` struct)
2042///
2043/// # FHIR Element Structure
2044///
2045/// FHIR elements can appear in three forms:
2046/// 1. **Primitive value**: Just the value itself (e.g., `"text"`, `42`)
2047/// 2. **Extended primitive**: An object with `value`, `id`, and/or `extension` fields
2048/// 3. **Extension-only**: An object with just `id` and/or `extension` (no value)
2049///
2050/// # Examples
2051///
2052/// ```rust
2053/// use helios_fhir::{Element, r4::Extension};
2054///
2055/// // Simple primitive value
2056/// let simple: Element<String, Extension> = Element {
2057///     value: Some("Hello World".to_string()),
2058///     id: None,
2059///     extension: None,
2060/// };
2061///
2062/// // Extended primitive with ID
2063/// let with_id: Element<String, Extension> = Element {
2064///     value: Some("Hello World".to_string()),
2065///     id: Some("text-element-1".to_string()),
2066///     extension: None,
2067/// };
2068///
2069/// // Extension-only element (no value)
2070/// let extension_only: Element<String, Extension> = Element {
2071///     value: None,
2072///     id: Some("disabled-element".to_string()),
2073///     extension: Some(vec![/* extensions */]),
2074/// };
2075/// ```
2076///
2077/// # Serialization Behavior
2078///
2079/// - If only `value` is present: serializes as the primitive value directly
2080/// - If `id` or `extension` are present: serializes as an object with all fields
2081/// - If everything is `None`: serializes as `null`
2082#[derive(Debug, PartialEq, Eq, Clone, Default)]
2083pub struct Element<V, E> {
2084    /// Optional element identifier for referencing within the resource
2085    pub id: Option<String>,
2086    /// Optional extensions providing additional metadata
2087    pub extension: Option<Vec<E>>,
2088    /// The actual primitive value
2089    pub value: Option<V>,
2090}
2091
2092impl<V, E> Element<V, E> {
2093    /// Returns true when no value, id, or extensions are present.
2094    pub fn is_empty(&self) -> bool {
2095        self.value.is_none()
2096            && self.id.is_none()
2097            && self.extension.as_ref().is_none_or(|ext| ext.is_empty())
2098    }
2099}
2100
2101// Custom Deserialize for Element<V, E>
2102// Remove PartialEq/Eq bounds for V and E as they are not needed for deserialization itself
2103impl<'de, V, E> Deserialize<'de> for Element<V, E>
2104where
2105    V: Deserialize<'de> + 'static, // Added 'static for TypeId comparisons
2106    E: Deserialize<'de>,           // Removed PartialEq
2107{
2108    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
2109    where
2110        D: Deserializer<'de>,
2111    {
2112        // Use the AnyValueVisitor approach to handle different JSON input types
2113        struct AnyValueVisitor<V, E>(PhantomData<(V, E)>);
2114
2115        impl<'de, V, E> Visitor<'de> for AnyValueVisitor<V, E>
2116        where
2117            V: Deserialize<'de> + 'static,
2118            E: Deserialize<'de>,
2119        {
2120            type Value = Element<V, E>;
2121
2122            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
2123                formatter
2124                    .write_str("a primitive value (string, number, boolean), an object, or null")
2125            }
2126
2127            // Handle primitive types by attempting to deserialize V and wrapping it
2128            fn visit_bool<Er>(self, v: bool) -> Result<Self::Value, Er>
2129            where
2130                Er: de::Error,
2131            {
2132                V::deserialize(de::value::BoolDeserializer::new(v)).map(|value| Element {
2133                    id: None,
2134                    extension: None,
2135                    value: Some(value),
2136                })
2137            }
2138            fn visit_i64<Er>(self, v: i64) -> Result<Self::Value, Er>
2139            where
2140                Er: de::Error,
2141            {
2142                V::deserialize(de::value::I64Deserializer::new(v)).map(|value| Element {
2143                    id: None,
2144                    extension: None,
2145                    value: Some(value),
2146                })
2147            }
2148            fn visit_u64<Er>(self, v: u64) -> Result<Self::Value, Er>
2149            where
2150                Er: de::Error,
2151            {
2152                V::deserialize(de::value::U64Deserializer::new(v)).map(|value| Element {
2153                    id: None,
2154                    extension: None,
2155                    value: Some(value),
2156                })
2157            }
2158            fn visit_f64<Er>(self, v: f64) -> Result<Self::Value, Er>
2159            where
2160                Er: de::Error,
2161            {
2162                V::deserialize(de::value::F64Deserializer::new(v)).map(|value| Element {
2163                    id: None,
2164                    extension: None,
2165                    value: Some(value),
2166                })
2167            }
2168            fn visit_str<Er>(self, v: &str) -> Result<Self::Value, Er>
2169            where
2170                Er: de::Error,
2171            {
2172                use std::any::TypeId;
2173
2174                // Try to handle numeric strings for integer types
2175                if TypeId::of::<V>() == TypeId::of::<i64>() {
2176                    if let Ok(int_val) = v.parse::<i64>() {
2177                        return V::deserialize(de::value::I64Deserializer::new(int_val)).map(
2178                            |value| Element {
2179                                id: None,
2180                                extension: None,
2181                                value: Some(value),
2182                            },
2183                        );
2184                    }
2185                } else if TypeId::of::<V>() == TypeId::of::<i32>() {
2186                    if let Ok(int_val) = v.parse::<i32>() {
2187                        return V::deserialize(de::value::I32Deserializer::new(int_val)).map(
2188                            |value| Element {
2189                                id: None,
2190                                extension: None,
2191                                value: Some(value),
2192                            },
2193                        );
2194                    }
2195                } else if TypeId::of::<V>() == TypeId::of::<u64>() {
2196                    if let Ok(int_val) = v.parse::<u64>() {
2197                        return V::deserialize(de::value::U64Deserializer::new(int_val)).map(
2198                            |value| Element {
2199                                id: None,
2200                                extension: None,
2201                                value: Some(value),
2202                            },
2203                        );
2204                    }
2205                } else if TypeId::of::<V>() == TypeId::of::<u32>() {
2206                    if let Ok(int_val) = v.parse::<u32>() {
2207                        return V::deserialize(de::value::U32Deserializer::new(int_val)).map(
2208                            |value| Element {
2209                                id: None,
2210                                extension: None,
2211                                value: Some(value),
2212                            },
2213                        );
2214                    }
2215                }
2216
2217                // Fall back to normal string deserialization
2218                V::deserialize(de::value::StrDeserializer::new(v)).map(|value| Element {
2219                    id: None,
2220                    extension: None,
2221                    value: Some(value),
2222                })
2223            }
2224            fn visit_string<Er>(self, v: String) -> Result<Self::Value, Er>
2225            where
2226                Er: de::Error,
2227            {
2228                use std::any::TypeId;
2229
2230                // Try to handle numeric strings for integer types
2231                if TypeId::of::<V>() == TypeId::of::<i64>() {
2232                    if let Ok(int_val) = v.parse::<i64>() {
2233                        return V::deserialize(de::value::I64Deserializer::new(int_val)).map(
2234                            |value| Element {
2235                                id: None,
2236                                extension: None,
2237                                value: Some(value),
2238                            },
2239                        );
2240                    }
2241                } else if TypeId::of::<V>() == TypeId::of::<i32>() {
2242                    if let Ok(int_val) = v.parse::<i32>() {
2243                        return V::deserialize(de::value::I32Deserializer::new(int_val)).map(
2244                            |value| Element {
2245                                id: None,
2246                                extension: None,
2247                                value: Some(value),
2248                            },
2249                        );
2250                    }
2251                } else if TypeId::of::<V>() == TypeId::of::<u64>() {
2252                    if let Ok(int_val) = v.parse::<u64>() {
2253                        return V::deserialize(de::value::U64Deserializer::new(int_val)).map(
2254                            |value| Element {
2255                                id: None,
2256                                extension: None,
2257                                value: Some(value),
2258                            },
2259                        );
2260                    }
2261                } else if TypeId::of::<V>() == TypeId::of::<u32>() {
2262                    if let Ok(int_val) = v.parse::<u32>() {
2263                        return V::deserialize(de::value::U32Deserializer::new(int_val)).map(
2264                            |value| Element {
2265                                id: None,
2266                                extension: None,
2267                                value: Some(value),
2268                            },
2269                        );
2270                    }
2271                }
2272
2273                // Fall back to normal string deserialization
2274                V::deserialize(de::value::StringDeserializer::new(v.clone())).map(|value| Element {
2275                    // Clone v for error message
2276                    id: None,
2277                    extension: None,
2278                    value: Some(value),
2279                })
2280            }
2281            fn visit_borrowed_str<Er>(self, v: &'de str) -> Result<Self::Value, Er>
2282            where
2283                Er: de::Error,
2284            {
2285                use std::any::TypeId;
2286
2287                // Try to handle numeric strings for integer types
2288                if TypeId::of::<V>() == TypeId::of::<i64>() {
2289                    if let Ok(int_val) = v.parse::<i64>() {
2290                        return V::deserialize(de::value::I64Deserializer::new(int_val)).map(
2291                            |value| Element {
2292                                id: None,
2293                                extension: None,
2294                                value: Some(value),
2295                            },
2296                        );
2297                    }
2298                } else if TypeId::of::<V>() == TypeId::of::<i32>() {
2299                    if let Ok(int_val) = v.parse::<i32>() {
2300                        return V::deserialize(de::value::I32Deserializer::new(int_val)).map(
2301                            |value| Element {
2302                                id: None,
2303                                extension: None,
2304                                value: Some(value),
2305                            },
2306                        );
2307                    }
2308                } else if TypeId::of::<V>() == TypeId::of::<u64>() {
2309                    if let Ok(int_val) = v.parse::<u64>() {
2310                        return V::deserialize(de::value::U64Deserializer::new(int_val)).map(
2311                            |value| Element {
2312                                id: None,
2313                                extension: None,
2314                                value: Some(value),
2315                            },
2316                        );
2317                    }
2318                } else if TypeId::of::<V>() == TypeId::of::<u32>() {
2319                    if let Ok(int_val) = v.parse::<u32>() {
2320                        return V::deserialize(de::value::U32Deserializer::new(int_val)).map(
2321                            |value| Element {
2322                                id: None,
2323                                extension: None,
2324                                value: Some(value),
2325                            },
2326                        );
2327                    }
2328                }
2329
2330                // Fall back to normal string deserialization
2331                V::deserialize(de::value::BorrowedStrDeserializer::new(v)).map(|value| Element {
2332                    id: None,
2333                    extension: None,
2334                    value: Some(value),
2335                })
2336            }
2337            fn visit_bytes<Er>(self, v: &[u8]) -> Result<Self::Value, Er>
2338            where
2339                Er: de::Error,
2340            {
2341                V::deserialize(de::value::BytesDeserializer::new(v)).map(|value| Element {
2342                    id: None,
2343                    extension: None,
2344                    value: Some(value),
2345                })
2346            }
2347            fn visit_byte_buf<Er>(self, v: Vec<u8>) -> Result<Self::Value, Er>
2348            where
2349                Er: de::Error,
2350            {
2351                // Use BytesDeserializer with a slice reference &v
2352                V::deserialize(de::value::BytesDeserializer::new(&v)).map(|value| Element {
2353                    id: None,
2354                    extension: None,
2355                    value: Some(value),
2356                })
2357            }
2358
2359            // Handle null
2360            fn visit_none<Er>(self) -> Result<Self::Value, Er>
2361            where
2362                Er: de::Error,
2363            {
2364                Ok(Element {
2365                    id: None,
2366                    extension: None,
2367                    value: None,
2368                })
2369            }
2370            fn visit_unit<Er>(self) -> Result<Self::Value, Er>
2371            where
2372                Er: de::Error,
2373            {
2374                Ok(Element {
2375                    id: None,
2376                    extension: None,
2377                    value: None,
2378                })
2379            }
2380
2381            // Handle Option<T> by visiting Some
2382            fn visit_some<De>(self, deserializer: De) -> Result<Self::Value, De::Error>
2383            where
2384                De: Deserializer<'de>,
2385            {
2386                // Re-dispatch to deserialize_any to handle the inner type correctly
2387                deserializer.deserialize_any(self)
2388            }
2389
2390            // Handle object
2391            fn visit_map<A>(self, map: A) -> Result<Self::Value, A::Error>
2392            where
2393                A: MapAccess<'de>,
2394            {
2395                // Deserialize the map using ElementObjectVisitor
2396                // Need to create a deserializer from the map access
2397                let map_deserializer = de::value::MapAccessDeserializer::new(map);
2398                map_deserializer.deserialize_map(ElementObjectVisitor(PhantomData))
2399            }
2400
2401            // We don't expect sequences for a single Element
2402            fn visit_seq<A>(self, _seq: A) -> Result<Self::Value, A::Error>
2403            where
2404                A: de::SeqAccess<'de>,
2405            {
2406                Err(de::Error::invalid_type(de::Unexpected::Seq, &self))
2407            }
2408        }
2409
2410        // Start deserialization using the visitor
2411        deserializer.deserialize_any(AnyValueVisitor(PhantomData))
2412    }
2413}
2414
2415// Custom Serialize for Element<V, E>
2416// Remove PartialEq/Eq bounds for V and E as they are not needed for serialization itself
2417impl<V, E> Serialize for Element<V, E>
2418where
2419    V: Serialize, // Removed PartialEq + Eq
2420    E: Serialize, // Removed PartialEq
2421{
2422    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
2423    where
2424        S: Serializer,
2425    {
2426        // If id and extension are None, serialize value directly (or null)
2427        if self.id.is_none() && self.extension.is_none() {
2428            match &self.value {
2429                Some(val) => val.serialize(serializer),
2430                None => serializer.serialize_none(),
2431            }
2432        } else {
2433            // Otherwise, serialize as an object containing id, extension, value if present
2434            let mut len = 0;
2435            if self.id.is_some() {
2436                len += 1;
2437            }
2438            if self.extension.is_some() {
2439                len += 1;
2440            }
2441            if self.value.is_some() {
2442                len += 1;
2443            }
2444
2445            let mut state = serializer.serialize_struct("Element", len)?;
2446            if let Some(id) = &self.id {
2447                state.serialize_field("id", id)?;
2448            }
2449            if let Some(extension) = &self.extension {
2450                state.serialize_field("extension", extension)?;
2451            }
2452            // Restore value serialization for direct Element serialization
2453            if let Some(value) = &self.value {
2454                state.serialize_field("value", value)?;
2455            }
2456            state.end()
2457        }
2458    }
2459}
2460
2461/// Specialized element container for FHIR decimal values with precision preservation.
2462///
2463/// This type combines the generic `Element` pattern with `PreciseDecimal` to provide
2464/// a complete solution for FHIR decimal elements that require both extension support
2465/// and precision preservation during serialization round-trips.
2466///
2467/// # Type Parameters
2468///
2469/// * `E` - The extension type (typically the generated `Extension` struct)
2470///
2471/// # FHIR Decimal Requirements
2472///
2473/// FHIR decimal elements must:
2474/// - Preserve original string precision (e.g., "12.30" vs "12.3")
2475/// - Support mathematical operations using `Decimal` arithmetic
2476/// - Handle extension metadata through `id` and `extension` fields
2477/// - Serialize back to the exact original format when possible
2478///
2479/// # Examples
2480///
2481/// ```rust
2482/// use helios_fhir::{DecimalElement, PreciseDecimal, r4::Extension};
2483/// use rust_decimal::Decimal;
2484///
2485/// // Create from a Decimal value
2486/// let decimal_elem = DecimalElement::<Extension>::new(Decimal::new(1234, 2)); // 12.34
2487///
2488/// // Create with extensions
2489/// let extended_decimal: DecimalElement<Extension> = DecimalElement {
2490///     value: Some(PreciseDecimal::from_parts(
2491///         Some(Decimal::new(12300, 3)),
2492///         "12.300".to_string()
2493///     )),
2494///     id: Some("precision-example".to_string()),
2495///     extension: Some(vec![/* extensions */]),
2496/// };
2497///
2498/// // Access the mathematical value
2499/// if let Some(precise) = &extended_decimal.value {
2500///     if let Some(decimal_val) = precise.value() {
2501///         println!("Mathematical value: {}", decimal_val);
2502///     }
2503///     println!("Original format: {}", precise.original_string());
2504/// }
2505/// ```
2506///
2507/// # Serialization Behavior
2508///
2509/// - **Value only**: Serializes as a JSON number preserving original precision
2510/// - **With extensions**: Serializes as an object with `value`, `id`, and `extension` fields
2511/// - **No value**: Serializes as an object with just the extension fields, or `null` if empty
2512///
2513/// # Integration with FHIRPath
2514///
2515/// When used with FHIRPath evaluation, `DecimalElement` returns:
2516/// - The `Decimal` value for mathematical operations
2517/// - An object representation when extension metadata is accessed
2518/// - Empty collection when the element has no value or extensions
2519#[derive(Debug, PartialEq, Eq, Clone, Default)]
2520pub struct DecimalElement<E> {
2521    /// Optional element identifier for referencing within the resource
2522    pub id: Option<String>,
2523    /// Optional extensions providing additional metadata
2524    pub extension: Option<Vec<E>>,
2525    /// The decimal value with precision preservation
2526    pub value: Option<PreciseDecimal>,
2527}
2528
2529impl<E> DecimalElement<E> {
2530    /// Creates a new `DecimalElement` with the specified decimal value.
2531    ///
2532    /// This constructor creates a simple decimal element with no extensions or ID,
2533    /// containing only the decimal value. The original string representation is
2534    /// automatically derived from the `Decimal` value's `Display` implementation.
2535    ///
2536    /// # Arguments
2537    ///
2538    /// * `value` - The `Decimal` value to store
2539    ///
2540    /// # Returns
2541    ///
2542    /// A new `DecimalElement` with the value set and `id`/`extension` as `None`.
2543    ///
2544    /// # Examples
2545    ///
2546    /// ```rust
2547    /// use helios_fhir::{DecimalElement, r4::Extension};
2548    /// use rust_decimal::Decimal;
2549    ///
2550    /// // Create a simple decimal element
2551    /// let element = DecimalElement::<Extension>::new(Decimal::new(12345, 3)); // 12.345
2552    ///
2553    /// // Verify the structure
2554    /// assert!(element.id.is_none());
2555    /// assert!(element.extension.is_none());
2556    /// assert!(element.value.is_some());
2557    ///
2558    /// // Access the decimal value
2559    /// if let Some(precise_decimal) = &element.value {
2560    ///     assert_eq!(precise_decimal.value(), Some(Decimal::new(12345, 3)));
2561    ///     assert_eq!(precise_decimal.original_string(), "12.345");
2562    /// }
2563    /// ```
2564    ///
2565    /// # Usage in FHIR Resources
2566    ///
2567    /// This method is typically used when creating FHIR elements programmatically:
2568    ///
2569    /// ```rust
2570    /// use helios_fhir::{DecimalElement, r4::{Extension, Observation}};
2571    /// use rust_decimal::Decimal;
2572    ///
2573    /// let temperature = DecimalElement::<Extension>::new(Decimal::new(3672, 2)); // 36.72
2574    ///
2575    /// // Would be used in an Observation like:
2576    /// // observation.value_quantity.value = Some(temperature);
2577    /// ```
2578    pub fn new(value: Decimal) -> Self {
2579        // Convert the Decimal to PreciseDecimal, which automatically handles
2580        // storing the original string representation via the From trait
2581        let precise_value = PreciseDecimal::from(value);
2582        Self {
2583            id: None,
2584            extension: None,
2585            value: Some(precise_value),
2586        }
2587    }
2588}
2589
2590// Custom Deserialize for DecimalElement<E> using intermediate Value
2591impl<'de, E> Deserialize<'de> for DecimalElement<E>
2592where
2593    E: Deserialize<'de> + Default,
2594{
2595    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
2596    where
2597        D: Deserializer<'de>,
2598    {
2599        // Deserialize into an intermediate serde_json::Value first
2600        let json_value = serde_json::Value::deserialize(deserializer)?;
2601
2602        match json_value {
2603            // Handle primitive JSON Number
2604            serde_json::Value::Number(n) => {
2605                // Directly parse the number string to create PreciseDecimal
2606                let s = n.to_string(); // Note: n.to_string() might normalize exponent case (e.g., 'E' -> 'e')
2607                // Replace 'E' with 'e' for parsing
2608                let s_for_parsing = s.replace('E', "e");
2609                // Use from_scientific if 'e' is present, otherwise parse
2610                let parsed_value = if s_for_parsing.contains('e') {
2611                    Decimal::from_scientific(&s_for_parsing).ok()
2612                } else {
2613                    s_for_parsing.parse::<Decimal>().ok()
2614                };
2615                // Store the ORIGINAL string `s` (as returned by n.to_string()).
2616                let pd = PreciseDecimal::from_parts(parsed_value, s);
2617                Ok(DecimalElement {
2618                    id: None,
2619                    extension: None,
2620                    value: Some(pd),
2621                })
2622            }
2623            // Handle primitive JSON String
2624            serde_json::Value::String(s) => {
2625                // Directly parse the string to create PreciseDecimal
2626                // Replace 'E' with 'e' for parsing
2627                let s_for_parsing = s.replace('E', "e");
2628                // Use from_scientific if 'e' is present, otherwise parse
2629                let parsed_value = if s_for_parsing.contains('e') {
2630                    Decimal::from_scientific(&s_for_parsing).ok()
2631                } else {
2632                    s_for_parsing.parse::<Decimal>().ok()
2633                };
2634                // Store the ORIGINAL string `s`.
2635                let pd = PreciseDecimal::from_parts(parsed_value, s); // s is owned, no clone needed
2636                Ok(DecimalElement {
2637                    id: None,
2638                    extension: None,
2639                    value: Some(pd),
2640                })
2641            }
2642            // Handle JSON object: deserialize fields individually
2643            serde_json::Value::Object(map) => {
2644                let mut id: Option<String> = None;
2645                let mut extension: Option<Vec<E>> = None;
2646                let mut value: Option<PreciseDecimal> = None;
2647
2648                for (k, v) in map {
2649                    match k.as_str() {
2650                        "id" => {
2651                            if id.is_some() {
2652                                return Err(de::Error::duplicate_field("id"));
2653                            }
2654                            // Deserialize id directly from its Value
2655                            id = Deserialize::deserialize(v).map_err(de::Error::custom)?;
2656                        }
2657                        "extension" => {
2658                            if extension.is_some() {
2659                                return Err(de::Error::duplicate_field("extension"));
2660                            }
2661                            #[cfg(feature = "xml")]
2662                            {
2663                                let single_or_vec: SingleOrVec<E> =
2664                                    Deserialize::deserialize(v).map_err(de::Error::custom)?;
2665                                extension = Some(single_or_vec.into());
2666                            }
2667                            #[cfg(not(feature = "xml"))]
2668                            {
2669                                extension =
2670                                    Deserialize::deserialize(v).map_err(de::Error::custom)?;
2671                            }
2672                        }
2673                        "value" => {
2674                            if value.is_some() {
2675                                return Err(de::Error::duplicate_field("value"));
2676                            }
2677                            // Deserialize value using PreciseDecimal::deserialize from its Value
2678                            // Handle null explicitly within the value field
2679                            if v.is_null() {
2680                                value = None;
2681                            } else {
2682                                value = Some(
2683                                    PreciseDecimal::deserialize(v).map_err(de::Error::custom)?,
2684                                );
2685                            }
2686                        }
2687                        // Ignore any unknown fields encountered
2688                        _ => {} // Simply ignore unknown fields
2689                    }
2690                }
2691                Ok(DecimalElement {
2692                    id,
2693                    extension,
2694                    value,
2695                })
2696            }
2697            // Handle JSON Null for the whole element
2698            serde_json::Value::Null => Ok(DecimalElement::default()), // Default has value: None
2699            // Handle other unexpected types
2700            other => Err(de::Error::invalid_type(
2701                match other {
2702                    serde_json::Value::Bool(b) => de::Unexpected::Bool(b),
2703                    serde_json::Value::Array(_) => de::Unexpected::Seq,
2704                    _ => de::Unexpected::Other("unexpected JSON type for DecimalElement"),
2705                },
2706                &"a decimal number, string, object, or null",
2707            )),
2708        }
2709    }
2710}
2711
2712// Reinstate custom Serialize implementation for DecimalElement
2713// Remove PartialEq bound for E
2714impl<E> Serialize for DecimalElement<E>
2715where
2716    E: Serialize, // Removed PartialEq bound for E
2717{
2718    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
2719    where
2720        S: Serializer,
2721    {
2722        // If we only have a value and no other fields, serialize just the value
2723        if self.id.is_none() && self.extension.is_none() {
2724            if let Some(value) = &self.value {
2725                // Serialize the PreciseDecimal directly, invoking its custom Serialize impl
2726                return value.serialize(serializer);
2727            } else {
2728                // If value is also None, serialize as null
2729                // based on updated test_serialize_decimal_with_no_fields
2730                return serializer.serialize_none();
2731            }
2732        }
2733
2734        // Otherwise, serialize as a struct with all present fields
2735        // Calculate the number of fields that are NOT None
2736        let mut len = 0;
2737        if self.id.is_some() {
2738            len += 1;
2739        }
2740        if self.extension.is_some() {
2741            len += 1;
2742        }
2743        if self.value.is_some() {
2744            len += 1;
2745        }
2746
2747        // Start serializing a struct with the calculated length
2748        let mut state = serializer.serialize_struct("DecimalElement", len)?;
2749
2750        // Serialize 'id' field if it's Some
2751        if let Some(id) = &self.id {
2752            state.serialize_field("id", id)?;
2753        }
2754
2755        // Serialize 'extension' field if it's Some
2756        if let Some(extension) = &self.extension {
2757            state.serialize_field("extension", extension)?;
2758        }
2759
2760        // Serialize 'value' field if it's Some
2761        if let Some(value) = &self.value {
2762            // Serialize the PreciseDecimal directly, invoking its custom Serialize impl
2763            state.serialize_field("value", value)?;
2764        }
2765
2766        // End the struct serialization
2767        state.end()
2768    }
2769}
2770
2771// For Element<V, E> - Returns Object with id, extension, value if present
2772impl<V, E> IntoEvaluationResult for Element<V, E>
2773where
2774    V: IntoEvaluationResult + Clone + 'static,
2775    E: IntoEvaluationResult + Clone,
2776{
2777    fn to_evaluation_result(&self) -> EvaluationResult {
2778        use std::any::TypeId;
2779
2780        // Prioritize returning the primitive value if it exists
2781        if let Some(v) = &self.value {
2782            let result = v.to_evaluation_result();
2783            // For primitive values, we need to preserve FHIR type information
2784            return match result {
2785                EvaluationResult::Boolean(b, _) => {
2786                    // Return FHIR boolean
2787                    EvaluationResult::fhir_boolean(b)
2788                }
2789                EvaluationResult::Integer(i, _) => {
2790                    // Return FHIR integer
2791                    EvaluationResult::fhir_integer(i)
2792                }
2793                #[cfg(not(any(feature = "R4", feature = "R4B")))]
2794                EvaluationResult::Integer64(i, _) => {
2795                    // Return FHIR integer64 (R5 and above)
2796                    EvaluationResult::fhir_integer64(i)
2797                }
2798                EvaluationResult::String(s, _) => {
2799                    // Determine the FHIR type name based on V's type
2800                    let fhir_type_name = if TypeId::of::<V>() == TypeId::of::<String>() {
2801                        // For strings, we need more context to determine the exact FHIR type
2802                        // Default to "string" but this could be date, dateTime, etc.
2803                        "string"
2804                    } else {
2805                        // Default fallback
2806                        "string"
2807                    };
2808                    EvaluationResult::fhir_string(s, fhir_type_name)
2809                }
2810                EvaluationResult::DateTime(dt, type_info) => {
2811                    // Check if V is PrecisionInstant - if so, this is an instant
2812                    if TypeId::of::<V>() == TypeId::of::<PrecisionInstant>() {
2813                        // Return as FHIR instant
2814                        EvaluationResult::DateTime(dt, Some(TypeInfoResult::new("FHIR", "instant")))
2815                    } else {
2816                        // Preserve original type info for PrecisionDateTime
2817                        EvaluationResult::DateTime(dt, type_info)
2818                    }
2819                }
2820                _ => result, // For other types, return as-is
2821            };
2822        } else if self.id.is_some() || self.extension.is_some() {
2823            // If value is None, but id or extension exist, return an Object with those
2824            let mut map = std::collections::HashMap::new();
2825            if let Some(id) = &self.id {
2826                map.insert("id".to_string(), EvaluationResult::string(id.clone()));
2827            }
2828            if let Some(ext) = &self.extension {
2829                let ext_collection: Vec<EvaluationResult> =
2830                    ext.iter().map(|e| e.to_evaluation_result()).collect();
2831                if !ext_collection.is_empty() {
2832                    map.insert(
2833                        "extension".to_string(),
2834                        EvaluationResult::collection(ext_collection),
2835                    );
2836                }
2837            }
2838            // Only return Object if map is not empty (i.e., id or extension was actually present)
2839            if !map.is_empty() {
2840                return EvaluationResult::typed_object(map, "FHIR", "Element");
2841            }
2842        }
2843
2844        // If value, id, and extension are all None, return Empty
2845        EvaluationResult::Empty
2846    }
2847}
2848
2849// For DecimalElement<E> - Returns Decimal value if present, otherwise handles id/extension
2850impl<E> IntoEvaluationResult for DecimalElement<E>
2851where
2852    E: IntoEvaluationResult + Clone,
2853{
2854    fn to_evaluation_result(&self) -> EvaluationResult {
2855        // Prioritize returning the primitive decimal value if it exists
2856        if let Some(precise_decimal) = &self.value {
2857            if let Some(decimal_val) = precise_decimal.value() {
2858                // Return FHIR decimal
2859                return EvaluationResult::fhir_decimal(decimal_val);
2860            }
2861            // If PreciseDecimal holds None for value, fall through to check id/extension
2862        }
2863
2864        // If value is None, but id or extension exist, return an Object with those
2865        if self.id.is_some() || self.extension.is_some() {
2866            let mut map = std::collections::HashMap::new();
2867            if let Some(id) = &self.id {
2868                map.insert("id".to_string(), EvaluationResult::string(id.clone()));
2869            }
2870            if let Some(ext) = &self.extension {
2871                let ext_collection: Vec<EvaluationResult> =
2872                    ext.iter().map(|e| e.to_evaluation_result()).collect();
2873                if !ext_collection.is_empty() {
2874                    map.insert(
2875                        "extension".to_string(),
2876                        EvaluationResult::collection(ext_collection),
2877                    );
2878                }
2879            }
2880            // Only return Object if map is not empty
2881            if !map.is_empty() {
2882                return EvaluationResult::typed_object(map, "FHIR", "decimal");
2883            }
2884        }
2885
2886        // If value, id, and extension are all None, return Empty
2887        EvaluationResult::Empty
2888    }
2889}
2890
2891// Implement the trait for the top-level enum
2892impl IntoEvaluationResult for FhirResource {
2893    fn to_evaluation_result(&self) -> EvaluationResult {
2894        match self {
2895            #[cfg(feature = "R4")]
2896            FhirResource::R4(r) => (*r).to_evaluation_result(), // Call impl on inner Box<r4::Resource>
2897            #[cfg(feature = "R4B")]
2898            FhirResource::R4B(r) => (*r).to_evaluation_result(), // Call impl on inner Box<r4b::Resource>
2899            #[cfg(feature = "R5")]
2900            FhirResource::R5(r) => (*r).to_evaluation_result(), // Call impl on inner Box<r5::Resource>
2901            #[cfg(feature = "R6")]
2902            FhirResource::R6(r) => (*r).to_evaluation_result(), // Call impl on inner Box<r6::Resource>
2903                                                                // Note: If no features are enabled, this match might be empty or non-exhaustive.
2904                                                                // This is generally okay as the enum itself wouldn't be usable.
2905        }
2906    }
2907}
2908
2909#[cfg(test)]
2910mod tests {
2911    use super::*;
2912
2913    #[test]
2914    fn test_integer_string_deserialization() {
2915        // Test deserializing a string "2" into Element<i64, ()>
2916        type TestElement = Element<i64, ()>;
2917
2918        // Test case 1: String containing integer
2919        let json_str = r#""2""#;
2920        let result: Result<TestElement, _> = serde_json::from_str(json_str);
2921        assert!(
2922            result.is_ok(),
2923            "Failed to deserialize string '2' as i64: {:?}",
2924            result.err()
2925        );
2926
2927        let element = result.unwrap();
2928        assert_eq!(element.value, Some(2i64));
2929        assert_eq!(element.id, None);
2930        assert_eq!(element.extension, None);
2931
2932        // Test case 2: Number
2933        let json_num = r#"2"#;
2934        let result: Result<TestElement, _> = serde_json::from_str(json_num);
2935        assert!(
2936            result.is_ok(),
2937            "Failed to deserialize number 2 as i64: {:?}",
2938            result.err()
2939        );
2940
2941        let element = result.unwrap();
2942        assert_eq!(element.value, Some(2i64));
2943    }
2944
2945    #[test]
2946    fn test_i32_string_deserialization() {
2947        type TestElement = Element<i32, ()>;
2948
2949        let json_str = r#""123""#;
2950        let result: Result<TestElement, _> = serde_json::from_str(json_str);
2951        assert!(result.is_ok());
2952
2953        let element = result.unwrap();
2954        assert_eq!(element.value, Some(123i32));
2955    }
2956
2957    #[test]
2958    fn test_invalid_string_fallback() {
2959        type TestElement = Element<i64, ()>;
2960
2961        // Non-numeric string should fail for integer type
2962        let json_str = r#""not_a_number""#;
2963        let result: Result<TestElement, _> = serde_json::from_str(json_str);
2964        assert!(
2965            result.is_err(),
2966            "Should fail to deserialize non-numeric string as i64"
2967        );
2968    }
2969}