flow-span 0.1.0-rc.5

Source positions, source files, the identifier interner, and the places in a run.
Documentation
//! Closed tables: a type's cases and one value for each, declared once.

/// Declares an enum's cases once, and tables that give each case one value.
///
/// The `enum` form declares a fieldless enum, its `ALL`, every variant in
/// declaration order with the enum's visibility, and its first table. A
/// variant's value there defaults to its own name. The `impl` form adds one
/// more table to an enum declared anywhere, one row per case, so a type may
/// have as many tables as it has encodings. Each table gives a `const fn`
/// that returns a case's value and a `fn` that returns the case a value
/// names, with the visibility its header writes. Each case is written as a
/// pattern, so the value `match` is exhaustive and a case left out of a
/// table does not compile. A value is a literal or a path, such as a unit
/// variant of another enum, and the inverse is a `match` too, so a value
/// written twice is an unreachable pattern.
///
/// ```
/// flow_span::spelled! {
///     /// A primary colour.
///     pub enum Primary {
///         /// Its name.
///         pub fn name(self) -> &'static str;
///         /// The colour `name` names.
///         pub fn named(&str);
///         /// Red.
///         Red,
///         /// Blue.
///         Blue = "blue",
///     }
/// }
///
/// flow_span::spelled! {
///     impl Primary {
///         /// Its number.
///         pub fn number(self) -> u8;
///         /// The colour numbered `number`.
///         pub fn numbered(u8);
///         [Primary::Red] = 0,
///         [Primary::Blue] = 1,
///     }
/// }
///
/// assert_eq!(Primary::named("Red"), Some(Primary::Red));
/// assert_eq!(Primary::Blue.name(), "blue");
/// assert_eq!(Primary::numbered(1), Some(Primary::Blue));
/// assert_eq!(Primary::ALL.len(), 2);
/// ```
#[macro_export]
macro_rules! spelled {
    (@value $variant:ident) => {
        stringify!($variant)
    };
    (@value $variant:ident $value:tt) => {
        $value
    };
    (
        $(#[$meta:meta])*
        $vis:vis enum $enum:ident {
            $(#[$get_doc:meta])*
            $get_vis:vis fn $get:ident(self) -> $value:ty;
            $(#[$of_doc:meta])*
            $of_vis:vis fn $of:ident($given:ty);
            $($(#[$doc:meta])* $variant:ident $(= $spelling:tt)?,)+
        }
    ) => {
        $(#[$meta])*
        #[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
        $vis enum $enum {
            $($(#[$doc])* $variant,)+
        }

        impl $enum {
            /// Every one, in declaration order.
            $vis const ALL: &'static [Self] = &[$($enum::$variant,)+];
        }

        $crate::spelled! {
            impl $enum {
                $(#[$get_doc])*
                $get_vis fn $get(self) -> $value;
                $(#[$of_doc])*
                $of_vis fn $of($given);
                $([$enum::$variant] = ($crate::spelled!(@value $variant $($spelling)?)),)+
            }
        }
    };
    (
        $(#[$meta:meta])*
        $vis:vis enum $enum:ident {
            $(#[$get_doc:meta])*
            $get_vis:vis fn $get:ident(self) -> $value:ty;
            $(#[$of_doc:meta])*
            $of_vis:vis fn $of:ident($given:ty);
            $($(#[$doc:meta])* $variant:ident = $spelling:path,)+
        }
    ) => {
        $crate::spelled! {
            $(#[$meta])*
            $vis enum $enum {
                $(#[$get_doc])*
                $get_vis fn $get(self) -> $value;
                $(#[$of_doc])*
                $of_vis fn $of($given);
                $($(#[$doc])* $variant = $spelling,)+
            }
        }
    };
    (
        impl $type:ty {
            $(#[$get_doc:meta])*
            $get_vis:vis fn $get:ident(self) -> $value:ty;
            $(#[$of_doc:meta])*
            $of_vis:vis fn $of:ident($given:ty);
            $([$($case:tt)+] = $spelling:tt,)+
        }
    ) => {
        impl $type {
            $(#[$get_doc])*
            $get_vis const fn $get(self) -> $value {
                match self {
                    $($($case)+ => $spelling,)+
                }
            }

            $(#[$of_doc])*
            $of_vis fn $of(value: $given) -> Option<Self> {
                match value {
                    $($spelling => Some($($case)+),)+
                    _ => None,
                }
            }
        }
    };
}