Skip to main content

Segment

Struct Segment 

Source
pub struct Segment(pub Segment);
Expand description

A Serde-enabled er7::Segment.

Unlike the levels below it, a segment carries two different kinds of information — its name and its fields — so it serializes as an object with two named fields, "name" and "fields", the same shape serde’s own manual-implementation guide walks through for a struct with named fields.

Example:

use serde_er7::Segment;

let message = er7::parse("MSH|^~\\&|LAB\rPID|1||9|4|SMITH^JOHN")?;
let pid = message.segment("PID").unwrap().clone();

let json = serde_json::to_string(&Segment(pid))?;
assert!(json.starts_with(r#"{"name":"PID","fields":"#));

let back: Segment = serde_json::from_str(&json)?;
assert_eq!(back.to_er7(&message.separators), "PID|1||9|4|SMITH^JOHN");

Tuple Fields§

§0: Segment

Methods from Deref<Target = Segment>§

Source

pub fn field(&self, n: usize) -> Option<&Field>

The 1-based field n, if the segment carried one.

For a header segment this follows HL7’s own numbering, so msh.field(1) is the field separator and msh.field(9) is the message type (R8, spec §4.4.2).

Example:

let message = er7::parse("MSH|^~\\&|LAB|ACME|||||ADT^A08|MSG1|P|2.5")?;
let msh = message.header().unwrap();
let separators = &message.separators;

assert_eq!(msh.field(1).unwrap().to_er7(separators), "|");      // MSH-1
assert_eq!(msh.field(2).unwrap().to_er7(separators), r"^~\&");  // MSH-2
assert_eq!(msh.field(3).unwrap().to_er7(separators), "LAB");
assert_eq!(msh.field(9).unwrap().to_er7(separators), "ADT^A08");
assert!(msh.field(99).is_none());
Source

pub fn field_mut(&mut self, n: usize) -> Option<&mut Field>

Mutable access to the 1-based field n.

Source

pub fn component(&self, field: usize, component: usize) -> Option<&Component>

The 1-based component component of the 1-based field field, taking the first repetition — the shape most lookups want.

Example:

let message = er7::parse("MSH|^~\\&|LAB\rPID|1||9|4|SMITH^JOHN")?;
let pid = message.segment("PID").unwrap();

assert_eq!(pid.component(5, 2).unwrap().to_text(&message.separators), "JOHN");
Source

pub fn is_header(&self) -> bool

True when this segment declares the message’s delimiters, i.e. it is an MSH header or one of the FHS/BHS batch headers. Fields 1 and 2 of such a segment are the delimiters themselves and are never split or escape-decoded.

Example:

let message = er7::parse("MSH|^~\\&|LAB\rPID|1")?;
assert!(message.segment("MSH").unwrap().is_header());
assert!(!message.segment("PID").unwrap().is_header());
Source

pub fn to_er7(&self, separators: &Separators) -> String

Write this segment as ER7, exactly as a receiver would read it.

Escape sequences are left intact, so the result can be sent, stored, or parsed again. This is the form the round-trip guarantee applies to (R16).

Example:

let message = er7::parse(r"MSH|^~\&|LAB|Smith \T\ Jones^X")?;
let separators = &message.separators;
let field = message.segment("MSH").unwrap().field(4).unwrap();

assert_eq!(field.to_er7(separators), r"Smith \T\ Jones^X");
Source

pub fn to_text(&self, separators: &Separators) -> String

Write this segment with its leaf text escape-decoded.

Structural delimiters remain, so the result shows the shape of the value as well as its content (R17). That also means the result is not re-parseable: a decoded \F\ becomes a literal field separator. Use this for display, logging, and database writes; use to_er7 for anything that goes back into a message.

Example:

let message = er7::parse(r"MSH|^~\&|LAB|Smith \T\ Jones^X")?;
let separators = &message.separators;
let field = message.segment("MSH").unwrap().field(4).unwrap();

// The escape decodes; the component separator stays.
assert_eq!(field.to_text(separators), "Smith & Jones^X");

Trait Implementations§

Source§

impl Clone for Segment

Source§

fn clone(&self) -> Segment

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Segment

Source§

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

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

impl Deref for Segment

Source§

type Target = Segment

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Segment

Dereferences the value.
Source§

impl DerefMut for Segment

Source§

fn deref_mut(&mut self) -> &mut Segment

Mutably dereferences the value.
Source§

impl<'de> Deserialize<'de> for Segment

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Eq for Segment

Source§

impl From<Segment> for Segment

Source§

fn from(inner: Segment) -> Segment

Converts to this type from the input type.
Source§

impl From<Segment> for Segment

Source§

fn from(outer: Segment) -> Segment

Converts to this type from the input type.
Source§

impl PartialEq for Segment

Source§

fn eq(&self, other: &Segment) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl Serialize for Segment

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 StructuralPartialEq for Segment

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> 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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.