matched_enums_macro 1.3.0

Contains the macro for matchable enums.
Documentation
#![no_std]

mod implementation;
mod models;
mod utils;

extern crate proc_macro;

mod runtime_configurable;

use proc_macro2::TokenStream;
use syn::{ItemEnum, parse_macro_input};

use implementation::ImplementationContext;
use models::Enumeration;

use runtime_configurable::{declare_runtime_configurable, implement_runtime_configurable};

fn derive_ranged_impl(enum_type: ItemEnum) -> syn::Result<TokenStream> {
    let enumeration = Enumeration::try_from(enum_type)?;

    let mut stream = TokenStream::default();

    if enumeration.attribute.allow_runtime_configurable {
        stream.extend(declare_runtime_configurable(&enumeration)?);
    }

    for value_type in &enumeration.attribute.value_types {
        let context = ImplementationContext::new(value_type, &enumeration);

        stream.extend(if enumeration.attribute.use_partial_matching {
            context.implement_partial()
        } else {
            context.implement_full()
        });

        if !enumeration.attribute.allow_runtime_configurable {
            continue;
        }

        stream.extend(implement_runtime_configurable(&context)?);
    }

    Ok(stream)
}

#[proc_macro_derive(Matched, attributes(matches, matched_enum))]
pub fn derive_ranged(stream: proc_macro::TokenStream) -> proc_macro::TokenStream {
    derive_ranged_impl(parse_macro_input!(stream as ItemEnum))
        .unwrap_or_else(|err| err.into_compile_error())
        .into()
}

#[cfg(test)]
mod test;