//! Derive macro implementation for the `apollo-errors` crate.
//!
//! This crate should not be used directly. Use `apollo_errors` instead,
//! which re-exports everything you need.

use proc_macro::TokenStream;
use syn::{DeriveInput, parse_macro_input};

mod codegen;
mod ir;
mod parse;

#[proc_macro_derive(
    Error,
    attributes(
        error,
        diagnostic,
        extension,
        http_status,
        http_header,
        jsonrpc_code,
        source,
        from
    )
)]
pub fn derive_error(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);

    // Data flow: Parse->IR->Codegen
    match parse::parse_error_derive(input) {
        Ok(ir) => {
            let tokens = codegen::generate(&ir);
            TokenStream::from(tokens)
        }
        Err(err) => err.to_compile_error().into(),
    }
}

#[cfg(test)]
mod tests;
