| @@ -16,11 +16,15 @@ |
| 16 | 16 | //! the section will do so. |
| 17 | 17 | use crate::{MatrixServer, Servers}; |
| 18 | 18 | use 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, |
| 21 | 22 | }; |
| 22 | 23 | use weechat::Weechat; |
| 23 | 24 | |
| 25 | use strum::EnumVariantNames; |
| 26 | use strum::VariantNames; |
| 27 | |
| 24 | 28 | use std::cell::{Ref, RefCell, RefMut}; |
| 25 | 29 | use std::ops::{Deref, DerefMut}; |
| 26 | 30 | use std::rc::Rc; |
| @@ -31,8 +35,31 @@ pub struct ConfigHandle { |
| 31 | 35 | servers: Servers, |
| 32 | 36 | } |
| 33 | 37 | |
| 34 | | macro_rules! option { |
| 35 | | (StringOption, $option_name:ident, $description:literal, $default:literal) => { |
| 38 | #[derive(EnumVariantNames)] |
| 39 | #[strum(serialize_all = "kebab_case")] |
| 40 | pub enum Test { |
| 41 | First, |
| 42 | Second |
| 43 | } |
| 44 | |
| 45 | impl Default for Test{ |
| 46 | fn default() -> Self { |
| 47 | Test::First |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | impl 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 | |
| 61 | macro_rules! string_create { |
| 62 | ($option_name:ident, $description:literal, $default:literal) => { |
| 36 | 63 | paste::item! { |
| 37 | 64 | fn [<create_option_ $option_name>](section: &mut SectionHandleMut) { |
| 38 | 65 | let option_name = stringify!($option_name); |
| @@ -44,37 +71,98 @@ macro_rules! option { |
| 44 | 71 | .expect(&format!("Can't create option {}", option_name)); |
| 45 | 72 | } |
| 46 | 73 | } |
| 74 | }; |
| 75 | } |
| 47 | 76 | |
| 77 | macro_rules! integer_create { |
| 78 | ($option_name:ident, $description:literal, $default:literal, $min:literal, $max:literal, [$($string_value:literal), *]) => { |
| 48 | 79 | 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(); |
| 51 | 82 | |
| 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 | )* |
| 55 | 86 | |
| 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)); |
| 62 | 97 | } |
| 63 | 98 | } |
| 64 | | }; |
| 99 | }; |
| 100 | } |
| 65 | 101 | |
| 66 | | (EvaluatedStringOption, $option_name:ident, $description:literal, $default:literal) => { |
| 102 | macro_rules! enum_create { |
| 103 | ($option_name:ident, $description:literal, $out_type:ty) => { |
| 67 | 104 | paste::item! { |
| 68 | 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 | 114 | 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); |
| 73 | 119 | |
| 74 | | section.new_string_option(option_settings) |
| 120 | section.new_integer_option(option_settings) |
| 75 | 121 | .expect(&format!("Can't create option {}", option_name)); |
| 76 | 122 | } |
| 77 | 123 | } |
| 124 | }; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | macro_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 | |
| 148 | macro_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 | 167 | paste::item! { |
| 80 | 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 | |
| 103 | 191 | macro_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 | 193 | paste::item! { |
| 106 | 194 | pub struct [<$section:camel Section>]<'a>(SectionHandle<'a>); |
| 107 | 195 | |
| @@ -130,7 +218,7 @@ macro_rules! section { |
| 130 | 218 | } |
| 131 | 219 | |
| 132 | 220 | macro_rules! config { |
| 133 | | ($(Section $section:ident { $({$($option:tt)*}), * }), *) => { |
| 221 | ($(Section $section:ident { $($option:tt)* }), *) => { |
| 134 | 222 | pub struct Config(weechat::config::Config); |
| 135 | 223 | |
| 136 | 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 | |
| 185 | 273 | config!( |
| 186 | 274 | 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 | |
| 190 | 294 | } |
| 191 | 295 | ); |
| 192 | 296 | |