1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use crate::display::FieldDisplay;
use super::Repeat;
use std::ops::Range;
/// A field in an HL7 message. A field is a collection of repeats, separated by the repeat
/// separator character. Fields are separated by the field separator character.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Field<'m> {
pub(crate) source: &'m str,
pub repeats: Vec<Repeat<'m>>,
pub range: Range<usize>,
}
impl<'m> Field<'m> {
pub(crate) fn new_single(source: &'m str, range: Range<usize>) -> Self {
Field {
source,
repeats: vec![Repeat::new_single(source, range.clone())],
range,
}
}
#[inline]
/// An iterator over the repeats of the field
pub fn repeats(&self) -> impl Iterator<Item = &Repeat<'m>> {
self.repeats.iter()
}
#[inline]
/// Display the field value, using the separators to decode escape sequences
/// by default. Note: if you want to display the raw value without decoding escape
/// sequences, use the `#` flag, e.g. `format!("{:#}", field.display(separators))`.
/// Repeats will be separated by the repeat separator character.
/// Fields will be separated by the field separator character.
/// Components will be separated by the component separator character.
/// Subcomponents will be separated by the subcomponent separator character.
pub fn display(&'m self, separators: &'m super::Separators) -> FieldDisplay<'m> {
FieldDisplay {
repeats: &self.repeats,
separators,
}
}
#[inline]
/// Get the raw value of the field. This is the value as it appears in the message,
/// without any decoding of escape sequences, and including all repeats and
/// their separators.
///
/// # Examples
///
/// ```
/// let field = hl7_parser::parser::parse_field("foo~bar").unwrap();
/// assert_eq!(field.repeats.len(), 2);
/// assert_eq!(field.raw_value(), "foo~bar");
/// ```
pub fn raw_value(&self) -> &'m str {
self.source
}
#[inline]
/// Returns true if the field has more than one repeat. Note that
/// if the field has only one repeat, the value of that repeat
/// is essentially the value of the field, so the value of the field
/// can be obtained using `raw_value()`.
///
/// # Examples
///
/// ```
/// let field = hl7_parser::parser::parse_field("foo~bar").unwrap();
/// assert_eq!(field.has_repeats(), true);
/// let field = hl7_parser::parser::parse_field("foo").unwrap();
/// assert_eq!(field.has_repeats(), false);
/// let field = hl7_parser::parser::parse_field("foo^bar").unwrap();
/// assert_eq!(field.has_repeats(), false);
/// ```
pub fn has_repeats(&self) -> bool {
self.repeats.len() > 1
}
/// Returns true if the field has no repeats, or if all repeats
/// have empty values.
///
/// # Examples
///
/// ```
/// let field = hl7_parser::parser::parse_field("foo~bar").unwrap();
/// assert_eq!(field.is_empty(), false);
/// let field = hl7_parser::parser::parse_field("").unwrap();
/// assert_eq!(field.is_empty(), true);
/// let field = hl7_parser::parser::parse_field("~").unwrap();
/// assert_eq!(field.is_empty(), true);
/// ```
pub fn is_empty(&self) -> bool {
self.repeats.is_empty() || self.repeats.iter().all(|r| r.is_empty())
}
/// Get the repeat at the specified 1-based index
/// Returns None if the index is out of bounds
///
/// # Examples
///
/// ```
/// let field = hl7_parser::parser::parse_field("foo~bar").unwrap();
/// assert_eq!(field.repeat(1).unwrap().raw_value(), "foo");
/// assert_eq!(field.repeat(2).unwrap().raw_value(), "bar");
/// assert_eq!(field.repeat(3), None);
/// ```
pub fn repeat(&self, number: usize) -> Option<&Repeat<'m>> {
debug_assert!(number > 0, "Repeat numbers are 1-indexed");
self.repeats.get(number - 1)
}
/// Get the component at the specified 1-based index
/// Returns None if the index is out of bounds
/// If the field has multiple repeats, the component will be taken from the first repeat
/// only.
/// If the field has no repeats, this will return None.
/// If the field has one or more repeats, this is equivalent to calling
/// `repeat(1).component(number)`.
///
/// This is a convenience method for the common case where a field has only one repeat.
///
/// # Examples
///
/// ```
/// let field = hl7_parser::parser::parse_field("foo^bar~baz^qux").unwrap();
/// assert_eq!(field.component(1).unwrap().raw_value(), "foo");
/// assert_eq!(field.component(2).unwrap().raw_value(), "bar");
/// assert_eq!(field.component(3), None);
/// ```
pub fn component(&self, number: usize) -> Option<&super::Component<'m>> {
debug_assert!(number > 0, "Component numbers are 1-indexed");
self.repeats.first().and_then(|r| r.component(number))
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::message::{Component, Separators, Subcomponent};
#[test]
fn fields_can_display() {
let separators = Separators::default();
let repeat = Repeat {
source: r"foo\F\bar",
components: vec![Component {
source: r"foo\F\bar",
subcomponents: vec![Subcomponent {
value: r"foo\F\bar",
range: 0..1, // ignore
}],
range: 0..1, // ignore
}],
range: 0..1, // ignore
};
let field = Field {
source: r"foo\F\bar~foo\F\bar",
repeats: vec![repeat.clone(), repeat],
range: 0..1, // ignore
};
assert_eq!(format!("{}", field.display(&separators)), "foo|bar~foo|bar");
assert_eq!(
format!("{:#}", field.display(&separators)),
r"foo\F\bar~foo\F\bar"
);
}
}