#[non_exhaustive]pub struct Element<'a> {
pub span: Span,
pub components: Components<'a>,
pub repeats: Vec<Components<'a>>,
}Expand description
A data element, which may have one or more component values.
#[non_exhaustive]: build one with Element::of (plus
and_repeat / with_span)
rather than a struct literal.
Uses SmallVec with an inline capacity of 4 to avoid heap allocation
for the common case (≤ 4 components). Each entry is a (value, span) pair,
guaranteeing that the component string and its byte span stay in sync.
A value that contained a release-character sequence is stored as
Cow::Owned; everything else borrows from the input.
§Repetition (ISO 9735-1 §8.6)
components holds the first occurrence, which is the
only one for every interchange that does not declare a repetition separator —
that is, virtually all of them. Further occurrences land in
repeats; read them together with
repetitions.
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.span: SpanSpan covering the whole element, including every occurrence.
components: Components<'a>Components of the first occurrence, in positional order.
repeats: Vec<Components<'a>>Second and subsequent occurrences of this data element.
Empty — and therefore unallocated — unless the interchange declares a repetition separator and the element actually repeats.
Implementations§
Source§impl<'a> Element<'a>
impl<'a> Element<'a>
Sourcepub fn of<S>(components: &[S]) -> Self
pub fn of<S>(components: &[S]) -> Self
Build a data element from its component values.
Accepts anything that converts to Cow<'a, str>, so both string literals
and owned Strings work:
use edifact_rs::{Element, OwnedElement};
let borrowed = Element::of(&["4000001000002", "", "9"]);
let owned: OwnedElement = Element::of(&[String::from("BY")]);
assert_eq!(borrowed.get_component(2), Some("9"));
assert_eq!(owned.get_component(0), Some("BY"));Sourcepub fn get_component(&self, n: usize) -> Option<&str>
pub fn get_component(&self, n: usize) -> Option<&str>
Return the component at position n (0-indexed) of the first occurrence.
Sourcepub fn component_or_empty(&self, n: usize) -> &str
pub fn component_or_empty(&self, n: usize) -> &str
Return the component at position n, or "" if absent.
Sourcepub fn component_span(&self, n: usize) -> Option<Span>
pub fn component_span(&self, n: usize) -> Option<Span>
Return the byte span of the component at position n, if it exists.
Sourcepub fn components(&self) -> impl Iterator<Item = &str>
pub fn components(&self) -> impl Iterator<Item = &str>
Iterate over the component values of the first occurrence.
Sourcepub fn repeat_count(&self) -> usize
pub fn repeat_count(&self) -> usize
Number of occurrences of this data element — always at least 1.
Sourcepub fn repetition(&self, n: usize) -> Option<&[(Cow<'a, str>, Span)]>
pub fn repetition(&self, n: usize) -> Option<&[(Cow<'a, str>, Span)]>
Components of occurrence n (0-indexed), if it exists.
Sourcepub fn repetitions(&self) -> impl Iterator<Item = &[(Cow<'a, str>, Span)]>
pub fn repetitions(&self) -> impl Iterator<Item = &[(Cow<'a, str>, Span)]>
Iterate over every occurrence of this element, the first one included.
§Example
// `UNA` byte 7 declares `*` as the repetition separator.
let segments: Vec<_> = edifact_rs::from_bytes(b"UNA:+.?*'RFF+ON:1*ON:2'")
.collect::<Result<Vec<_>, _>>()?;
let rff = segments[0].get_element(0).unwrap();
let refs: Vec<&str> = rff
.repetitions()
.map(|components| components[1].0.as_ref())
.collect();
assert_eq!(refs, ["1", "2"]);Sourcepub fn repeated_component(&self, n: usize) -> impl Iterator<Item = &str>
pub fn repeated_component(&self, n: usize) -> impl Iterator<Item = &str>
Read component n from every occurrence of this element (ISO 9735-1 §8.6).
One item per occurrence, using "" where an occurrence omits the
component: §8.7.3 makes occurrence position significant, so dropping the
empty ones would shift every later value into the wrong slot.
Sourcepub fn with_span(self, span: Span) -> Self
pub fn with_span(self, span: Span) -> Self
Set the span covering this element.
Parsed elements carry real spans; hand-built ones default to
Span::default and only need this when the caller is synthesising
input for diagnostics.
Sourcepub fn and_repeat<S>(self, components: &[S]) -> Self
pub fn and_repeat<S>(self, components: &[S]) -> Self
Append a further occurrence of this data element (ISO 9735-1 §8.6).
Useful when building segments for
Writer::write_segment; the writer joins
occurrences with the active repetition separator.
Sourcepub fn offset_in_place(&mut self, delta: usize)
pub fn offset_in_place(&mut self, delta: usize)
Shift every stored span by delta bytes, in place.
Every occurrence is shifted, not just the first: the reader parses each segment from a zero-based slice and then rebases it onto the stream, so an occurrence left unshifted points into a different segment entirely.
Sourcepub fn into_owned(self) -> OwnedElement
pub fn into_owned(self) -> OwnedElement
Detach this element from the input buffer, cloning any borrowed text.
Trait Implementations§
impl<'a> Eq for Element<'a>
impl<'a> StructuralPartialEq for Element<'a>
Auto Trait Implementations§
impl<'a> Freeze for Element<'a>
impl<'a> RefUnwindSafe for Element<'a>
impl<'a> Send for Element<'a>
impl<'a> Sync for Element<'a>
impl<'a> Unpin for Element<'a>
impl<'a> UnsafeUnpin for Element<'a>
impl<'a> UnwindSafe for Element<'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