config_docs/
config_docs.rs

1/// Trait that allows for documentation printing
2pub trait ConfigDocs {
3    /// Returns the fields in your struct with their assiciated documentation
4    fn config_docs() -> &'static [(&'static str, &'static str)];
5}
6
7macro_rules! default_impl_for_types {
8    ($($t:ty),*) => {
9        $(
10            impl ConfigDocs for $t {
11                fn config_docs() -> &'static [(&'static str, &'static str)] {
12                    &[]
13                }
14            }
15        )*
16    };
17}
18
19// Implement MyTrait for all integers and floating-point types
20default_impl_for_types!(
21    i8, i16, i32, i64, i128, isize, u8, u16, u32, u64, u128, usize, f32, f64, String, &str
22);
23
24#[cfg(test)]
25mod test {
26    use super::*;
27    use config_docs_macros::ConfigDocs;
28
29    #[test]
30    fn it_parses_structs() {
31        #[allow(unused)]
32        #[derive(ConfigDocs)]
33        struct ThemeConfig {
34            /// Foreground color used in the text as a hex color
35            ///
36            /// # Example:
37            /// `fg = "#7f00ff"`
38            fg: String,
39            /// Background color used in the text as a hex color
40            ///
41            /// # Example:
42            /// `bg = "#7f00ff"`
43            bg: String,
44        }
45
46        assert_eq!(
47            ThemeConfig::config_docs(),
48            &[
49                ("fg", "Foreground color used in the text as a hex color"),
50                ("bg", "Background color used in the text as a hex color"),
51            ]
52        )
53    }
54
55    #[test]
56    fn it_parses_nested_structs() {
57        #[derive(ConfigDocs)]
58        #[allow(unused)]
59        struct Config {
60            /// Sets the colors used throughout the app
61            pub theme: ThemeConfig,
62            /// Sets the keybinds used throughout the app
63            pub keybinds: KeybindsConfig,
64        }
65        #[derive(ConfigDocs)]
66        #[allow(unused)]
67        struct ThemeConfig {
68            /// Foreground color used in the text as a hex color
69            ///
70            /// # Example:
71            /// `fg = "#7f00ff"`
72            fg: String,
73            /// Background color used in the text as a hex color
74            ///
75            /// # Example:
76            /// `bg = "#7f00ff"`
77            bg: String,
78        }
79        #[derive(ConfigDocs)]
80        #[allow(unused)]
81        struct KeybindsConfig {
82            /// show help
83            help: String,
84            /// quit app
85            quit: String,
86        }
87
88        assert_eq!(
89            Config::config_docs(),
90            &[
91                ("theme", "Sets the colors used throughout the app"),
92                ("fg", "Foreground color used in the text as a hex color"),
93                ("bg", "Background color used in the text as a hex color"),
94                ("keybinds", "Sets the keybinds used throughout the app"),
95                ("help", "show help"),
96                ("quit", "quit app")
97            ]
98        )
99    }
100}