Commit c4d1d79629

c4d1d79629c8ca115252837de84f1dd28a3c6e94

parent: 7f35acd23c

Unsigned

Damir Jelić <poljar@termina.org.uk> · 2020-07-01 15:45 UTC

config_macros: A bit of cleanup and polish for the config macro.

Layout: unified · split

src/config.rs +1 −5
@@ -16,14 +16,10 @@
1616//! the section will do so.
1717
1818use crate::{MatrixServer, Servers};
19use weechat::config::{
20 Conf, ConfigOption, ConfigSection, ConfigSectionSettings, OptionChanged,
21 SectionHandle, SectionHandleMut, SectionReadCallback, StringOptionSettings,
22};
19use weechat::config::{Conf, OptionChanged, SectionReadCallback};
2320use weechat::Weechat;
2421
2522use std::cell::{Ref, RefCell, RefMut};
26use std::ops::{Deref, DerefMut};
2723use std::rc::Rc;
2824
2925config!(
src/config_macros.rs +39 −93
@@ -1,95 +1,35 @@
1#[allow(unused_macros)]
2macro_rules! string_create {
3 ($option_name:ident, $description:literal, $default:literal) => {
4 paste::item! {
5 fn [<create_option_ $option_name>](section: &mut SectionHandleMut) {
6 let option_name = stringify!($option_name);
7 let option_settings = StringOptionSettings::new(option_name)
8 .description($description)
9 .default_value($default);
10
11 section.new_string_option(option_settings)
12 .expect(&format!("Can't create option {}", option_name));
13 }
14 }
15 };
16}
17
18#[allow(unused_macros)]
19macro_rules! color_create {
20 ($option_name:ident, $description:literal, $default:literal) => {
21 paste::item! {
22 fn [<create_option_ $option_name>](section: &mut SectionHandleMut) {
23 let option_name = stringify!($option_name);
24 let option_settings = ColorOptionSettings::new(option_name)
25 .description($description)
26 .default_value($default);
27
28 section.new_color_option(option_settings)
29 .expect(&format!("Can't create option {}", option_name));
30 }
1macro_rules! option_settings {
2 ($option_type:ident, $option_name:ident, $description:literal, $default:literal $(,)?) => {
3 paste::expr! {
4 [<$option_type OptionSettings>]::new(stringify!($option_name))
5 .description($description)
6 .default_value($default)
317 }
328 };
33}
34
35#[allow(unused_macros)]
36macro_rules! bool_create {
37 ($option_name:ident, $description:literal, $default:literal) => {
38 paste::item! {
39 fn [<create_option_ $option_name>](section: &mut SectionHandleMut) {
40 let option_name = stringify!($option_name);
41 let option_settings = BooleanOptionSettings::new(option_name)
42 .description($description)
43 .default_value($default);
44
45 section.new_boolean_option(option_settings)
46 .expect(&format!("Can't create option {}", option_name));
47 }
48 }
9 (Integer, $option_name:ident, $description:literal, $default:literal, $min:literal..$max:literal $(,)?) => {
10 IntegerOptionSettings::new(stringify!($option_name))
11 .description($description)
12 .default_value($default)
13 .min($min)
14 .max($max)
4915 };
50}
51
52#[allow(unused_macros)]
53macro_rules! integer_create {
54 ($option_name:ident, $description:literal, $default:literal, $min:literal, $max:literal) => {
55 paste::item! {
56 fn [<create_option_ $option_name>](section: &mut SectionHandleMut) {
57 let option_name = stringify!($option_name);
58
59 let option_settings = IntegerOptionSettings::new(option_name)
60 .description($description)
61 .default_value($default)
62 .min($min)
63 .max($max);
16 (Enum, $option_name:ident, $description:literal, $out_type:ty $(,)?) => {
17 IntegerOptionSettings::new(stringify!($option_name))
18 .description($description)
19 .default_value(<$out_type>::default() as i32)
20 .string_values(<$out_type>::VARIANTS.iter().map(|v| v.to_string()).collect::<Vec<String>>());
6421
65 section.new_integer_option(option_settings)
66 .expect(&format!("Can't create option {}", option_name));
67 }
68 }
6922 };
7023}
7124
7225#[allow(unused_macros)]
73macro_rules! enum_create {
74 ($option_name:ident, $description:literal, $out_type:ty) => {
26macro_rules! option_create {
27 ($option_type:ident, $option_weechat_type:ident, $option_name:ident, $($args:tt)*) => {
7528 paste::item! {
7629 fn [<create_option_ $option_name>](section: &mut SectionHandleMut) {
77 let mut string_values: Vec<String> = Vec::new();
78
79 for value in $out_type::VARIANTS {
80 string_values.push(value.to_string());
81 }
82
83 let default_value = $out_type::default();
84
85 let option_name = stringify!($option_name);
86 let option_settings = IntegerOptionSettings::new(option_name)
87 .description($description)
88 .default_value(default_value as i32)
89 .string_values(string_values);
90
91 section.new_integer_option(option_settings)
92 .expect(&format!("Can't create option {}", option_name));
30 let option_settings = option_settings!($option_type, $option_name, $($args)*);
31 section.[<new_ $option_weechat_type:lower _option>](option_settings)
32 .expect(&format!("Can't create option {}", stringify!($option_name)));
9333 }
9434 }
9535 };
@@ -116,33 +56,33 @@ macro_rules! option_getter {
11656}
11757
11858macro_rules! option {
119 (String, $option_name:ident, $description:literal, $default:literal $(,)?) => {
120 string_create!($option_name, $description, $default);
59 (String, $option_name:ident, $($args:tt)*) => {
60 option_create!(String, String, $option_name, $($args)*);
12161 option_getter!(String, $option_name, String);
12262 };
12363
124 (Color, $option_name:ident, $description:literal, $default:literal $(,)?) => {
125 color_create!($option_name, $description, $default);
64 (Color, $option_name:ident, $($args:tt)*) => {
65 option_create!(Color, Color, $option_name, $($args)*);
12666 option_getter!(Color, $option_name, String);
12767 };
12868
129 (bool, $option_name:ident, $description:literal, $default:literal $(,)?) => {
130 bool_create!($option_name, $description, $default);
69 (bool, $option_name:ident, $($args:tt)*) => {
70 option_create!(Boolean, Boolean, $option_name, $($args)*);
13171 option_getter!(Boolean, $option_name, bool);
13272 };
13373
134 (Integer, $option_name:ident, $description:literal, $default:literal, $min:literal..$max:literal $(,)?) => {
135 integer_create!($option_name, $description, $default, $min, $max);
74 (Integer, $option_name:ident, $($args:tt)*) => {
75 option_create!(Integer, Integer, $option_name, $($args)*);
13676 option_getter!(Integer, $option_name, i64);
13777 };
13878
13979 (Enum, $option_name:ident, $description:literal, $out_type:ty $(,)?) => {
140 enum_create!($option_name, $description, $out_type);
80 option_create!(Enum, Integer, $option_name, $description, $out_type);
14181 option_getter!(Integer, $option_name, $out_type);
14282 };
14383
144 (EvaluatedString, $option_name:ident, $description:literal, $default:literal $(,)?) => {
145 string_create!($option_name, $description, $default);
84 (EvaluatedString, $option_name:ident, $($args:tt)*) => {
85 option_create!(String, String, $option_name, $($args)*);
14686
14787 paste::item! {
14888 pub fn [<$option_name>](&self) -> String {
@@ -310,6 +250,12 @@ macro_rules! config {
310250 ($config_name:literal, $(Section $section:ident { $($option:tt)* }), * $(,)?) => {
311251 #[allow(unused_imports)]
312252 use strum::VariantNames;
253 use std::ops::{Deref, DerefMut};
254 use weechat::config::{
255 SectionHandle, SectionHandleMut, StringOptionSettings,
256 ConfigOption, ConfigSection, ConfigSectionSettings,
257 BooleanOptionSettings, IntegerOptionSettings, ColorOptionSettings,
258 };
313259 pub struct Config(weechat::config::Config);
314260
315261 impl Deref for Config {