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: SegmentMethods from Deref<Target = Segment>§
Sourcepub fn field(&self, n: usize) -> Option<&Field>
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());Sourcepub fn field_mut(&mut self, n: usize) -> Option<&mut Field>
pub fn field_mut(&mut self, n: usize) -> Option<&mut Field>
Mutable access to the 1-based field n.
Sourcepub fn component(&self, field: usize, component: usize) -> Option<&Component>
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");Sourcepub fn is_header(&self) -> bool
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());Sourcepub fn to_er7(&self, separators: &Separators) -> String
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");Sourcepub fn to_text(&self, separators: &Separators) -> String
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");