Struct Message

Source
pub struct Message<'m> {
    pub segments: Vec<Segment<'m>>,
    pub separators: Separators,
    /* private fields */
}
Expand description

A parsed HL7 message. This is the top-level structure that you get when you parse a message. It contains the segments of the message, as well as the separators used in the message.

§Examples

let message = hl7_parser::Message::parse("MSH|^~\\&|").unwrap();
let msh = message.segment("MSH").unwrap();
assert_eq!(msh.name, "MSH");

Fields§

§segments: Vec<Segment<'m>>§separators: Separators

Implementations§

Source§

impl<'m> Message<'m>

Source

pub fn parse(input: &'m str) -> Result<Self, ParseError>

Parse a message from a string. This will return an error if the message is not a valid HL7 message.

§Examples
let message = hl7_parser::Message::parse("MSH|^~\\&|EPIC|EPICADT|SMS|SMSADT|199912271408|CHARRIS|ADT^A04|1817457|D|2.5|\rEVN|A04|199912271408|||CHARRIS\rPID||0493575^^^2^ID 1|454721||DOE^JOHN^^^^|DOE^JOHN^^^^|19480203|M||B|254 MYSTREET AVE^^MYTOWN^OH^44123^USA||(216)123-4567|||M|NON|400003403~1129086|\rNK1||ROE^MARIE^^^^|SPO||(216)123-4567||EC|||||||||||||||||||||||||||\rPV1||O|168 ~219~C~PMA^^^^^^^^^||||277^ALLEN MYLASTNAME^BONNIE^^^^|||||||||| ||2688684|||||||||||||||||||||||||199912271408||||||002376853").unwrap();
let msh = message.segment("MSH").unwrap();
assert_eq!(msh.field(4).unwrap().raw_value(), "EPICADT");
let pid = message.segment("PID").unwrap();
let patient_name = pid.field(5).unwrap();
assert_eq!(patient_name.raw_value(), "DOE^JOHN^^^^");
let first_name = patient_name.component(2).unwrap();
let last_name = patient_name.component(1).unwrap();
assert_eq!(first_name.raw_value(), "JOHN");
assert_eq!(last_name.raw_value(), "DOE");
Source

pub fn parse_with_lenient_newlines( input: &'m str, lenient_newlines: bool, ) -> Result<Self, ParseError>

Parse a message from a string, allowing lenient newlines. This will return an error if the message is not a valid HL7 message. If lenient_newlines is true, this will allow \n and \r\n to be treated the same as \r as the separator for segments. This is useful for parsing messages that come as standard text files where each segment is separated by platform-specific newlines.

§Examples
let message = hl7_parser::Message::parse_with_lenient_newlines("MSH|^~\\&|EPIC|EPICADT|SMS|SMSADT|199912271408|CHARRIS|ADT^A04|1817457|D|2.5|\nEVN|A04|199912271408|||CHARRIS\nPID||0493575^^^2^ID 1|454721||DOE^JOHN^^^^|DOE^JOHN^^^^|19480203|M||B|254 MYSTREET AVE^^MYTOWN^OH^44123^USA||(216)123-4567|||M|NON|400003403~1129086|\nNK1||ROE^MARIE^^^^|SPO||(216)123-4567||EC|||||||||||||||||||||||||||\nPV1||O|168 ~219~C~PMA^^^^^^^^^||||277^ALLEN MYLASTNAME^BONNIE^^^^|||||||||| ||2688684|||||||||||||||||||||||||199912271408||||||002376853", true).unwrap();
let msh = message.segment("MSH").unwrap();
assert_eq!(msh.field(4).unwrap().raw_value(), "EPICADT");
let pid = message.segment("PID").unwrap();
let patient_name = pid.field(5).unwrap();
assert_eq!(patient_name.raw_value(), "DOE^JOHN^^^^");
let first_name = patient_name.component(2).unwrap();
let last_name = patient_name.component(1).unwrap();
assert_eq!(first_name.raw_value(), "JOHN");
assert_eq!(last_name.raw_value(), "DOE");
Source

pub fn segment(&self, name: &str) -> Option<&Segment<'m>>

Find a segment with the given name. If there are more than one segments with this name, return the first one.

§Examples
let message = hl7_parser::Message::parse("MSH|^~\\&|EPIC|EPICADT|SMS|SMSADT|199912271408|CHARRIS|ADT^A04|1817457|D|2.5|\rEVN|A04|199912271408|||CHARRIS\rPID||0493575^^^2^ID 1|454721||DOE^JOHN^^^^|DOE^JOHN^^^^|19480203|M||B|254 MYSTREET AVE^^MYTOWN^OH^44123^USA||(216)123-4567|||M|NON|400003403~1129086|\rNK1||ROE^MARIE^^^^|SPO||(216)123-4567||EC|||||||||||||||||||||||||||\rPV1||O|168 ~219~C~PMA^^^^^^^^^||||277^ALLEN MYLASTNAME^BONNIE^^^^|||||||||| ||2688684|||||||||||||||||||||||||199912271408||||||002376853").unwrap();
let msh = message.segment("MSH").unwrap();
assert_eq!(msh.name, "MSH");
assert_eq!(msh.field(4).unwrap().raw_value(), "EPICADT");
let pid = message.segment("PID").unwrap();
assert_eq!(pid.field(2).unwrap().raw_value(), "0493575^^^2^ID 1");
Source

pub fn segment_n(&self, name: &str, n: usize) -> Option<&Segment<'m>>

Find the nth segment with the given name. If there are fewer than n segments with this name, return None. Segments are 1-indexed.

§Examples
let message =
hl7_parser::Message::parse("MSH|^~\\&|\rABC|foo\rXYZ|bar\rABC|baz").unwrap();
let abc1 = message.segment_n("ABC", 1).unwrap();
assert_eq!(abc1.field(1).unwrap().raw_value(), "foo");
let abc2 = message.segment_n("ABC", 2).unwrap();
assert_eq!(abc2.field(1).unwrap().raw_value(), "baz");
let abc3 = message.segment_n("ABC", 3);
assert_eq!(abc3, None);
Source

pub fn segment_count(&self, name: &str) -> usize

Count the number of segments with the given name.

Source

pub fn segments(&self) -> impl Iterator<Item = &Segment<'m>>

An iterator over the segments of the message

Source

pub fn raw_value(&self) -> &'m str

Get the raw value of the message. This is the value as it appears in the message, without any decoding of escape sequences, and including all segments and their separators. This is the same as the input string that was used to parse the message.

Source

pub fn locate_cursor(&self, cursor: usize) -> Option<LocatedCursor<'_>>

Locate the cursor within the message. Equivalent to calling hl7_parser::locate::locate_cursor with the message and the cursor position.

Source

pub fn query<Q>(&'m self, query: Q) -> Option<LocationQueryResult<'m>>

Query the message for a specific location. This is a more flexible way to access the fields, components, and subcomponents of the message.

§Examples
let message =
hl7_parser::Message::parse("MSH|^~\\&|foo|bar|baz|quux|20010504094523||ADT^A01|1234|P|2.3|||").unwrap();
let field = message.query("MSH.3").unwrap().raw_value();
assert_eq!(field, "foo");
let component = message.query("MSH.7.1").unwrap().raw_value();
assert_eq!(component, "20010504094523");

Trait Implementations§

Source§

impl<'m> Clone for Message<'m>

Source§

fn clone(&self) -> Message<'m>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'m> Debug for Message<'m>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'m> From<&'m Message<'m>> for MessageBuilder

Convert a message into a message builder.

Source§

fn from(message: &'m Message<'_>) -> Self

Converts to this type from the input type.
Source§

impl<'m> PartialEq for Message<'m>

Source§

fn eq(&self, other: &Message<'m>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'m> Serialize for Message<'m>

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<'m> Eq for Message<'m>

Source§

impl<'m> StructuralPartialEq for Message<'m>

Auto Trait Implementations§

§

impl<'m> Freeze for Message<'m>

§

impl<'m> RefUnwindSafe for Message<'m>

§

impl<'m> Send for Message<'m>

§

impl<'m> Sync for Message<'m>

§

impl<'m> Unpin for Message<'m>

§

impl<'m> UnwindSafe for Message<'m>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.