hl7_parser/
display.rs

1use crate::message::{Component, Field, Repeat, Separators, Subcomponent};
2use std::fmt::Display;
3
4/// A display implementation for segments.
5/// This will decode the escape sequences in the segment value
6/// using the separators. If the `#` flag is used, the raw value
7/// will be displayed without decoding the escape sequences.
8#[derive(Debug, Copy, Clone, PartialEq, Eq)]
9pub struct SegmentDisplay<'m> {
10    pub(crate) name: &'m str,
11    pub(crate) fields: &'m Vec<Field<'m>>,
12    pub(crate) separators: &'m Separators,
13}
14
15impl Display for SegmentDisplay<'_> {
16    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17        write!(f, "{}", self.name)?;
18        for field in self.fields {
19            write!(f, "{}", self.separators.field)?;
20            if f.alternate() {
21                write!(f, "{:#}", field.display(self.separators))?;
22            } else {
23                write!(f, "{}", field.display(self.separators))?;
24            }
25        }
26        Ok(())
27    }
28}
29
30#[derive(Debug, Copy, Clone, PartialEq, Eq)]
31pub struct FieldDisplay<'m> {
32    pub(crate) repeats: &'m Vec<Repeat<'m>>,
33    pub(crate) separators: &'m Separators,
34}
35
36impl Display for FieldDisplay<'_> {
37    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38        let mut first: bool = true;
39        for repeat in self.repeats {
40            if first {
41                first = false;
42            } else {
43                write!(f, "{}", self.separators.repetition)?;
44            }
45            if f.alternate() {
46                write!(f, "{:#}", repeat.display(self.separators))?;
47            } else {
48                write!(f, "{}", repeat.display(self.separators))?;
49            }
50        }
51        Ok(())
52    }
53}
54
55#[derive(Debug, Copy, Clone, PartialEq, Eq)]
56pub struct RepeatDisplay<'m> {
57    pub(crate) components: &'m Vec<Component<'m>>,
58    pub(crate) separators: &'m Separators,
59}
60
61impl Display for RepeatDisplay<'_> {
62    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63        let mut first: bool = true;
64        for component in self.components {
65            if first {
66                first = false;
67            } else {
68                write!(f, "{}", self.separators.repetition)?;
69            }
70            if f.alternate() {
71                write!(f, "{:#}", component.display(self.separators))?;
72            } else {
73                write!(f, "{}", component.display(self.separators))?;
74            }
75        }
76        Ok(())
77    }
78}
79
80/// A display implementation for components.
81/// This will decode the escape sequences in the component values
82/// using the separators. If the `#` flag is used, the raw value
83/// will be displayed without decoding the escape sequences.
84#[derive(Debug, Copy, Clone, PartialEq, Eq)]
85pub struct ComponentDisplay<'m> {
86    pub(crate) subcomponents: &'m Vec<Subcomponent<'m>>,
87    pub(crate) separators: &'m Separators,
88}
89
90impl Display for ComponentDisplay<'_> {
91    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
92        //     write!(f, "{}", self.separators.decode(self.value))
93        let mut first: bool = true;
94        for subcomponent in self.subcomponents {
95            if first {
96                first = false;
97            } else {
98                write!(f, "{}", self.separators.subcomponent)?;
99            }
100            if f.alternate() {
101                write!(f, "{}", subcomponent.value)?;
102            } else {
103                write!(f, "{}", subcomponent.display(self.separators))?;
104            }
105        }
106        Ok(())
107    }
108}
109
110/// A display implementation for subcomponents.
111/// This will decode the escape sequences in the subcomponent value
112/// using the separators. If the `#` flag is used, the raw value
113/// will be displayed without decoding the escape sequences.
114#[derive(Debug, Copy, Clone, PartialEq, Eq)]
115pub struct SubcomponentDisplay<'m> {
116    pub(crate) value: &'m str,
117    pub(crate) separators: &'m Separators,
118}
119
120impl Display for SubcomponentDisplay<'_> {
121    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
122        if f.alternate() {
123            write!(f, "{}", self.value)
124        } else {
125            write!(f, "{}", self.separators.decode(self.value))
126        }
127    }
128}