Commit 799862c106

799862c106a9a839c18fb9b09edd22a6bb9dac25

parent: 4026269346

Unsigned

Damir Jelić <poljar@termina.org.uk> · 2020-06-27 12:55 UTC

config: Add support to declare boolean options.

Layout: unified · split

src/config.rs +27 −2
@@ -18,7 +18,7 @@ use crate::{MatrixServer, Servers};
18use weechat::config::{ 18use weechat::config::{
19 Conf, ConfigOption, ConfigSection, ConfigSectionSettings, 19 Conf, ConfigOption, ConfigSection, ConfigSectionSettings,
20 IntegerOptionSettings, OptionChanged, SectionHandle, SectionHandleMut, 20 IntegerOptionSettings, OptionChanged, SectionHandle, SectionHandleMut,
21 SectionReadCallback, StringOptionSettings, 21 SectionReadCallback, StringOptionSettings, BooleanOptionSettings,
22}; 22};
23use weechat::Weechat; 23use weechat::Weechat;
24 24
@@ -74,6 +74,22 @@ macro_rules! string_create {
74 }; 74 };
75} 75}
76 76
77macro_rules! bool_create {
78 ($option_name:ident, $description:literal, $default:literal) => {
79 paste::item! {
80 fn [<create_option_ $option_name>](section: &mut SectionHandleMut) {
81 let option_name = stringify!($option_name);
82 let option_settings = BooleanOptionSettings::new(option_name)
83 .description($description)
84 .default_value($default);
85
86 section.new_boolean_option(option_settings)
87 .expect(&format!("Can't create option {}", option_name));
88 }
89 }
90 };
91}
92
77macro_rules! integer_create { 93macro_rules! integer_create {
78 ($option_name:ident, $description:literal, $default:literal, $min:literal, $max:literal, [$($string_value:literal), *]) => { 94 ($option_name:ident, $description:literal, $default:literal, $min:literal, $max:literal, [$($string_value:literal), *]) => {
79 paste::item! { 95 paste::item! {
@@ -150,6 +166,11 @@ macro_rules! option {
150 option_getter!(String, $option_name, String); 166 option_getter!(String, $option_name, String);
151 }; 167 };
152 168
169 (bool, $option_name:ident, $description:literal, $default:literal) => {
170 bool_create!($option_name, $description, $default);
171 option_getter!(Boolean, $option_name, bool);
172 };
173
153 (Integer, $option_name:ident, $description:literal, $default:literal, $min:literal, $max:literal) => { 174 (Integer, $option_name:ident, $description:literal, $default:literal, $min:literal, $max:literal) => {
154 integer_create!($option_name, $description, $default, $min, $max, []); 175 integer_create!($option_name, $description, $default, $min, $max, []);
155 option_getter!(Integer, $option_name, i64); 176 option_getter!(Integer, $option_name, i64);
@@ -288,8 +309,12 @@ config!(
288 enum_test: Enum { 309 enum_test: Enum {
289 "test", 310 "test",
290 Test 311 Test
291 } 312 },
292 313
314 bool_test: bool {
315 "description",
316 false
317 }
293 } 318 }
294); 319);
295 320