Skip to main content

Crate edifact_rs_derive

Crate edifact_rs_derive 

Source
Expand description

Derive macros for EdifactSerialize and EdifactDeserialize.

§Segment struct (single segment)

#[derive(EdifactSerialize, EdifactDeserialize)]
#[edifact(segment = "BGM")]
pub struct BgmSegment {
    #[edifact(element = 0)]
    pub doc_name_code: String,
    #[edifact(element = 1)]
    pub doc_id: String,
    #[edifact(element = 2)]
    pub msg_function: Option<String>,
}

§Segment struct with qualifier

#[derive(EdifactSerialize, EdifactDeserialize)]
#[edifact(segment = "NAD", qualifier = "MS")]
pub struct NadMs {
    #[edifact(element = 1)]
    pub party_id: String,
}

§Message struct (multiple segments)

#[derive(EdifactSerialize, EdifactDeserialize)]
pub struct OrdersMessage {
    pub bgm: BgmSegment,
    pub buyer: NadMs,
    #[edifact(group)]
    pub lines: Vec<LinSegment>,
}

§#[edifact(group)] and Vec<T> fields

The #[edifact(group)] attribute marks a Vec<T> field as a contiguous group of repeated segments. Without the attribute, Vec<T> on a segment struct collects all matching segments from the window into the Vec.

Note: #[edifact(group)] is a documentation and diagnostic attribute only — the generated deserialization code for Vec<T> is identical whether the attribute is present or absent. Its value is in self-documenting intent and in enabling future compile-time group-boundary enforcement.

§Non-String fields and Display / FromStr

Non-String field types (e.g. u32, bool, your own newtype) are serialized via Display and deserialized via FromStr. The derive macro does not add a compile-time bound; if the type does not implement both traits the generated code will fail to compile with a standard “trait not satisfied” error.

To avoid surprises, ensure any non-String field type implements both:

impl std::fmt::Display for MyCode { ... }
impl std::str::FromStr for MyCode { ... }

Derive Macros§

EdifactDeserialize
Derive edifact_rs::EdifactDeserialize for segment or message structs.
EdifactSerialize
Derive edifact_rs::EdifactSerialize for segment or message structs.