Skip to main content

impl_code_vocabulary

Macro impl_code_vocabulary 

macro_rules! impl_code_vocabulary {
    (
        $ty:ty, $label:literal $(, form = $form:literal)? ;
        $( $variant:ident => $canonical:literal $(, [ $($alias:literal),+ $(,)? ])? ; )+
        $( @fallback $fallback:expr ; )?
    ) => { ... };
    (
        normalized; $ty:ty, $label:literal $(, form = $form:literal)? ;
        $( $variant:ident => $canonical:literal $(, [ $($alias:literal),+ $(,)? ])? ; )+
        $( @fallback $fallback:expr ; )?
    ) => { ... };
    (
        exact; $ty:ty, $label:literal $(, form = $form:literal)? ;
        $( $variant:ident => $canonical:literal $(, [ $($alias:literal),+ $(,)? ])? ; )+
        $( @fallback $fallback:expr ; )?
    ) => { ... };
    (
        @impl $eq:path;
        $ty:ty, $label:literal $(, form = $form:literal)? ;
        $( $variant:ident => $canonical:literal $(, [ $($alias:literal),+ $(,)? ])? ; )+
        $( @fallback $fallback:expr ; )?
    ) => { ... };
}
Expand description

Generates ACCEPTED and FromStr for a vocabulary whose codes are punctuated by an external scheme, from one variant-to-spelling mapping.

The sibling of impl_vocabulary!, and the reason the two exist separately: this one matches on case-folded equality and nothing else. It does not strip _, -, or whitespace, so / and . stay significant and "ACT/365" does not become "ACT/365.FIXED". What it does share is the obligation that matters to a caller — the rejection names the accepted set, from the same list the parser matched against.

Unlike impl_vocabulary! it does not generate code(): these enums render their canonical spelling under rules of their own (Frequency returns an owned period code, RollConvention carries a day of the month), so code() stays hand-written and is held to the list by the round-trip in tests/closed_vocabulary.rs.

Two optional clauses carry the vocabularies whose accepted set is not closed:

  • form = "…" — an open tail rendered after the list, for a set that is a list of names plus a shape.
  • @fallback <expr> — an Fn(&str) -> Option<Self> tried after every listed spelling, which is what parses that tail.

A vocabulary declaring fallback without form would advertise a closed set it does not have, so the two are written together.

ⓘ
impl_code_vocabulary! {
    DayCountFraction, "day count";
    Act360      => "ACT/360";
    Act365Fixed => "ACT/365.FIXED";
}