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 = [
23262326 "libc",
23272327 "matrix-sdk",
23282328 "paste",
2329 "strum",
23292330 "tokio",
23302331 "tracing",
23312332 "tracing-subscriber",
Cargo.toml +1
@@ -19,6 +19,7 @@ async-std = { version = "1.6.2", features = [ "unstable" ] }
1919clap = "2.33.1"
2020async-trait = "0.1.36"
2121paste = "0.1"
22strum = "0.18.0"
2223uuid = { version = "0.8.1", features = ["v4"] }
2324
2425weechat = { git = "https://github.com/poljar/rust-weechat", features = ["async-executor"] }
src/config.rs +132 −28
@@ -16,11 +16,15 @@
1616//! the section will do so.
1717use crate::{MatrixServer, Servers};
1818use weechat::config::{
19 Conf, ConfigOption, ConfigSection, ConfigSectionSettings, OptionChanged,
20 SectionHandle, SectionHandleMut, SectionReadCallback, StringOptionSettings,
19 Conf, ConfigOption, ConfigSection, ConfigSectionSettings,
20 IntegerOptionSettings, OptionChanged, SectionHandle, SectionHandleMut,
21 SectionReadCallback, StringOptionSettings,
2122};
2223use weechat::Weechat;
2324
25use strum::EnumVariantNames;
26use strum::VariantNames;
27
2428use std::cell::{Ref, RefCell, RefMut};
2529use std::ops::{Deref, DerefMut};
2630use std::rc::Rc;
@@ -31,8 +35,31 @@ pub struct ConfigHandle {
3135 servers: Servers,
3236}
3337
34macro_rules! option {
35 (StringOption, $option_name:ident, $description:literal, $default:literal) => {
38#[derive(EnumVariantNames)]
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) => {
3663 paste::item! {
3764 fn [<create_option_ $option_name>](section: &mut SectionHandleMut) {
3865 let option_name = stringify!($option_name);
@@ -44,37 +71,98 @@ macro_rules! option {
4471 .expect(&format!("Can't create option {}", option_name));
4572 }
4673 }
74 };
75}
4776
77macro_rules! integer_create {
78 ($option_name:ident, $description:literal, $default:literal, $min:literal, $max:literal, [$($string_value:literal), *]) => {
4879 paste::item! {
49 pub fn [<$option_name>](&self) -> String {
50 let option_name = stringify!($option_name);
80 fn [<create_option_ $option_name>](section: &mut SectionHandleMut) {
81 let mut string_values: Vec<String> = Vec::new();
5182
52 let option = self.0.search_option(option_name)
53 .expect(&format!("Couldn't find option {} in section {}",
54 option_name, self.0.name()));
83 $(
84 string_values.push($string_value.into());
85 )*
5586
56 if let ConfigOption::String(o) = option {
57 o.value().to_string()
58 } else {
59 panic!("Incorect option type for option {} in section {}",
60 option_name, self.0.name());
61 }
87 let option_name = stringify!($option_name);
88 let option_settings = IntegerOptionSettings::new(option_name)
89 .description($description)
90 .default_value($default)
91 .min($min)
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));
6297 }
6398 }
64 };
99 };
100}
65101
66 (EvaluatedStringOption, $option_name:ident, $description:literal, $default:literal) => {
102macro_rules! enum_create {
103 ($option_name:ident, $description:literal, $out_type:ty) => {
67104 paste::item! {
68105 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
69114 let option_name = stringify!($option_name);
70 let option_settings = StringOptionSettings::new(option_name)
71 .description(&format!("{} (note: the content is evaluated, see /help eval)", $description))
72 .default_value($default);
115 let option_settings = IntegerOptionSettings::new(option_name)
116 .description($description)
117 .default_value(default_value as i32)
118 .string_values(string_values);
73119
74 section.new_string_option(option_settings)
120 section.new_integer_option(option_settings)
75121 .expect(&format!("Can't create option {}", option_name));
76122 }
77123 }
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);
78166
79167 paste::item! {
80168 pub fn [<$option_name>](&self) -> String {
@@ -97,11 +185,11 @@ macro_rules! option {
97185 }
98186 }
99187 }
100 };
188 };
101189}
102190
103191macro_rules! section {
104 ($section:ident { $({$option_type:ident, $option_name:ident, $($option:tt)*}), * }) => {
192 ($section:ident { $($option_name:ident: $option_type:ident {$($option:tt)*}), * }) => {
105193 paste::item! {
106194 pub struct [<$section:camel Section>]<'a>(SectionHandle<'a>);
107195
@@ -130,7 +218,7 @@ macro_rules! section {
130218}
131219
132220macro_rules! config {
133 ($(Section $section:ident { $({$($option:tt)*}), * }), *) => {
221 ($(Section $section:ident { $($option:tt)* }), *) => {
134222 pub struct Config(weechat::config::Config);
135223
136224 impl Deref for Config {
@@ -177,16 +265,32 @@ macro_rules! config {
177265 }
178266
179267 $(
180 section!($section { $({$($option)*}), * });
268 section!($section { $($option)* });
181269 )*
182270 }
183271}
184272
185273config!(
186274 Section look {
187 {StringOption, encrypted_room_sign,
188 "A sign that is used to show that the current room is encrypted",
189 "🔒"}
275 encrypted_room_sign: String {
276 "A sign that is used to show that the current room is encrypted",
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
190294 }
191295);
192296