Commit 2ac6207b6e

2ac6207b6edda70e64a988b05a2e946ad0dd4c40

parent: 3969a986f5

Unsigned

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

config: Neater config declaration syntax and more option types.

Layout: unified · split

Cargo.lock +1
@@ -2326,6 +2326,7 @@ dependencies = [
2326 "libc", 2326 "libc",
2327 "matrix-sdk", 2327 "matrix-sdk",
2328 "paste", 2328 "paste",
2329 "strum",
2329 "tokio", 2330 "tokio",
2330 "tracing", 2331 "tracing",
2331 "tracing-subscriber", 2332 "tracing-subscriber",
Cargo.toml +1
@@ -19,6 +19,7 @@ async-std = { version = "1.6.2", features = [ "unstable" ] }
19clap = "2.33.1" 19clap = "2.33.1"
20async-trait = "0.1.36" 20async-trait = "0.1.36"
21paste = "0.1" 21paste = "0.1"
22strum = "0.18.0"
22uuid = { version = "0.8.1", features = ["v4"] } 23uuid = { version = "0.8.1", features = ["v4"] }
23 24
24weechat = { git = "https://github.com/poljar/rust-weechat", features = ["async-executor"] } 25weechat = { git = "https://github.com/poljar/rust-weechat", features = ["async-executor"] }
src/config.rs +132 −28
@@ -16,11 +16,15 @@
16//! the section will do so. 16//! the section will do so.
17use crate::{MatrixServer, Servers}; 17use crate::{MatrixServer, Servers};
18use weechat::config::{ 18use weechat::config::{
19 Conf, ConfigOption, ConfigSection, ConfigSectionSettings, OptionChanged, 19 Conf, ConfigOption, ConfigSection, ConfigSectionSettings,
20 SectionHandle, SectionHandleMut, SectionReadCallback, StringOptionSettings, 20 IntegerOptionSettings, OptionChanged, SectionHandle, SectionHandleMut,
21 SectionReadCallback, StringOptionSettings,
21}; 22};
22use weechat::Weechat; 23use weechat::Weechat;
23 24
25use strum::EnumVariantNames;
26use strum::VariantNames;
27
24use std::cell::{Ref, RefCell, RefMut}; 28use std::cell::{Ref, RefCell, RefMut};
25use std::ops::{Deref, DerefMut}; 29use std::ops::{Deref, DerefMut};
26use std::rc::Rc; 30use std::rc::Rc;
@@ -31,8 +35,31 @@ pub struct ConfigHandle {
31 servers: Servers, 35 servers: Servers,
32} 36}
33 37
34macro_rules! option { 38#[derive(EnumVariantNames)]
35 (StringOption, $option_name:ident, $description:literal, $default:literal) => { 39#[strum(serialize_all = "kebab_case")]
40pub enum Test {
41 First,
42 Second
43}
44
45impl Default for Test{
46 fn default() -> Self {
47 Test::First
48 }
49}
50
51impl From<i32> for Test {
52 fn from(value: i32) -> Self {
53 match value {
54 0 => Test::First,
55 1 => Test::Second,
56 _ => unreachable!()
57 }
58 }
59}
60
61macro_rules! string_create {
62 ($option_name:ident, $description:literal, $default:literal) => {
36 paste::item! { 63 paste::item! {
37 fn [<create_option_ $option_name>](section: &mut SectionHandleMut) { 64 fn [<create_option_ $option_name>](section: &mut SectionHandleMut) {
38 let option_name = stringify!($option_name); 65 let option_name = stringify!($option_name);
@@ -44,37 +71,98 @@ macro_rules! option {
44 .expect(&format!("Can't create option {}", option_name)); 71 .expect(&format!("Can't create option {}", option_name));
45 } 72 }
46 } 73 }
74 };
75}
47 76
77macro_rules! integer_create {
78 ($option_name:ident, $description:literal, $default:literal, $min:literal, $max:literal, [$($string_value:literal), *]) => {
48 paste::item! { 79 paste::item! {
49 pub fn [<$option_name>](&self) -> String { 80 fn [<create_option_ $option_name>](section: &mut SectionHandleMut) {
50 let option_name = stringify!($option_name); 81 let mut string_values: Vec<String> = Vec::new();
51 82
52 let option = self.0.search_option(option_name) 83 $(
53 .expect(&format!("Couldn't find option {} in section {}", 84 string_values.push($string_value.into());
54 option_name, self.0.name())); 85 )*
55 86
56 if let ConfigOption::String(o) = option { 87 let option_name = stringify!($option_name);
57 o.value().to_string() 88 let option_settings = IntegerOptionSettings::new(option_name)
58 } else { 89 .description($description)
59 panic!("Incorect option type for option {} in section {}", 90 .default_value($default)
60 option_name, self.0.name()); 91 .min($min)
61 } 92 .max($max)
93 .string_values(string_values);
94
95 section.new_integer_option(option_settings)
96 .expect(&format!("Can't create option {}", option_name));
62 } 97 }
63 } 98 }
64 }; 99 };
100}
65 101
66 (EvaluatedStringOption, $option_name:ident, $description:literal, $default:literal) => { 102macro_rules! enum_create {
103 ($option_name:ident, $description:literal, $out_type:ty) => {
67 paste::item! { 104 paste::item! {
68 fn [<create_option_ $option_name>](section: &mut SectionHandleMut) { 105 fn [<create_option_ $option_name>](section: &mut SectionHandleMut) {
106 let mut string_values: Vec<String> = Vec::new();
107
108 for value in $out_type::VARIANTS {
109 string_values.push(value.to_string());
110 }
111
112 let default_value = $out_type::default();
113
69 let option_name = stringify!($option_name); 114 let option_name = stringify!($option_name);
70 let option_settings = StringOptionSettings::new(option_name) 115 let option_settings = IntegerOptionSettings::new(option_name)
71 .description(&format!("{} (note: the content is evaluated, see /help eval)", $description)) 116 .description($description)
72 .default_value($default); 117 .default_value(default_value as i32)
118 .string_values(string_values);
73 119
74 section.new_string_option(option_settings) 120 section.new_integer_option(option_settings)
75 .expect(&format!("Can't create option {}", option_name)); 121 .expect(&format!("Can't create option {}", option_name));
76 } 122 }
77 } 123 }
124 };
125}
126
127
128macro_rules! option_getter {
129 ($option_type:ident, $option_name:ident, $output_type:ty) => {
130 paste::item! {
131 pub fn [<$option_name>](&self) -> $output_type {
132 let option_name = stringify!($option_name);
133
134 if let ConfigOption::[<$option_type>](o) = self.0.search_option(option_name)
135 .expect(&format!("Couldn't find option {} in section {}",
136 option_name, self.0.name()))
137 {
138 $output_type::from(o.value())
139 } else {
140 panic!("Incorect option type for option {} in section {}",
141 option_name, self.0.name());
142 }
143 }
144 }
145 };
146}
147
148macro_rules! option {
149 (String, $option_name:ident, $description:literal, $default:literal) => {
150 string_create!($option_name, $description, $default);
151 option_getter!(String, $option_name, String);
152 };
153
154 (Integer, $option_name:ident, $description:literal, $default:literal, $min:literal, $max:literal) => {
155 integer_create!($option_name, $description, $default, $min, $max, []);
156 option_getter!(Integer, $option_name, i64);
157 };
158
159 (Enum, $option_name:ident, $description:literal, $out_type:ty) => {
160 enum_create!($option_name, $description, $out_type);
161 option_getter!(Integer, $option_name, $out_type);
162 };
163
164 (EvaluatedString, $option_name:ident, $description:literal, $default:literal) => {
165 string_create!($option_name, $description, $default);
78 166
79 paste::item! { 167 paste::item! {
80 pub fn [<$option_name>](&self) -> String { 168 pub fn [<$option_name>](&self) -> String {
@@ -97,11 +185,11 @@ macro_rules! option {
97 } 185 }
98 } 186 }
99 } 187 }
100 }; 188 };
101} 189}
102 190
103macro_rules! section { 191macro_rules! section {
104 ($section:ident { $({$option_type:ident, $option_name:ident, $($option:tt)*}), * }) => { 192 ($section:ident { $($option_name:ident: $option_type:ident {$($option:tt)*}), * }) => {
105 paste::item! { 193 paste::item! {
106 pub struct [<$section:camel Section>]<'a>(SectionHandle<'a>); 194 pub struct [<$section:camel Section>]<'a>(SectionHandle<'a>);
107 195
@@ -130,7 +218,7 @@ macro_rules! section {
130} 218}
131 219
132macro_rules! config { 220macro_rules! config {
133 ($(Section $section:ident { $({$($option:tt)*}), * }), *) => { 221 ($(Section $section:ident { $($option:tt)* }), *) => {
134 pub struct Config(weechat::config::Config); 222 pub struct Config(weechat::config::Config);
135 223
136 impl Deref for Config { 224 impl Deref for Config {
@@ -177,16 +265,32 @@ macro_rules! config {
177 } 265 }
178 266
179 $( 267 $(
180 section!($section { $({$($option)*}), * }); 268 section!($section { $($option)* });
181 )* 269 )*
182 } 270 }
183} 271}
184 272
185config!( 273config!(
186 Section look { 274 Section look {
187 {StringOption, encrypted_room_sign, 275 encrypted_room_sign: String {
188 "A sign that is used to show that the current room is encrypted", 276 "A sign that is used to show that the current room is encrypted",
189 "🔒"} 277 "🔒"
278 },
279 what: EvaluatedString {
280 "test",
281 "test2"
282 },
283 int_test: Integer {
284 "test",
285 5,
286 0,
287 10
288 },
289 enum_test: Enum {
290 "test",
291 Test
292 }
293
190 } 294 }
191); 295);
192 296