Commit c4d1d79629
Unsigned
Layout: unified · split
src/config.rs +1 −5
| @@ -16,14 +16,10 @@ | ||
| 16 | 16 | //! the section will do so. |
| 17 | 17 | |
| 18 | 18 | use crate::{MatrixServer, Servers}; |
| 19 | use weechat::config::{ | |
| 20 | Conf, ConfigOption, ConfigSection, ConfigSectionSettings, OptionChanged, | |
| 21 | SectionHandle, SectionHandleMut, SectionReadCallback, StringOptionSettings, | |
| 22 | }; | |
| 19 | use weechat::config::{Conf, OptionChanged, SectionReadCallback}; | |
| 23 | 20 | use weechat::Weechat; |
| 24 | 21 | |
| 25 | 22 | use std::cell::{Ref, RefCell, RefMut}; |
| 26 | use std::ops::{Deref, DerefMut}; | |
| 27 | 23 | use std::rc::Rc; |
| 28 | 24 | |
| 29 | 25 | config!( |
src/config_macros.rs +39 −93
| @@ -1,95 +1,35 @@ | ||
| 1 | #[allow(unused_macros)] | |
| 2 | macro_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)] | |
| 19 | macro_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 | } | |
| 1 | macro_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) | |
| 31 | 7 | } |
| 32 | 8 | }; |
| 33 | } | |
| 34 | ||
| 35 | #[allow(unused_macros)] | |
| 36 | macro_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) | |
| 49 | 15 | }; |
| 50 | } | |
| 51 | ||
| 52 | #[allow(unused_macros)] | |
| 53 | macro_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>>()); | |
| 64 | 21 | |
| 65 | section.new_integer_option(option_settings) | |
| 66 | .expect(&format!("Can't create option {}", option_name)); | |
| 67 | } | |
| 68 | } | |
| 69 | 22 | }; |
| 70 | 23 | } |
| 71 | 24 | |
| 72 | 25 | #[allow(unused_macros)] |
| 73 | macro_rules! enum_create { | |
| 74 | ($option_name:ident, $description:literal, $out_type:ty) => { | |
| 26 | macro_rules! option_create { | |
| 27 | ($option_type:ident, $option_weechat_type:ident, $option_name:ident, $($args:tt)*) => { | |
| 75 | 28 | paste::item! { |
| 76 | 29 | 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))); | |
| 93 | 33 | } |
| 94 | 34 | } |
| 95 | 35 | }; |
| @@ -116,33 +56,33 @@ macro_rules! option_getter { | ||
| 116 | 56 | } |
| 117 | 57 | |
| 118 | 58 | macro_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)*); | |
| 121 | 61 | option_getter!(String, $option_name, String); |
| 122 | 62 | }; |
| 123 | 63 | |
| 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)*); | |
| 126 | 66 | option_getter!(Color, $option_name, String); |
| 127 | 67 | }; |
| 128 | 68 | |
| 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)*); | |
| 131 | 71 | option_getter!(Boolean, $option_name, bool); |
| 132 | 72 | }; |
| 133 | 73 | |
| 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)*); | |
| 136 | 76 | option_getter!(Integer, $option_name, i64); |
| 137 | 77 | }; |
| 138 | 78 | |
| 139 | 79 | (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); | |
| 141 | 81 | option_getter!(Integer, $option_name, $out_type); |
| 142 | 82 | }; |
| 143 | 83 | |
| 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)*); | |
| 146 | 86 | |
| 147 | 87 | paste::item! { |
| 148 | 88 | pub fn [<$option_name>](&self) -> String { |
| @@ -310,6 +250,12 @@ macro_rules! config { | ||
| 310 | 250 | ($config_name:literal, $(Section $section:ident { $($option:tt)* }), * $(,)?) => { |
| 311 | 251 | #[allow(unused_imports)] |
| 312 | 252 | 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 | }; | |
| 313 | 259 | pub struct Config(weechat::config::Config); |
| 314 | 260 | |
| 315 | 261 | impl Deref for Config { |