1 //! This crate provides a derive macro for `ConfigType`.
2
3 #![recursion_limit = "256"]
4
5 mod attrs;
6 mod config_type;
7 mod item_enum;
8 mod item_struct;
9 mod utils;
10
11 use std::str::FromStr;
12
13 use proc_macro::TokenStream;
14 use syn::parse_macro_input;
15
16 #[proc_macro_attribute]
config_type(_args: TokenStream, input: TokenStream) -> TokenStream17 pub fn config_type(_args: TokenStream, input: TokenStream) -> TokenStream {
18 let input = parse_macro_input!(input as syn::Item);
19 let output = config_type::define_config_type(&input);
20
21 #[cfg(feature = "debug-with-rustfmt")]
22 {
23 utils::debug_with_rustfmt(&output);
24 }
25
26 TokenStream::from(output)
27 }
28
29 /// 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]
nightly_only_test(_args: TokenStream, input: TokenStream) -> TokenStream41 pub 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
43 if 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"
47 TokenStream::from_str("").unwrap()
48 }
49 }
50
51 /// 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]
stable_only_test(_args: TokenStream, input: TokenStream) -> TokenStream63 pub 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
65 if 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'
69 TokenStream::from_str("").unwrap()
70 }
71 }
72
73 /// 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]
rustfmt_only_ci_test(_args: TokenStream, input: TokenStream) -> TokenStream76 pub fn rustfmt_only_ci_test(_args: TokenStream, input: TokenStream) -> TokenStream {
77 if option_env!("RUSTFMT_CI").is_some() {
78 input
79 } else {
80 let mut token_stream = TokenStream::from_str("#[ignore]").unwrap();
81 token_stream.extend(input);
82 token_stream
83 }
84 }
85