pub struct Segment<'a> {
pub tag: &'a str,
pub span: Span,
pub tag_span: Span,
pub elements: Vec<Element<'a>>,
}Expand description
A single EDIFACT segment, borrowing its data from the source input.
Fields§
§tag: &'a strSegment tag, usually three uppercase letters.
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: &'a str, elements: Vec<Element<'a>>) -> Self
pub fn new(tag: &'a str, elements: Vec<Element<'a>>) -> Self
Construct a segment with default spans.
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: get 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.
Mirrors OwnedSegment::component_str, eliminating the need to chain
get_element(elem)?.get_component(comp) in rule closures.
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 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>
Source§impl<'a> From<Segment<'a>> for OwnedSegment
impl<'a> From<Segment<'a>> for OwnedSegment
Source§impl<'s, 'd> SegmentAccessor<'s> for Segment<'d>where
'd: 's,
impl<'s, 'd> SegmentAccessor<'s> for Segment<'d>where
'd: 's,
Source§fn get_element(&'s self, idx: usize) -> Option<&'s str>
fn get_element(&'s self, idx: usize) -> Option<&'s str>
idx.Source§fn get_component(&'s self, elem: usize, comp: usize) -> Option<&'s str>
fn get_component(&'s self, elem: usize, comp: usize) -> Option<&'s str>
Source§fn get_composite(&'s self, idx: usize) -> Option<CompositeElement<'s>>
fn get_composite(&'s self, idx: usize) -> Option<CompositeElement<'s>>
idx.Source§fn text_element(&'s self, idx: usize) -> Result<&'s str, EdifactError>
fn text_element(&'s self, idx: usize) -> Result<&'s str, EdifactError>
Source§fn optional_element(&'s self, idx: usize) -> Option<&'s str>
fn optional_element(&'s self, idx: usize) -> Option<&'s str>
Source§fn code_element<T: FromStr>(&'s self, idx: usize) -> Result<T, EdifactError>
fn code_element<T: FromStr>(&'s self, idx: usize) -> Result<T, EdifactError>
Source§fn required_composite(
&'s self,
elem: usize,
comp: usize,
) -> Result<&'s str, EdifactError>
fn required_composite( &'s self, elem: usize, comp: usize, ) -> Result<&'s str, EdifactError>
Source§fn repeating_components_iter(
&'s self,
elem: usize,
start_idx: usize,
count: usize,
) -> impl Iterator<Item = Result<&'s str, EdifactError>> + 's
fn repeating_components_iter( &'s self, elem: usize, start_idx: usize, count: usize, ) -> impl Iterator<Item = Result<&'s str, EdifactError>> + 's
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