Skip to main content

Builder

Struct Builder 

Source
pub struct Builder { /* private fields */ }
Expand description

Assemble a message segment by segment; see the module documentation.

use hl7_2::{Builder, Version};

let message = Builder::new(Version::V2_5)
    .message_type("ADT", "A01")
    .control_id("MSG00001")
    .timestamp("20240101093851")
    .sending("HIS", "HOSPITAL")
    .receiving("EPIC", "CLINIC")
    .segment("EVN")
    .set("EVN-1", "A01")
    .segment("PID")
    .set("PID-3.1", "241900")
    .set("PID-5.1.1", "SMITH")
    .set("PID-5.2", "JOHN")
    .build()?;

assert_eq!(message.structure_id(), "ADT_A01");
assert!(message.to_er7().contains("PID|||241900||SMITH^JOHN"));

Implementations§

Source§

impl Builder

Source

pub fn new(version: Version) -> Builder

Start a message for version, with the standard delimiters (|^~\&), a processing ID of P (production), and MSH-12 set.

Everything else — message type, control ID, timestamp, sender, receiver — is empty until set, and MSH-9 and MSH-10 being empty is exactly what Message::validate reports, so a half-built message says so rather than looking finished.

§Panics

Never in practice: the bundled dictionaries are embedded at compile time and parsed on first use, so a failure here would mean this crate shipped a malformed one.

Source

pub fn with_dictionary(version: Version, dictionary: Arc<Dictionary>) -> Builder

Start a message for version, read through dictionary — the schema-mode counterpart of Builder::new.

§Panics

Never in practice: the bundled dictionaries are embedded at compile time and parsed on first use, so a failure here would mean this crate shipped a malformed one.

Source

pub fn from_message(message: Message) -> Builder

Start from an existing message, to add to it or answer it.

Source

pub fn message_type(self, code: &str, trigger: &str) -> Builder

Set MSH-9: the message code and trigger event, e.g. ("ADT", "A01"). The structure ID (MSH-9.3) is filled in from the dictionary, so ADT^A04 correctly declares ADT_A01.

Source

pub fn control_id(self, id: &str) -> Builder

Set MSH-10, the message control ID: the sender’s own identifier for this message, which the receiver echoes in its acknowledgement.

Source

pub fn timestamp(self, timestamp: &str) -> Builder

Set MSH-7, the date and time the message was sent, as HL7 writes it (YYYYMMDDHHMMSS, optionally with a fraction and an offset).

Source

pub fn sending(self, application: &str, facility: &str) -> Builder

Set MSH-3 and MSH-4, the sending application and facility.

Source

pub fn receiving(self, application: &str, facility: &str) -> Builder

Set MSH-5 and MSH-6, the receiving application and facility.

Source

pub fn processing_id(self, id: &str) -> Builder

Set MSH-11, the processing ID: P production, T training, D debugging. A builder starts at P.

Source

pub fn segment(self, name: &str) -> Builder

Append an empty segment. Subsequent Builder::set calls that name this segment address this occurrence, because paths without an explicit [n] mean the first — so add a segment, fill it, then add the next.

Source

pub fn set(self, path: &str, value: &str) -> Builder

Set a value, escaping delimiters in it; see Message::set.

Source

pub fn set_er7(self, path: &str, er7_text: &str) -> Builder

Set a value from text that is already ER7-encoded; see Message::set_er7.

Source

pub fn encode(self, value: &impl ToHl7) -> Builder

Write a crate::ToHl7 value’s fields into the message being built — struct mode’s other direction.

Source

pub fn build(self) -> Result<Message, Error>

Finish, returning the message, or the first error a step hit.

§Errors

The first Error recorded while building — a path that could not be written, reported here rather than at the call that caused it, so a chain of setters reads as one expression.

Source

pub fn build_valid(self) -> Result<Message, Error>

Finish, and reject a message that does not pass validation. Use it for an outbound message, where sending something malformed costs more than noticing here.

§Errors

As Builder::build, and additionally when the built message does not validate against its dictionary.

Trait Implementations§

Source§

impl Debug for Builder

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> 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, 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.