rustfmt_config_proc_macro/lib.rs
1//! This crate provides a derive macro for `ConfigType`.
2
3#![recursion_limit = "256"]
4
5mod attrs;
6mod config_type;
7mod item_enum;
8mod item_struct;
9mod utils;
10
11use std::str::FromStr;
12
13use proc_macro::TokenStream;
14use syn::parse_macro_input;
15
16#[proc_macro_attribute]
17pub 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]
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
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]
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
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. Mosty commonly
75/// this is because rust-lang/rust mounts the repo in a [read-only filesystem](
76/// https://github.com/rust-lang/rust/blob/1.99.0/src/ci/docker/run.sh#L261)
77#[proc_macro_attribute]
78pub fn rustfmt_only_ci_test(_args: TokenStream, input: TokenStream) -> TokenStream {
79 if option_env!("RUSTFMT_CI").is_some() {
80 input
81 } else {
82 let mut token_stream = TokenStream::from_str("#[ignore]").unwrap();
83 token_stream.extend(input);
84 token_stream
85 }
86}