fake-main 0.1.3

Just like a create-root level `#![cfg(...)]` macro, but injects a dummy `fn main` when the predicate is `false`
Documentation
//! The cfg fake main macro.

#![feature(proc_macro_expand)]

extern crate proc_macro;
use proc_macro::TokenStream;

/// The macro that does what `cfg` does but also injects a fake `fn main` when
/// the predicate is `false`.
///
/// # Usage
///
/// ```ignore
/// # #[allow(clippy::needless_doctest_main)]
/// #![feature(prelude_import)]
/// #![feature(custom_inner_attributes)]
/// #![::fake_main::cfg_(not(feature = "skip"))]
///
/// fn main() {
///    println!("Real fn main");
/// }
/// ```
#[proc_macro_attribute]
pub fn cfg_(attr: TokenStream, item: TokenStream) -> TokenStream {
    let attr = proc_macro2::TokenStream::from(attr);
    let item = proc_macro2::TokenStream::from(item);

    let attr_text = attr.to_string();

    let to_expand: proc_macro2::TokenStream = quote::quote! {
        ::core::cfg!( #attr )
    };
    let to_expand: TokenStream = to_expand.into();
    let expanded = to_expand.expand_expr().unwrap();

    let enabled = expanded.to_string() == "true";

    if enabled {
        quote::quote! {
            #![feature(prelude_import)]
            #![allow(internal_features)]
            #![allow(unused_imports)]

            #item
        }
        .into()
    } else {
        quote::quote! {
            //! Fake main.

            #![feature(prelude_import)]
            #![allow(internal_features)]
            #![allow(missing_docs)]

            #[macro_use]
            extern crate std;

            #[prelude_import]
            use std::prelude::v1::*;

            /// Fake main.
            fn main() {
                unimplemented!(#attr_text);
            }
        }
        .into()
    }
}