hl7_net/sub_component.rs
1use crate::encoding::HL7Encoding;
2
3/// The smallest data unit in an HL7 message: a subcomponent within a component.
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub struct SubComponent {
6 raw: String,
7}
8
9impl SubComponent {
10 /// Creates a subcomponent from its raw (still-encoded) string value.
11 pub fn new(raw: impl Into<String>) -> Self {
12 Self { raw: raw.into() }
13 }
14
15 /// The raw, still-encoded value (the .NET `UndecodedValue`), or `None` when
16 /// the value is the "present but null" marker.
17 pub fn raw(&self, enc: &HL7Encoding) -> Option<&str> {
18 if self.raw == enc.present_but_null {
19 None
20 } else {
21 Some(&self.raw)
22 }
23 }
24
25 /// The decoded value, or `None` when the value is "present but null".
26 pub fn value(&self, enc: &HL7Encoding) -> Option<String> {
27 if self.raw == enc.present_but_null {
28 None
29 } else {
30 Some(enc.decode(&self.raw))
31 }
32 }
33
34 /// Replaces the raw value. The supplied string is stored as-is and escaped on
35 /// serialization (matching the .NET `Value` setter on a leaf element).
36 pub fn set_value(&mut self, value: impl Into<String>) {
37 self.raw = value.into();
38 }
39}