1//! This crate provides a derive macro for `ConfigType`.
23#![recursion_limit = "256"]
45mod attrs;
6mod config_type;
7mod item_enum;
8mod item_struct;
9mod utils;
1011use std::str::FromStr;
1213use proc_macro::TokenStream;
14use syn::parse_macro_input;
1516#[proc_macro_attribute]
17pub fn config_type(_args: TokenStream, input: TokenStream) -> TokenStream {
18let input = parse_macro_input!(input as syn::Item);
19let output = config_type::define_config_type(&input);
2021#[cfg(feature = "debug-with-rustfmt")]
22{
23 utils::debug_with_rustfmt(&output);
24 }
2526 TokenStream::from(output)
27}
2829/// Used to conditionally output the TokenStream for tests that need to be run on nightly only.
30///
31/// ```rust
32/// # use rustfmt_config_proc_macro::nightly_only_test;
33///
34/// #[nightly_only_test]
35/// #[test]
36/// fn test_needs_nightly_rustfmt() {
37/// assert!(true);
38/// }
39/// ```
40#[proc_macro_attribute]
41pub fn nightly_only_test(_args: TokenStream, input: TokenStream) -> TokenStream {
42// if CFG_RELEASE_CHANNEL is not set we default to nightly, hence why the default is true
43if option_env!("CFG_RELEASE_CHANNEL").map_or(true, |c| c == "nightly" || c == "dev") {
44 input
45 } else {
46// output an empty token stream if CFG_RELEASE_CHANNEL is not set to "nightly" or "dev"
47TokenStream::from_str("").unwrap()
48 }
49}
5051/// Used to conditionally output the TokenStream for tests that need to be run on stable only.
52///
53/// ```rust
54/// # use rustfmt_config_proc_macro::stable_only_test;
55///
56/// #[stable_only_test]
57/// #[test]
58/// fn test_needs_stable_rustfmt() {
59/// assert!(true);
60/// }
61/// ```
62#[proc_macro_attribute]
63pub fn stable_only_test(_args: TokenStream, input: TokenStream) -> TokenStream {
64// if CFG_RELEASE_CHANNEL is not set we default to nightly, hence why the default is false
65if option_env!("CFG_RELEASE_CHANNEL").map_or(false, |c| c == "stable") {
66 input
67 } else {
68// output an empty token stream if CFG_RELEASE_CHANNEL is not set or is not 'stable'
69TokenStream::from_str("").unwrap()
70 }
71}
7273/// Used to conditionally output the TokenStream for tests that should be run as part of rustfmts
74/// test suite, but should be ignored when running in the rust-lang/rust test suite.
75#[proc_macro_attribute]
76pub fn rustfmt_only_ci_test(_args: TokenStream, input: TokenStream) -> TokenStream {
77if option_env!("RUSTFMT_CI").is_some() {
78 input
79 } else {
80let mut token_stream = TokenStream::from_str("#[ignore]").unwrap();
81 token_stream.extend(input);
82 token_stream
83 }
84}