Skip to main content

Field

Struct Field 

Source
pub struct Field(pub Field);
Expand description

A Serde-enabled er7::Field.

A field is nothing but its repetitions in order (er7::Field::repetitions), so — like Repetition one level down — it serializes as a plain array: 555-1111~555-2222 becomes [["555-1111"], ["555-2222"]]. A field that was sent empty (||) has no repetitions at all, and serializes as []; this is what distinguishes it from a present-but-empty repetition (er7::Field spec).

Example:

use serde_er7::Field;

let message = er7::parse(r"MSH|^~\&|LAB|555-1111~555-2222")?;
let field = message.segment("MSH").unwrap().field(4).unwrap().clone();

let json = serde_json::to_string(&Field(field))?;
let back: Field = serde_json::from_str(&json)?;
assert_eq!(back.to_er7(&message.separators), "555-1111~555-2222");

// Absent (`||`) round-trips as an empty array, not `[[]]`.
let empty: Field = serde_json::from_str("[]")?;
assert!(empty.repetitions.is_empty());

Tuple Fields§

§0: Field

Methods from Deref<Target = Field>§

Source

pub fn repetition(&self, n: usize) -> Option<&Repetition>

The 1-based repetition n, if the message carried one.

Example:

let message = er7::parse("MSH|^~\\&|LAB\rPID|555-1111~555-2222")?;
let phones = message.segment("PID").unwrap().field(1).unwrap();

assert_eq!(phones.repetitions.len(), 2);
assert_eq!(phones.repetition(2).unwrap().to_er7(&message.separators), "555-2222");
assert!(phones.repetition(3).is_none());
Source

pub fn repetition_mut(&mut self, n: usize) -> Option<&mut Repetition>

Mutable access to the 1-based repetition n.

Source

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

The 1-based component n of the first repetition — the common case, since most fields do not repeat.

Example:

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

assert_eq!(name.component(1).unwrap().to_er7(&message.separators), "SMITH");
Source

pub fn is_empty(&self) -> bool

True when the field was sent with no repetitions, or with only empty ones. A field holding the explicit null is not empty; see Field::is_null.

Source

pub fn is_null(&self) -> bool

True when this field is exactly the explicit null "", meaning the sender is clearing the stored value.

This is the level the distinction is usually asked at, and getting it wrong is a patient-safety bug: an absent or empty field means “leave the stored value alone”, while a null means “clear it” (R10, R11, spec §5.3).

Example:

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

// Absent: never sent.
assert!(pid.field(9).is_none());
// Empty: sent as `||`, no value.
assert!(pid.field(2).unwrap().is_empty() && !pid.field(2).unwrap().is_null());
// Null: sent as `""`, clear the stored value.
assert!(pid.field(3).unwrap().is_null() && !pid.field(3).unwrap().is_empty());
Source

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

Write this field 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 field 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 Field

Source§

fn clone(&self) -> Field

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 Field

Source§

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

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

impl Default for Field

Source§

fn default() -> Field

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

impl Deref for Field

Source§

type Target = Field

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Field

Dereferences the value.
Source§

impl DerefMut for Field

Source§

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

Mutably dereferences the value.
Source§

impl<'de> Deserialize<'de> for Field

Source§

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

Read an array of repetitions into repetitions.

Source§

impl Eq for Field

Source§

impl From<Field> for Field

Source§

fn from(inner: Field) -> Field

Converts to this type from the input type.
Source§

impl From<Field> for Field

Source§

fn from(outer: Field) -> Field

Converts to this type from the input type.
Source§

impl PartialEq for Field

Source§

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

Source§

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

Write each repetition as one array element. See Component::serialize for the cost of the clone this involves.

Source§

impl StructuralPartialEq for Field

Auto Trait Implementations§

§

impl Freeze for Field

§

impl RefUnwindSafe for Field

§

impl Send for Field

§

impl Sync for Field

§

impl Unpin for Field

§

impl UnsafeUnpin for Field

§

impl UnwindSafe for Field

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.