1use crate::encoding::HL7Encoding;
2use crate::error::Hl7Error;
3use crate::sub_component::SubComponent;
4
5#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct Component {
9 raw: String,
10 pub sub_components: Vec<SubComponent>,
12 pub is_subcomponentized: bool,
14}
15
16impl Component {
17 pub fn parse(raw: &str, enc: &HL7Encoding) -> Self {
19 let sub_components: Vec<SubComponent> = raw
20 .split(enc.subcomponent_delimiter)
21 .map(SubComponent::new)
22 .collect();
23 let is_subcomponentized = sub_components.len() > 1;
24
25 Self { raw: raw.to_string(), sub_components, is_subcomponentized }
26 }
27
28 pub(crate) fn single(raw: &str) -> Self {
31 Self {
32 raw: raw.to_string(),
33 sub_components: vec![SubComponent::new(raw)],
34 is_subcomponentized: false,
35 }
36 }
37
38 pub fn raw(&self, enc: &HL7Encoding) -> Option<&str> {
40 if self.raw == enc.present_but_null {
41 None
42 } else {
43 Some(&self.raw)
44 }
45 }
46
47 pub fn value(&self, enc: &HL7Encoding) -> Option<String> {
49 if self.raw == enc.present_but_null {
50 None
51 } else {
52 Some(enc.decode(&self.raw))
53 }
54 }
55
56 pub fn set_value(&mut self, value: &str, enc: &HL7Encoding) {
58 *self = Component::parse(value, enc);
59 }
60
61 pub fn sub_component(&self, position: usize) -> Result<&SubComponent, Hl7Error> {
63 position
64 .checked_sub(1)
65 .and_then(|i| self.sub_components.get(i))
66 .ok_or_else(|| Hl7Error::new("SubComponent not available"))
67 }
68}