//! This internal library provides the procedural macros needed by the crate [`color-print`].
//!
//! [`color-print`]: https://crates.io/crates/color-print

extern crate proc_macro;

#[macro_use]
mod util;
mod error;
#[cfg(not(feature = "terminfo"))]
mod ansi;
#[cfg(not(feature = "terminfo"))]
mod ansi_constants;
mod color_context;
mod format_args;
mod parse;
#[cfg(feature = "terminfo")]
mod terminfo;

use proc_macro::TokenStream;
use quote::{quote, ToTokens};

/// The same as `format!()`, but parses color tags.
#[proc_macro]
pub fn cformat(input: TokenStream) -> TokenStream {
    get_macro("format", input)
}

/// The same as `print!()`, but parses color tags.
#[proc_macro]
pub fn cprint(input: TokenStream) -> TokenStream {
    get_macro("print", input)
}

/// The same as `println!()`, but parses color tags.
#[proc_macro]
pub fn cprintln(input: TokenStream) -> TokenStream {
    get_macro("println", input)
}

/// Renders a whole processed macro.
fn get_macro(macro_name: &str, input: TokenStream) -> TokenStream {
    #[cfg(not(feature = "terminfo"))]
    let format_args = crate::ansi::get_format_args(input);
    #[cfg(feature = "terminfo")]
    let format_args = crate::terminfo::get_format_args(input);

    let format_args = format_args.unwrap_or_else(|err| err.to_token_stream());
    let macro_name = util::ident(macro_name);
    (quote! { #macro_name!(#format_args) }).into()
}
