#[non_exhaustive]pub struct Segment<'a> {
pub tag: Cow<'a, str>,
pub span: Span,
pub tag_span: Span,
pub elements: Vec<Element<'a>>,
}Expand description
A single EDIFACT segment.
Borrows its text from the parsed input where it can, and owns it where it
cannot: from_bytes yields Segment<'input>, while
from_reader yields Segment<'static> — aliased as
OwnedSegment. Covariance in 'a means one is accepted wherever the other
is, so every API in this crate takes a single shape.
#[non_exhaustive]: build one with Segment::new rather than a struct
literal, so a future field stays additive. The fields stay public, so reading
and .. destructuring are unaffected.
Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.tag: Cow<'a, str>Segment tag — three ASCII uppercase letters for anything this crate parses.
span: SpanSpan covering the whole segment payload.
tag_span: SpanSpan covering only the segment tag.
elements: Vec<Element<'a>>Segment elements in positional order.
Implementations§
Source§impl<'a> Segment<'a>
impl<'a> Segment<'a>
Sourcepub fn new(tag: impl Into<Cow<'a, str>>, elements: Vec<Element<'a>>) -> Self
pub fn new(tag: impl Into<Cow<'a, str>>, elements: Vec<Element<'a>>) -> Self
Build a segment from a tag and its data elements.
Spans default to Span::default, which is what a segment synthesised
from a non-EDIFACT source should carry — there is no input to point at.
Use with_spans when there is.
The tag is not checked here; it is checked when the segment is written.
A tag is emitted verbatim, so one the parser would reject is refused as
EdifactError::InvalidSegmentTag by
Writer::write_segment rather than
written out as bytes that do not read back.
§Example
use edifact_rs::{Element, Segment, segments_to_bytes};
let segment = Segment::new("BGM", vec![Element::of(&["220"])]);
assert_eq!(segments_to_bytes(&[segment])?, b"BGM+220'".to_vec());Sourcepub fn with_spans(self, span: Span, tag_span: Span) -> Self
pub fn with_spans(self, span: Span, tag_span: Span) -> Self
Set the segment and tag spans.
Sourcepub fn tag(&self) -> &str
pub fn tag(&self) -> &str
The segment tag as a plain &str.
segment.tag compares directly against a string literal
(segment.tag == "BGM"); this is for the places that need a &str, such
as a match.
Sourcepub fn get_element(&self, n: usize) -> Option<&Element<'a>>
pub fn get_element(&self, n: usize) -> Option<&Element<'a>>
Return the element at position n (0-indexed), if it exists.
Sourcepub fn element_str(&self, n: usize) -> Option<&str>
pub fn element_str(&self, n: usize) -> Option<&str>
Shorthand: component 0 of element n — the most common access pattern.
Sourcepub fn component_str(&self, elem: usize, comp: usize) -> Option<&str>
pub fn component_str(&self, elem: usize, comp: usize) -> Option<&str>
Get component comp of element elem (both 0-based), or None if absent.
Sourcepub fn element_span(&self, n: usize) -> Option<Span>
pub fn element_span(&self, n: usize) -> Option<Span>
Return the byte span of the element at position n, if it exists.
Sourcepub fn repeated_component(
&self,
element: usize,
component: usize,
) -> impl Iterator<Item = &str>
pub fn repeated_component( &self, element: usize, component: usize, ) -> impl Iterator<Item = &str>
Read component component from every occurrence of element element.
Yields nothing when the element is absent. See
Element::repeated_component.
§Example
// `UNA` position 050 declares `*` as the repetition separator.
let segments: Vec<_> = edifact_rs::from_bytes(b"UNA:+.?*'RFF+ON:1*ON:2*ON:3'")
.collect::<Result<Vec<_>, _>>()?;
let references: Vec<&str> = segments[0].repeated_component(0, 1).collect();
assert_eq!(references, ["1", "2", "3"]);Sourcepub fn into_owned(self) -> OwnedSegment
pub fn into_owned(self) -> OwnedSegment
Detach this segment from the input buffer, cloning any borrowed text.
Use it to keep a segment alive past the buffer it was parsed from.
Sourcepub fn required_element(&self, idx: usize) -> Result<&str, EdifactError>
pub fn required_element(&self, idx: usize) -> Result<&str, EdifactError>
Read element idx, treating an empty value as absent.
EDIFACT lets an element be syntactically present but empty (SEG++').
A mandatory data element must carry a value, so this reports
EdifactError::MissingRequiredElement for both cases.
§Errors
EdifactError::MissingRequiredElement when the element is absent or empty.
Sourcepub fn optional_element(&self, idx: usize) -> Option<&str>
pub fn optional_element(&self, idx: usize) -> Option<&str>
Read element idx, treating an empty value as absent.
Sourcepub fn required_component(
&self,
elem: usize,
comp: usize,
) -> Result<&str, EdifactError>
pub fn required_component( &self, elem: usize, comp: usize, ) -> Result<&str, EdifactError>
Read component comp of element elem, treating an empty value as absent.
§Errors
EdifactError::MissingRequiredElement when the element itself is
absent, and EdifactError::MissingRequiredComponent when the element is
present but the component is absent or empty. The distinction matters:
the first says the segment is too short, the second that one composite is
incomplete.
Sourcepub fn optional_component(&self, elem: usize, comp: usize) -> Option<&str>
pub fn optional_component(&self, elem: usize, comp: usize) -> Option<&str>
Read component comp of element elem, treating an empty value as absent.
Sourcepub fn parsed_element<T: FromStr>(&self, idx: usize) -> Result<T, EdifactError>
pub fn parsed_element<T: FromStr>(&self, idx: usize) -> Result<T, EdifactError>
Read element idx and parse it into T.
§Errors
As required_element, plus
EdifactError::InvalidText when the value does not parse.
Sourcepub fn value_at(&self, path: ElementPath) -> Option<&str>
pub fn value_at(&self, path: ElementPath) -> Option<&str>
Read the value at an already-resolved ElementPath.
Use this when the same path is reused across many segments — resolve once
with SegmentLayout::resolve_code, then read without repeating the
lookup.
Sourcepub fn span_at(&self, path: ElementPath) -> Option<Span>
pub fn span_at(&self, path: ElementPath) -> Option<Span>
Byte span of the value at an already-resolved ElementPath.
Sourcepub fn value_by_code<L: SegmentLayout + ?Sized>(
&self,
layout: &L,
data_element: &str,
) -> Result<Option<&str>, EdifactError>
pub fn value_by_code<L: SegmentLayout + ?Sized>( &self, layout: &L, data_element: &str, ) -> Result<Option<&str>, EdifactError>
Read a value by its UN/EDIFACT data element identifier.
Positional access (seg.element_str(4)) fails silently when the index is
wrong: it reads a different, usually still-plausible value. Code-addressed
access cannot — a stale or mistyped identifier is a
EdifactError::UnknownDataElement, checked against the directory.
Ok(None) means the identifier is valid for this segment but the value is
absent from this instance, which is the normal state for a conditional
element.
§Performance
Each call scans the layout for the identifier. That is a handful of short
string comparisons and fine for one-off reads, but when pulling the same
identifier out of many segments, resolve once with
SegmentLayout::resolve_code and read with value_at.
§Example
use edifact_rs::{ComponentRef, ElementRef, SegmentDefinition, Status};
static C507: &[ComponentRef] = &[
ComponentRef::new(1, "2005", Status::Mandatory),
ComponentRef::new(2, "2380", Status::Conditional),
ComponentRef::new(3, "2379", Status::Conditional),
];
static DTM_ELEMENTS: &[ElementRef] =
&[ElementRef::composite(1, "C507", Status::Mandatory, 1, C507)];
static DTM: SegmentDefinition =
SegmentDefinition::new("DTM", "Date/time/period", DTM_ELEMENTS);
let segments: Vec<_> = edifact_rs::from_bytes(b"DTM+137:20260101:102'")
.collect::<Result<Vec<_>, _>>()?;
let dtm = &segments[0];
assert_eq!(dtm.value_by_code(&DTM, "2380")?, Some("20260101"));
// A data element that this segment does not define is a hard error,
// not a wrong-but-quiet read.
assert!(dtm.value_by_code(&DTM, "3055").is_err());§Errors
Returns EdifactError::SegmentLayoutMismatch when layout describes a
different segment tag, EdifactError::UnknownDataElement when the
identifier is not in the definition, and
EdifactError::AmbiguousDataElement when it appears more than once.
Sourcepub fn span_by_code<L: SegmentLayout + ?Sized>(
&self,
layout: &L,
data_element: &str,
) -> Result<Option<Span>, EdifactError>
pub fn span_by_code<L: SegmentLayout + ?Sized>( &self, layout: &L, data_element: &str, ) -> Result<Option<Span>, EdifactError>
Byte span of a value addressed by its UN/EDIFACT data element identifier.
Use this to attach a precise Span to a
ValidationIssue without hand-counting indices.
§Errors
As value_by_code.
Sourcepub fn element_by_code<L: SegmentLayout + ?Sized>(
&self,
layout: &L,
data_element: &str,
) -> Result<Option<&Element<'a>>, EdifactError>
pub fn element_by_code<L: SegmentLayout + ?Sized>( &self, layout: &L, data_element: &str, ) -> Result<Option<&Element<'a>>, EdifactError>
Return the whole Element addressed by a data element identifier.
When the identifier names a component inside a composite, the enclosing composite element is returned.
§Errors
As value_by_code.
Trait Implementations§
impl<'a> Eq for Segment<'a>
impl<'a> StructuralPartialEq for Segment<'a>
Auto Trait Implementations§
impl<'a> Freeze for Segment<'a>
impl<'a> RefUnwindSafe for Segment<'a>
impl<'a> Send for Segment<'a>
impl<'a> Sync for Segment<'a>
impl<'a> Unpin for Segment<'a>
impl<'a> UnsafeUnpin for Segment<'a>
impl<'a> UnwindSafe for Segment<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<D> OwoColorize for D
impl<D> OwoColorize for D
Source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
Source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
Source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
Source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
Source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
Source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
Source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
Source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
Source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
Source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
Source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
Source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
Source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
Source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
Source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
Source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
Source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
Source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
Source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
Source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
Source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
Source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
Source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
Source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
Source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
Source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
Source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
Source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
Source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
Source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
Source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
Source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
Source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
Source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
Source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
Source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
Source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
Source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
Source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
Source§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg or
a color-specific method, such as OwoColorize::green, Read moreSource§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg or
a color-specific method, such as OwoColorize::on_yellow, Read more