hl7_parser/message/
segment.rs

1use crate::display::SegmentDisplay;
2use std::ops::Range;
3
4use super::{Field, Separators};
5
6/// A segment in an HL7 message. A segment is a collection of fields, separated by the field
7/// separator character. Each segment has a name, which is the first field in the segment.
8/// Segments are separated by the segment separator character.
9#[derive(Debug, Clone, PartialEq, Eq)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize))]
11pub struct Segment<'m> {
12    pub(crate) source: &'m str,
13    /// The name of the segment
14    pub name: &'m str,
15    /// The fields of the segment
16    pub fields: Vec<Field<'m>>,
17    /// The range of the segment in the original message
18    pub range: Range<usize>,
19}
20
21impl<'m> Segment<'m> {
22    #[inline]
23    /// An iterator over the fields of the segment
24    pub fn fields(&self) -> impl Iterator<Item = &Field<'m>> {
25        self.fields.iter()
26    }
27
28    #[inline]
29    /// Display the segment value, using the separators to decode escape sequences
30    /// by default. Note: if you want to display the raw value without decoding escape
31    /// sequences, use the `#` flag, e.g. `format!("{:#}", segment.display(separators))`.
32    /// Repeats will be separated by the repeat separator character.
33    /// Fields will be separated by the field separator character.
34    /// Components will be separated by the component separator character.
35    /// Subcomponents will be separated by the subcomponent separator character.
36    /// Segments will be separated by the segment separator character.
37    /// Escape sequences will be decoded using the escape character.
38    pub fn display(&'m self, separators: &'m Separators) -> SegmentDisplay<'m> {
39        SegmentDisplay {
40            name: self.name,
41            fields: &self.fields,
42            separators,
43        }
44    }
45
46    #[inline]
47    /// Get the raw value of the segment. This is the value as it appears in the message,
48    /// without any decoding of escape sequences, and including all fields and
49    /// their separators.
50    ///
51    /// # Examples
52    ///
53    /// ```
54    /// let segment = hl7_parser::parser::parse_segment("ZFO|foo|bar").unwrap();
55    /// assert_eq!(segment.name, "ZFO");
56    /// assert_eq!(segment.fields.len(), 2);
57    /// assert_eq!(segment.raw_value(), "ZFO|foo|bar");
58    /// ```
59    pub fn raw_value(&self) -> &'m str {
60        self.source
61    }
62
63    #[inline]
64    /// Get a specific field of the segment by number. Fields are numbered starting at 1.
65    /// Returns `None` if the field number is out of range.
66    ///
67    /// # Examples
68    ///
69    /// ```
70    /// let segment = hl7_parser::parser::parse_segment("ZFO|foo|bar").unwrap();
71    /// assert_eq!(segment.field(1).unwrap().raw_value(), "foo");
72    /// assert_eq!(segment.field(2).unwrap().raw_value(), "bar");
73    /// assert_eq!(segment.field(3), None);
74    /// ```
75    pub fn field(&self, number: usize) -> Option<&Field<'m>> {
76        debug_assert!(number > 0, "Field numbers are 1-indexed");
77        self.fields.get(number - 1)
78    }
79}