hl7-net 0.1.0

Lightweight HL7 V2 parser/writer, ported from the Efferent HL7-V2 .NET library
Documentation
use crate::encoding::HL7Encoding;

/// The smallest data unit in an HL7 message: a subcomponent within a component.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SubComponent {
    raw: String,
}

impl SubComponent {
    /// Creates a subcomponent from its raw (still-encoded) string value.
    pub fn new(raw: impl Into<String>) -> Self {
        Self { raw: raw.into() }
    }

    /// The raw, still-encoded value (the .NET `UndecodedValue`), or `None` when
    /// the value is the "present but null" marker.
    pub fn raw(&self, enc: &HL7Encoding) -> Option<&str> {
        if self.raw == enc.present_but_null {
            None
        } else {
            Some(&self.raw)
        }
    }

    /// The decoded value, or `None` when the value is "present but null".
    pub fn value(&self, enc: &HL7Encoding) -> Option<String> {
        if self.raw == enc.present_but_null {
            None
        } else {
            Some(enc.decode(&self.raw))
        }
    }

    /// Replaces the raw value. The supplied string is stored as-is and escaped on
    /// serialization (matching the .NET `Value` setter on a leaf element).
    pub fn set_value(&mut self, value: impl Into<String>) {
        self.raw = value.into();
    }
}