Skip to main content

rustfmt_nightly/config/
style_edition.rs

1use crate::config::StyleEdition;
2
3/// Defines the default value for the given style edition
4#[allow(dead_code)]
5pub trait StyleEditionDefault {
6    type ConfigType;
7    fn style_edition_default(style_edition: StyleEdition) -> Self::ConfigType;
8}
9
10/// macro to help implement `StyleEditionDefault` for config options
11#[macro_export]
12macro_rules! style_edition_default {
13    ($ty:ident, $config_ty:ty, _ => $default:expr) => {
14        impl $crate::config::style_edition::StyleEditionDefault for $ty {
15            type ConfigType = $config_ty;
16
17            fn style_edition_default(_: $crate::config::StyleEdition) -> Self::ConfigType {
18                $default
19            }
20        }
21    };
22    ($ty:ident, $config_ty:ty, Edition2024 => $default_2024:expr, _ => $default_2015:expr) => {
23        impl $crate::config::style_edition::StyleEditionDefault for $ty {
24            type ConfigType = $config_ty;
25
26            fn style_edition_default(
27                style_edition: $crate::config::StyleEdition,
28            ) -> Self::ConfigType {
29                match style_edition {
30                    $crate::config::StyleEdition::Edition2015
31                    | $crate::config::StyleEdition::Edition2018
32                    | $crate::config::StyleEdition::Edition2021 => $default_2015,
33                    // TODO: should update macro when default_2027 becomes available
34                    $crate::config::StyleEdition::Edition2024
35                    | $crate::config::StyleEdition::Edition2027 => $default_2024,
36                }
37            }
38        }
39    };
40}
41
42#[cfg(test)]
43mod test {
44    use super::*;
45    use crate::config::StyleEdition;
46
47    #[test]
48    fn test_impl_default_style_edition_struct_for_all_editions() {
49        struct Unit;
50        style_edition_default!(Unit, usize, _ => 100);
51
52        // regardless of the style edition used the value will always return 100
53        assert_eq!(Unit::style_edition_default(StyleEdition::Edition2015), 100);
54        assert_eq!(Unit::style_edition_default(StyleEdition::Edition2018), 100);
55        assert_eq!(Unit::style_edition_default(StyleEdition::Edition2021), 100);
56        assert_eq!(Unit::style_edition_default(StyleEdition::Edition2024), 100);
57        assert_eq!(Unit::style_edition_default(StyleEdition::Edition2027), 100);
58    }
59
60    #[test]
61    fn test_impl_default_style_edition_for_old_and_new_editions() {
62        struct Unit;
63        style_edition_default!(Unit, usize, Edition2024 => 50, _ => 100);
64
65        // style edition 2015-2021 are all the same
66        assert_eq!(Unit::style_edition_default(StyleEdition::Edition2015), 100);
67        assert_eq!(Unit::style_edition_default(StyleEdition::Edition2018), 100);
68        assert_eq!(Unit::style_edition_default(StyleEdition::Edition2021), 100);
69
70        // style edition 2024
71        assert_eq!(Unit::style_edition_default(StyleEdition::Edition2024), 50);
72        assert_eq!(Unit::style_edition_default(StyleEdition::Edition2027), 50);
73    }
74}