1/// Trait that allows for documentation printing
2pub trait ConfigDocs {
3/// Returns the fields in your struct with their assiciated documentation
4fn config_docs() -> &'static [(&'static str, &'static str)];
5}
67macro_rules! default_impl_for_types {
8 ($($t:ty),*) => {
9 $(
10impl ConfigDocs for $t {
11fn config_docs() -> &'static [(&'static str, &'static str)] {
12&[]
13 }
14 }
15 )*
16 };
17}
1819// 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);
2324#[cfg(test)]
25mod test {
26use super::*;
27use config_docs_macros::ConfigDocs;
2829#[test]
30fn it_parses_structs() {
31#[allow(unused)]
32 #[derive(ConfigDocs)]
33struct ThemeConfig {
34/// Foreground color used in the text as a hex color
35 ///
36 /// # Example:
37 /// `fg = "#7f00ff"`
38fg: String,
39/// Background color used in the text as a hex color
40 ///
41 /// # Example:
42 /// `bg = "#7f00ff"`
43bg: String,
44 }
4546assert_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 }
5455#[test]
56fn it_parses_nested_structs() {
57#[derive(ConfigDocs)]
58 #[allow(unused)]
59struct Config {
60/// Sets the colors used throughout the app
61pub theme: ThemeConfig,
62/// Sets the keybinds used throughout the app
63pub keybinds: KeybindsConfig,
64 }
65#[derive(ConfigDocs)]
66 #[allow(unused)]
67struct ThemeConfig {
68/// Foreground color used in the text as a hex color
69 ///
70 /// # Example:
71 /// `fg = "#7f00ff"`
72fg: String,
73/// Background color used in the text as a hex color
74 ///
75 /// # Example:
76 /// `bg = "#7f00ff"`
77bg: String,
78 }
79#[derive(ConfigDocs)]
80 #[allow(unused)]
81struct KeybindsConfig {
82/// show help
83help: String,
84/// quit app
85quit: String,
86 }
8788assert_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}