pub fn from_bytes_decoded(
input: &[u8],
) -> Result<Vec<OwnedSegment>, EdifactError>Expand description
Parse a byte slice, decoding it from the repertoire its own UNB declares.
decode_interchange followed by from_bytes_owned, in one call that a
caller cannot forget to make. Forgetting is the failure mode worth designing
against: a UNOC corpus stored as UTF-8 parses fine, so the tests pass and
the first conformant counterparty message — the one with ü as the single
byte 0xFC — is rejected as invalid text.
Segments are owned because the decoded buffer is this function’s, not the
caller’s: an ISO 8859-1 payload has to be transcoded to exist as UTF-8 at
all. When the payload is already ASCII or UNOY, decoding borrows and copies
nothing, but the segments are still owned — reach for
decode_interchange plus from_bytes when you want to keep the
zero-copy path and hold the buffer yourself.
§Errors
As decode_interchange, plus any parse error.
§Example
// A conformant UNOC interchange: `Müller` is `4D FC 6C 6C 65 72`.
let mut raw = b"UNB+UNOC:3+S+R+260101:0900+IC1'NAD+BY+M".to_vec();
raw.push(0xFC);
raw.extend_from_slice(b"ller'UNZ+0+IC1'");
// Parsing it directly fails — it is not UTF-8, and it never claimed to be.
assert!(edifact_rs::from_bytes(&raw).collect::<Result<Vec<_>, _>>().is_err());
let segments = edifact_rs::from_bytes_decoded(&raw)?;
assert_eq!(segments[1].element_str(1), Some("Müller"));