src/config.rs
383 lines · 11823 bytes
1//! Configuration module
2//!
3//! This is the global plugin configuration.
4//!
5//! Configuration options should be split out into different sections:
6//!
7//! * network
8//! * look
9//! * color
10//! * server
11//!
12//! The server config options are added in the server.rs file.
13//!
14//! The config options created here will be alive as long as the plugin is
15//! loaded so they don't need to be freed manually. The drop implementation of
16//! the section will do so.
17use crate::{MatrixServer, Servers};
18use weechat::config::{
19 Conf, ConfigOption, ConfigSection, ConfigSectionSettings,
20 IntegerOptionSettings, OptionChanged, SectionHandle, SectionHandleMut,
21 SectionReadCallback, StringOptionSettings,
22};
23use weechat::Weechat;
24
25use strum::EnumVariantNames;
26use strum::VariantNames;
27
28use std::cell::{Ref, RefCell, RefMut};
29use std::ops::{Deref, DerefMut};
30use std::rc::Rc;
31
32#[derive(Clone)]
33pub struct ConfigHandle {
34 inner: Rc<RefCell<Config>>,
35 servers: Servers,
36}
37
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) => {
63 paste::item! {
64 fn [<create_option_ $option_name>](section: &mut SectionHandleMut) {
65 let option_name = stringify!($option_name);
66 let option_settings = StringOptionSettings::new(option_name)
67 .description($description)
68 .default_value($default);
69
70 section.new_string_option(option_settings)
71 .expect(&format!("Can't create option {}", option_name));
72 }
73 }
74 };
75}
76
77macro_rules! integer_create {
78 ($option_name:ident, $description:literal, $default:literal, $min:literal, $max:literal, [$($string_value:literal), *]) => {
79 paste::item! {
80 fn [<create_option_ $option_name>](section: &mut SectionHandleMut) {
81 let mut string_values: Vec<String> = Vec::new();
82
83 $(
84 string_values.push($string_value.into());
85 )*
86
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));
97 }
98 }
99 };
100}
101
102macro_rules! enum_create {
103 ($option_name:ident, $description:literal, $out_type:ty) => {
104 paste::item! {
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
114 let option_name = stringify!($option_name);
115 let option_settings = IntegerOptionSettings::new(option_name)
116 .description($description)
117 .default_value(default_value as i32)
118 .string_values(string_values);
119
120 section.new_integer_option(option_settings)
121 .expect(&format!("Can't create option {}", option_name));
122 }
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);
166
167 paste::item! {
168 pub fn [<$option_name>](&self) -> String {
169 let option_name = stringify!($option_name);
170
171 let option = self.0.search_option(option_name)
172 .expect(&format!("Couldn't find option {} in section {}",
173 option_name, self.0.name()));
174
175 if let ConfigOption::String(o) = option {
176 Weechat::eval_string_expression(&o.value())
177 .expect(&format!(
178 "Can't evaluate string expression for option {} in section {}",
179 option_name,
180 self.0.name())
181 )
182 } else {
183 panic!("Incorect option type for option {} in section {}",
184 option_name, self.0.name());
185 }
186 }
187 }
188 };
189}
190
191macro_rules! section {
192 ($section:ident { $($option_name:ident: $option_type:ident {$($option:tt)*}), * }) => {
193 paste::item! {
194 pub struct [<$section:camel Section>]<'a>(SectionHandle<'a>);
195
196 impl<'a> [<$section:camel Section>]<'a> {
197 pub fn create(config: &mut Config) {
198 let section_settings = ConfigSectionSettings::new(stringify!($section));
199
200 let mut $section = config.new_section(section_settings)
201 .expect(&format!("Can't create config section {}", stringify!($section)));
202
203 [<$section:camel Section>]::create_options(&mut $section);
204 }
205
206 pub fn create_options(section: &mut SectionHandleMut) {
207 $(
208 [<$section:camel Section>]::[<create_option_ $option_name>](section);
209 )*
210 }
211
212 $(
213 option!($option_type, $option_name, $($option)*);
214 )*
215 }
216 }
217 }
218}
219
220macro_rules! config {
221 ($(Section $section:ident { $($option:tt)* }), *) => {
222 pub struct Config(weechat::config::Config);
223
224 impl Deref for Config {
225 type Target = weechat::config::Config;
226
227 fn deref(&self) -> &Self::Target {
228 &self.0
229 }
230 }
231
232 impl DerefMut for Config {
233 fn deref_mut(&mut self) -> &mut Self::Target {
234 &mut self.0
235 }
236 }
237
238 impl Config {
239 fn new(config: weechat::config::Config) -> Self {
240 let mut config = Config(config);
241 config.create_sections();
242
243 config
244 }
245
246 paste::item! {
247 fn create_sections(&mut self) {
248 $(
249 paste::expr! { [<$section:camel Section>]::create(self) };
250 )*
251 }
252 }
253
254 paste::item! {
255 $(
256 pub fn $section(&self) -> [<$section:camel Section>] {
257 let section_name = stringify!($section);
258 let section = self.0.search_section(section_name)
259 .expect(&format!("Couldn't find section {}", section_name));
260
261 paste::item! { [<$section:camel Section>](section) }
262 }
263 )*
264 }
265 }
266
267 $(
268 section!($section { $($option)* });
269 )*
270 }
271}
272
273config!(
274 Section look {
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
294 }
295);
296
297impl ConfigHandle {
298 pub fn new(_weechat: &Weechat, servers: &Servers) -> ConfigHandle {
299 let config = Weechat::config_new("matrix-rust")
300 .expect("Can't create new config");
301
302 let config = Config::new(config);
303
304 let config = ConfigHandle {
305 inner: Rc::new(RefCell::new(config)),
306 servers: servers.clone(),
307 };
308
309 let server_section_options = ConfigSectionSettings::new("server")
310 .set_write_callback(
311 |_weechat: &Weechat,
312 config: &Conf,
313 section: &mut ConfigSection| {
314 config.write_section(section.name());
315 for option in section.options() {
316 config.write_option(option);
317 }
318 },
319 )
320 .set_read_callback(config.clone());
321
322 {
323 let mut config_borrow = config.borrow_mut();
324
325 config_borrow
326 .new_section(server_section_options)
327 .expect("Can't create server section");
328 }
329
330 config
331 }
332
333 pub fn borrow(&self) -> Ref<'_, Config> {
334 self.inner.borrow()
335 }
336
337 pub fn borrow_mut(&self) -> RefMut<'_, Config> {
338 self.inner.borrow_mut()
339 }
340}
341
342impl SectionReadCallback for ConfigHandle {
343 fn callback(
344 &mut self,
345 _: &Weechat,
346 _: &Conf,
347 section: &mut ConfigSection,
348 option_name: &str,
349 option_value: &str,
350 ) -> OptionChanged {
351 if option_name.is_empty() {
352 return OptionChanged::Error;
353 }
354
355 let option_args: Vec<&str> = option_name.splitn(2, '.').collect();
356
357 if option_args.len() != 2 {
358 return OptionChanged::Error;
359 }
360
361 let server_name = option_args[0];
362
363 {
364 let mut servers_borrow = self.servers.borrow_mut();
365
366 // We are reading the config, if the server doesn't yet exists
367 // we need to create it before setting the option and running
368 // the option change callback.
369 if !servers_borrow.contains_key(server_name) {
370 let server = MatrixServer::new(server_name, &self, section);
371 servers_borrow.insert(server_name.to_owned(), server);
372 }
373 }
374
375 let option = section.search_option(option_name);
376
377 if let Some(o) = option {
378 o.set(option_value, true)
379 } else {
380 OptionChanged::NotFound
381 }
382 }
383}