Skip to main content

Subcomponent

Struct Subcomponent 

Source
pub struct Subcomponent(pub Subcomponent);
Expand description

A Serde-enabled er7::Subcomponent.

A subcomponent holds only text — er7::Subcomponent::raw — so it serializes as a bare string rather than as a one-field object. That choice is the one that makes the whole tree read naturally in JSON: PID-5.1 becomes "SMITH", not {"raw": "SMITH"}.

The text serialized is raw, exactly as the sender wrote it — escape sequences included, not er7::Subcomponent::value-decoded. That keeps the crate’s core promise: Message::parse(text)? followed by .to_er7() on the other end of any Serde format reproduces the original bytes. Decode with er7::Subcomponent::value yourself where you want the resolved text instead.

Example:

use serde_er7::Subcomponent;

let leaf = Subcomponent(er7::Subcomponent::new(r"Smith \T\ Jones"));
let json = serde_json::to_string(&leaf)?;
assert_eq!(json, r#""Smith \\T\\ Jones""#);

let back: Subcomponent = serde_json::from_str(&json)?;
assert_eq!(back.raw, r"Smith \T\ Jones");

Tuple Fields§

§0: Subcomponent

Methods from Deref<Target = Subcomponent>§

Source

pub fn value(&self, separators: &Separators) -> Cow<'_, str>

The decoded text: escape sequences that stand for characters are resolved, and the explicit null "" reads as the empty string.

Because null and empty both read as empty here, ask Subcomponent::is_null when the difference matters — for a database write, it always does (spec §5.3).

Example:

use er7::{Separators, Subcomponent};

let separators = Separators::default();

assert_eq!(Subcomponent::new(r"a\T\b").value(&separators), "a&b");
// A sequence with no plain-text meaning is kept as written.
assert_eq!(Subcomponent::new(r"a\.br\b").value(&separators), r"a\.br\b");
// The explicit null reads as empty; ask `is_null` to tell them apart.
assert_eq!(Subcomponent::new(r#""""#).value(&separators), "");
Source

pub fn set(&mut self, value: &str, separators: &Separators)

Replace the text with value, encoding any delimiters it contains.

This is the recommended way to write a value. Assigning Subcomponent::raw directly is allowed, but then the escaping is yours to get right: an unescaped & would split the component in two the next time the message was parsed, shifting every value after it (spec §5.5).

Example:

use er7::{Separators, Subcomponent};

let separators = Separators::default();
let mut leaf = Subcomponent::default();

leaf.set("Smith & Jones", &separators);
assert_eq!(leaf.raw, r"Smith \T\ Jones");
assert_eq!(leaf.value(&separators), "Smith & Jones");
§This takes text, not ER7

Everything handed to set is data, so every delimiter in it is encoded — including ~. Passing a whole field’s ER7 through here therefore collapses it: three repetitions arrive as one value holding two \R\ sequences. That is set doing its job, and the wrong tool for moving a value that is more than one leaf.

Copy the structure instead. Every level of the tree is a public Vec and every node is Clone, so a repeating field moves as itself (spec §5.5):

use er7::Field;

let source = er7::parse("MSH|^~\\&|LAB\rPID|1||A~B~C")?;
let mut target = er7::parse("MSH|^~\\&|LAB\rPID|1")?;

let ids = source.segment("PID").unwrap().field(3).unwrap().clone();
let pid = target.segment_at_mut("PID", 1).unwrap();
if pid.fields.len() < 3 {
    pid.fields.resize(3, Field::default());   // 1-based position 3
}
pid.fields[2] = ids;

// All three repetitions are still repetitions.
assert_eq!(target.to_er7(), "MSH|^~\\&|LAB\rPID|1||A~B~C");
assert_eq!(target.query("PID-3[2]")?.as_deref(), Some("B"));
Source

pub fn is_null(&self) -> bool

True when this is the explicit HL7 null "", meaning “the sender is clearing this value”, not “the sender had nothing to say”.

Getting this wrong is a patient-safety bug: treating a null as empty leaves a withdrawn allergy on the record (spec §5.3, R10).

Example:

use er7::Subcomponent;

let null = Subcomponent::new(r#""""#);
assert!(null.is_null());
assert!(!null.is_empty());   // the null is text, so never both

let empty = Subcomponent::new("");
assert!(empty.is_empty());
assert!(!empty.is_null());
Source

pub fn is_empty(&self) -> bool

True when no text was sent here at all. The explicit null is text, so is_empty and Subcomponent::is_null are never both true — together they separate “nothing to say” from “clear this” (R11).

See Subcomponent::is_null for a worked example of both.

Source

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

Write this subcomponent 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 subcomponent 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 Subcomponent

Source§

fn clone(&self) -> Subcomponent

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 Subcomponent

Source§

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

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

impl Default for Subcomponent

Source§

fn default() -> Subcomponent

Returns the “default value” for a type. Read more
Source§

impl Deref for Subcomponent

Source§

type Target = Subcomponent

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Subcomponent

Dereferences the value.
Source§

impl DerefMut for Subcomponent

Source§

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

Mutably dereferences the value.
Source§

impl<'de> Deserialize<'de> for Subcomponent

Source§

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

Read a string into raw, via Deserializer::deserialize_str.

Source§

impl Eq for Subcomponent

Source§

impl From<Subcomponent> for Subcomponent

Source§

fn from(inner: Subcomponent) -> Subcomponent

Converts to this type from the input type.
Source§

impl From<Subcomponent> for Subcomponent

Source§

fn from(outer: Subcomponent) -> Subcomponent

Converts to this type from the input type.
Source§

impl PartialEq for Subcomponent

Source§

fn eq(&self, other: &Subcomponent) -> 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 Subcomponent

Source§

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

Write raw as a string, via Serializer::serialize_str.

Source§

impl StructuralPartialEq for Subcomponent

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.