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                    $crate::config::StyleEdition::Edition2024 => $default_2024,
34                }
35            }
36        }
37    };
38}
39
40#[cfg(test)]
41mod test {
42    use super::*;
43    use crate::config::StyleEdition;
44
45    #[test]
46    fn test_impl_default_style_edition_struct_for_all_editions() {
47        struct Unit;
48        style_edition_default!(Unit, usize, _ => 100);
49
50        // regardless of the style edition used the value will always return 100
51        assert_eq!(Unit::style_edition_default(StyleEdition::Edition2015), 100);
52        assert_eq!(Unit::style_edition_default(StyleEdition::Edition2018), 100);
53        assert_eq!(Unit::style_edition_default(StyleEdition::Edition2021), 100);
54        assert_eq!(Unit::style_edition_default(StyleEdition::Edition2024), 100);
55    }
56
57    #[test]
58    fn test_impl_default_style_edition_for_old_and_new_editions() {
59        struct Unit;
60        style_edition_default!(Unit, usize, Edition2024 => 50, _ => 100);
61
62        // style edition 2015-2021 are all the same
63        assert_eq!(Unit::style_edition_default(StyleEdition::Edition2015), 100);
64        assert_eq!(Unit::style_edition_default(StyleEdition::Edition2018), 100);
65        assert_eq!(Unit::style_edition_default(StyleEdition::Edition2021), 100);
66
67        // style edition 2024
68        assert_eq!(Unit::style_edition_default(StyleEdition::Edition2024), 50);
69    }
70}