Commit d53278181b
Unsigned
Layout: unified · split
Makefile +1 −1
| @@ -3,7 +3,7 @@ PREFIX ?= $(WEECHAT_HOME) | |||
| 3 | 3 | ||
| 4 | .PHONY: install install-dir phony lint | 4 | .PHONY: install install-dir phony lint |
| 5 | 5 | ||
| 6 | target/debug/libmatrix.so: src/lib.rs src/server.rs src/room_buffer.rs src/commands.rs src/config.rs | 6 | target/debug/libmatrix.so: src/lib.rs src/server.rs src/room_buffer.rs src/commands.rs src/config.rs src/config_macros.rs |
| 7 | cargo build | 7 | cargo build |
| 8 | 8 | ||
| 9 | install: install-dir target/debug/libmatrix.so | 9 | install: install-dir target/debug/libmatrix.so |
src/config.rs −247
| @@ -58,253 +58,6 @@ impl From<i32> for Test { | |||
| 58 | } | 58 | } |
| 59 | } | 59 | } |
| 60 | 60 | ||
| 61 | macro_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 | |||
| 77 | macro_rules! color_create { | ||
| 78 | ($option_name:ident, $description:literal, $default:literal) => { | ||
| 79 | paste::item! { | ||
| 80 | fn [<create_option_ $option_name>](section: &mut SectionHandleMut) { | ||
| 81 | let option_name = stringify!($option_name); | ||
| 82 | let option_settings = ColorOptionSettings::new(option_name) | ||
| 83 | .description($description) | ||
| 84 | .default_value($default); | ||
| 85 | |||
| 86 | section.new_color_option(option_settings) | ||
| 87 | .expect(&format!("Can't create option {}", option_name)); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | }; | ||
| 91 | } | ||
| 92 | |||
| 93 | macro_rules! bool_create { | ||
| 94 | ($option_name:ident, $description:literal, $default:literal) => { | ||
| 95 | paste::item! { | ||
| 96 | fn [<create_option_ $option_name>](section: &mut SectionHandleMut) { | ||
| 97 | let option_name = stringify!($option_name); | ||
| 98 | let option_settings = BooleanOptionSettings::new(option_name) | ||
| 99 | .description($description) | ||
| 100 | .default_value($default); | ||
| 101 | |||
| 102 | section.new_boolean_option(option_settings) | ||
| 103 | .expect(&format!("Can't create option {}", option_name)); | ||
| 104 | } | ||
| 105 | } | ||
| 106 | }; | ||
| 107 | } | ||
| 108 | |||
| 109 | macro_rules! integer_create { | ||
| 110 | ($option_name:ident, $description:literal, $default:literal, $min:literal, $max:literal) => { | ||
| 111 | paste::item! { | ||
| 112 | fn [<create_option_ $option_name>](section: &mut SectionHandleMut) { | ||
| 113 | let option_name = stringify!($option_name); | ||
| 114 | |||
| 115 | let option_settings = IntegerOptionSettings::new(option_name) | ||
| 116 | .description($description) | ||
| 117 | .default_value($default) | ||
| 118 | .min($min) | ||
| 119 | .max($max); | ||
| 120 | |||
| 121 | section.new_integer_option(option_settings) | ||
| 122 | .expect(&format!("Can't create option {}", option_name)); | ||
| 123 | } | ||
| 124 | } | ||
| 125 | }; | ||
| 126 | } | ||
| 127 | |||
| 128 | macro_rules! enum_create { | ||
| 129 | ($option_name:ident, $description:literal, $out_type:ty) => { | ||
| 130 | paste::item! { | ||
| 131 | fn [<create_option_ $option_name>](section: &mut SectionHandleMut) { | ||
| 132 | let mut string_values: Vec<String> = Vec::new(); | ||
| 133 | |||
| 134 | for value in $out_type::VARIANTS { | ||
| 135 | string_values.push(value.to_string()); | ||
| 136 | } | ||
| 137 | |||
| 138 | let default_value = $out_type::default(); | ||
| 139 | |||
| 140 | let option_name = stringify!($option_name); | ||
| 141 | let option_settings = IntegerOptionSettings::new(option_name) | ||
| 142 | .description($description) | ||
| 143 | .default_value(default_value as i32) | ||
| 144 | .string_values(string_values); | ||
| 145 | |||
| 146 | section.new_integer_option(option_settings) | ||
| 147 | .expect(&format!("Can't create option {}", option_name)); | ||
| 148 | } | ||
| 149 | } | ||
| 150 | }; | ||
| 151 | } | ||
| 152 | |||
| 153 | macro_rules! option_getter { | ||
| 154 | ($option_type:ident, $option_name:ident, $output_type:ty) => { | ||
| 155 | paste::item! { | ||
| 156 | pub fn [<$option_name>](&self) -> $output_type { | ||
| 157 | let option_name = stringify!($option_name); | ||
| 158 | |||
| 159 | if let ConfigOption::[<$option_type>](o) = self.0.search_option(option_name) | ||
| 160 | .expect(&format!("Couldn't find option {} in section {}", | ||
| 161 | option_name, self.0.name())) | ||
| 162 | { | ||
| 163 | $output_type::from(o.value()) | ||
| 164 | } else { | ||
| 165 | panic!("Incorect option type for option {} in section {}", | ||
| 166 | option_name, self.0.name()); | ||
| 167 | } | ||
| 168 | } | ||
| 169 | } | ||
| 170 | }; | ||
| 171 | } | ||
| 172 | |||
| 173 | macro_rules! option { | ||
| 174 | (String, $option_name:ident, $description:literal, $default:literal $(,)?) => { | ||
| 175 | string_create!($option_name, $description, $default); | ||
| 176 | option_getter!(String, $option_name, String); | ||
| 177 | }; | ||
| 178 | |||
| 179 | (Color, $option_name:ident, $description:literal, $default:literal $(,)?) => { | ||
| 180 | color_create!($option_name, $description, $default); | ||
| 181 | option_getter!(Color, $option_name, String); | ||
| 182 | }; | ||
| 183 | |||
| 184 | (bool, $option_name:ident, $description:literal, $default:literal $(,)?) => { | ||
| 185 | bool_create!($option_name, $description, $default); | ||
| 186 | option_getter!(Boolean, $option_name, bool); | ||
| 187 | }; | ||
| 188 | |||
| 189 | (Integer, $option_name:ident, $description:literal, $default:literal, $min:literal..$max:literal $(,)?) => { | ||
| 190 | integer_create!($option_name, $description, $default, $min, $max); | ||
| 191 | option_getter!(Integer, $option_name, i64); | ||
| 192 | }; | ||
| 193 | |||
| 194 | (Enum, $option_name:ident, $description:literal, $out_type:ty $(,)?) => { | ||
| 195 | enum_create!($option_name, $description, $out_type); | ||
| 196 | option_getter!(Integer, $option_name, $out_type); | ||
| 197 | }; | ||
| 198 | |||
| 199 | (EvaluatedString, $option_name:ident, $description:literal, $default:literal $(,)?) => { | ||
| 200 | string_create!($option_name, $description, $default); | ||
| 201 | |||
| 202 | paste::item! { | ||
| 203 | pub fn [<$option_name>](&self) -> String { | ||
| 204 | let option_name = stringify!($option_name); | ||
| 205 | |||
| 206 | let option = self.0.search_option(option_name) | ||
| 207 | .expect(&format!("Couldn't find option {} in section {}", | ||
| 208 | option_name, self.0.name())); | ||
| 209 | |||
| 210 | if let ConfigOption::String(o) = option { | ||
| 211 | Weechat::eval_string_expression(&o.value()) | ||
| 212 | .expect(&format!( | ||
| 213 | "Can't evaluate string expression for option {} in section {}", | ||
| 214 | option_name, | ||
| 215 | self.0.name()) | ||
| 216 | ) | ||
| 217 | } else { | ||
| 218 | panic!("Incorect option type for option {} in section {}", | ||
| 219 | option_name, self.0.name()); | ||
| 220 | } | ||
| 221 | } | ||
| 222 | } | ||
| 223 | }; | ||
| 224 | } | ||
| 225 | |||
| 226 | macro_rules! section { | ||
| 227 | ($section:ident { $($option_name:ident: $option_type:ident {$($option:tt)*}), * $(,)? }) => { | ||
| 228 | paste::item! { | ||
| 229 | pub struct [<$section:camel Section>]<'a>(SectionHandle<'a>); | ||
| 230 | |||
| 231 | impl<'a> [<$section:camel Section>]<'a> { | ||
| 232 | pub fn create(config: &mut Config) { | ||
| 233 | let section_settings = ConfigSectionSettings::new(stringify!($section)); | ||
| 234 | |||
| 235 | let mut $section = config.new_section(section_settings) | ||
| 236 | .expect(&format!("Can't create config section {}", stringify!($section))); | ||
| 237 | |||
| 238 | [<$section:camel Section>]::create_options(&mut $section); | ||
| 239 | } | ||
| 240 | |||
| 241 | pub fn create_options(section: &mut SectionHandleMut) { | ||
| 242 | $( | ||
| 243 | [<$section:camel Section>]::[<create_option_ $option_name>](section); | ||
| 244 | )* | ||
| 245 | } | ||
| 246 | |||
| 247 | $( | ||
| 248 | option!($option_type, $option_name, $($option)*); | ||
| 249 | )* | ||
| 250 | } | ||
| 251 | } | ||
| 252 | } | ||
| 253 | } | ||
| 254 | |||
| 255 | macro_rules! config { | ||
| 256 | ($(Section $section:ident { $($option:tt)* }), * $(,)?) => { | ||
| 257 | pub struct Config(weechat::config::Config); | ||
| 258 | |||
| 259 | impl Deref for Config { | ||
| 260 | type Target = weechat::config::Config; | ||
| 261 | |||
| 262 | fn deref(&self) -> &Self::Target { | ||
| 263 | &self.0 | ||
| 264 | } | ||
| 265 | } | ||
| 266 | |||
| 267 | impl DerefMut for Config { | ||
| 268 | fn deref_mut(&mut self) -> &mut Self::Target { | ||
| 269 | &mut self.0 | ||
| 270 | } | ||
| 271 | } | ||
| 272 | |||
| 273 | impl Config { | ||
| 274 | fn new(config: weechat::config::Config) -> Self { | ||
| 275 | let mut config = Config(config); | ||
| 276 | config.create_sections(); | ||
| 277 | |||
| 278 | config | ||
| 279 | } | ||
| 280 | |||
| 281 | paste::item! { | ||
| 282 | fn create_sections(&mut self) { | ||
| 283 | $( | ||
| 284 | paste::expr! { [<$section:camel Section>]::create(self) }; | ||
| 285 | )* | ||
| 286 | } | ||
| 287 | } | ||
| 288 | |||
| 289 | paste::item! { | ||
| 290 | $( | ||
| 291 | pub fn $section(&self) -> [<$section:camel Section>] { | ||
| 292 | let section_name = stringify!($section); | ||
| 293 | let section = self.0.search_section(section_name) | ||
| 294 | .expect(&format!("Couldn't find section {}", section_name)); | ||
| 295 | |||
| 296 | paste::item! { [<$section:camel Section>](section) } | ||
| 297 | } | ||
| 298 | )* | ||
| 299 | } | ||
| 300 | } | ||
| 301 | |||
| 302 | $( | ||
| 303 | section!($section { $($option)* }); | ||
| 304 | )* | ||
| 305 | } | ||
| 306 | } | ||
| 307 | |||
| 308 | config!( | 61 | config!( |
| 309 | Section look { | 62 | Section look { |
| 310 | encrypted_room_sign: String { | 63 | encrypted_room_sign: String { |
src/config_macros.rs added +255
| @@ -0,0 +1,255 @@ | |||
| 1 | use weechat::config::{ | ||
| 2 | BooleanOptionSettings, Conf, ConfigOption, ConfigSection, | ||
| 3 | ConfigSectionSettings, IntegerOptionSettings, OptionChanged, SectionHandle, | ||
| 4 | SectionHandleMut, SectionReadCallback, StringOptionSettings, ColorOptionSettings, | ||
| 5 | }; | ||
| 6 | use weechat::Weechat; | ||
| 7 | |||
| 8 | use strum::VariantNames; | ||
| 9 | |||
| 10 | macro_rules! string_create { | ||
| 11 | ($option_name:ident, $description:literal, $default:literal) => { | ||
| 12 | paste::item! { | ||
| 13 | fn [<create_option_ $option_name>](section: &mut SectionHandleMut) { | ||
| 14 | let option_name = stringify!($option_name); | ||
| 15 | let option_settings = StringOptionSettings::new(option_name) | ||
| 16 | .description($description) | ||
| 17 | .default_value($default); | ||
| 18 | |||
| 19 | section.new_string_option(option_settings) | ||
| 20 | .expect(&format!("Can't create option {}", option_name)); | ||
| 21 | } | ||
| 22 | } | ||
| 23 | }; | ||
| 24 | } | ||
| 25 | |||
| 26 | macro_rules! color_create { | ||
| 27 | ($option_name:ident, $description:literal, $default:literal) => { | ||
| 28 | paste::item! { | ||
| 29 | fn [<create_option_ $option_name>](section: &mut SectionHandleMut) { | ||
| 30 | let option_name = stringify!($option_name); | ||
| 31 | let option_settings = ColorOptionSettings::new(option_name) | ||
| 32 | .description($description) | ||
| 33 | .default_value($default); | ||
| 34 | |||
| 35 | section.new_color_option(option_settings) | ||
| 36 | .expect(&format!("Can't create option {}", option_name)); | ||
| 37 | } | ||
| 38 | } | ||
| 39 | }; | ||
| 40 | } | ||
| 41 | |||
| 42 | macro_rules! bool_create { | ||
| 43 | ($option_name:ident, $description:literal, $default:literal) => { | ||
| 44 | paste::item! { | ||
| 45 | fn [<create_option_ $option_name>](section: &mut SectionHandleMut) { | ||
| 46 | let option_name = stringify!($option_name); | ||
| 47 | let option_settings = BooleanOptionSettings::new(option_name) | ||
| 48 | .description($description) | ||
| 49 | .default_value($default); | ||
| 50 | |||
| 51 | section.new_boolean_option(option_settings) | ||
| 52 | .expect(&format!("Can't create option {}", option_name)); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | }; | ||
| 56 | } | ||
| 57 | |||
| 58 | macro_rules! integer_create { | ||
| 59 | ($option_name:ident, $description:literal, $default:literal, $min:literal, $max:literal) => { | ||
| 60 | paste::item! { | ||
| 61 | fn [<create_option_ $option_name>](section: &mut SectionHandleMut) { | ||
| 62 | let option_name = stringify!($option_name); | ||
| 63 | |||
| 64 | let option_settings = IntegerOptionSettings::new(option_name) | ||
| 65 | .description($description) | ||
| 66 | .default_value($default) | ||
| 67 | .min($min) | ||
| 68 | .max($max); | ||
| 69 | |||
| 70 | section.new_integer_option(option_settings) | ||
| 71 | .expect(&format!("Can't create option {}", option_name)); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | }; | ||
| 75 | } | ||
| 76 | |||
| 77 | macro_rules! enum_create { | ||
| 78 | ($option_name:ident, $description:literal, $out_type:ty) => { | ||
| 79 | paste::item! { | ||
| 80 | fn [<create_option_ $option_name>](section: &mut SectionHandleMut) { | ||
| 81 | let mut string_values: Vec<String> = Vec::new(); | ||
| 82 | |||
| 83 | for value in $out_type::VARIANTS { | ||
| 84 | string_values.push(value.to_string()); | ||
| 85 | } | ||
| 86 | |||
| 87 | let default_value = $out_type::default(); | ||
| 88 | |||
| 89 | let option_name = stringify!($option_name); | ||
| 90 | let option_settings = IntegerOptionSettings::new(option_name) | ||
| 91 | .description($description) | ||
| 92 | .default_value(default_value as i32) | ||
| 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 | |||
| 102 | macro_rules! option_getter { | ||
| 103 | ($option_type:ident, $option_name:ident, $output_type:ty) => { | ||
| 104 | paste::item! { | ||
| 105 | pub fn [<$option_name>](&self) -> $output_type { | ||
| 106 | let option_name = stringify!($option_name); | ||
| 107 | |||
| 108 | if let ConfigOption::[<$option_type>](o) = self.0.search_option(option_name) | ||
| 109 | .expect(&format!("Couldn't find option {} in section {}", | ||
| 110 | option_name, self.0.name())) | ||
| 111 | { | ||
| 112 | $output_type::from(o.value()) | ||
| 113 | } else { | ||
| 114 | panic!("Incorect option type for option {} in section {}", | ||
| 115 | option_name, self.0.name()); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | } | ||
| 119 | }; | ||
| 120 | } | ||
| 121 | |||
| 122 | macro_rules! option { | ||
| 123 | (String, $option_name:ident, $description:literal, $default:literal $(,)?) => { | ||
| 124 | string_create!($option_name, $description, $default); | ||
| 125 | option_getter!(String, $option_name, String); | ||
| 126 | }; | ||
| 127 | |||
| 128 | (Color, $option_name:ident, $description:literal, $default:literal $(,)?) => { | ||
| 129 | color_create!($option_name, $description, $default); | ||
| 130 | option_getter!(Color, $option_name, String); | ||
| 131 | }; | ||
| 132 | |||
| 133 | (bool, $option_name:ident, $description:literal, $default:literal $(,)?) => { | ||
| 134 | bool_create!($option_name, $description, $default); | ||
| 135 | option_getter!(Boolean, $option_name, bool); | ||
| 136 | }; | ||
| 137 | |||
| 138 | (Integer, $option_name:ident, $description:literal, $default:literal, $min:literal..$max:literal $(,)?) => { | ||
| 139 | integer_create!($option_name, $description, $default, $min, $max); | ||
| 140 | option_getter!(Integer, $option_name, i64); | ||
| 141 | }; | ||
| 142 | |||
| 143 | (Enum, $option_name:ident, $description:literal, $out_type:ty $(,)?) => { | ||
| 144 | enum_create!($option_name, $description, $out_type); | ||
| 145 | option_getter!(Integer, $option_name, $out_type); | ||
| 146 | }; | ||
| 147 | |||
| 148 | (EvaluatedString, $option_name:ident, $description:literal, $default:literal $(,)?) => { | ||
| 149 | string_create!($option_name, $description, $default); | ||
| 150 | |||
| 151 | paste::item! { | ||
| 152 | pub fn [<$option_name>](&self) -> String { | ||
| 153 | let option_name = stringify!($option_name); | ||
| 154 | |||
| 155 | let option = self.0.search_option(option_name) | ||
| 156 | .expect(&format!("Couldn't find option {} in section {}", | ||
| 157 | option_name, self.0.name())); | ||
| 158 | |||
| 159 | if let ConfigOption::String(o) = option { | ||
| 160 | Weechat::eval_string_expression(&o.value()) | ||
| 161 | .expect(&format!( | ||
| 162 | "Can't evaluate string expression for option {} in section {}", | ||
| 163 | option_name, | ||
| 164 | self.0.name()) | ||
| 165 | ) | ||
| 166 | } else { | ||
| 167 | panic!("Incorect option type for option {} in section {}", | ||
| 168 | option_name, self.0.name()); | ||
| 169 | } | ||
| 170 | } | ||
| 171 | } | ||
| 172 | }; | ||
| 173 | } | ||
| 174 | |||
| 175 | macro_rules! section { | ||
| 176 | ($section:ident { $($option_name:ident: $option_type:ident {$($option:tt)*}), * $(,)? }) => { | ||
| 177 | paste::item! { | ||
| 178 | pub struct [<$section:camel Section>]<'a>(SectionHandle<'a>); | ||
| 179 | |||
| 180 | impl<'a> [<$section:camel Section>]<'a> { | ||
| 181 | pub fn create(config: &mut Config) { | ||
| 182 | let section_settings = ConfigSectionSettings::new(stringify!($section)); | ||
| 183 | |||
| 184 | let mut $section = config.new_section(section_settings) | ||
| 185 | .expect(&format!("Can't create config section {}", stringify!($section))); | ||
| 186 | |||
| 187 | [<$section:camel Section>]::create_options(&mut $section); | ||
| 188 | } | ||
| 189 | |||
| 190 | pub fn create_options(section: &mut SectionHandleMut) { | ||
| 191 | $( | ||
| 192 | [<$section:camel Section>]::[<create_option_ $option_name>](section); | ||
| 193 | )* | ||
| 194 | } | ||
| 195 | |||
| 196 | $( | ||
| 197 | option!($option_type, $option_name, $($option)*); | ||
| 198 | )* | ||
| 199 | } | ||
| 200 | } | ||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | macro_rules! config { | ||
| 205 | ($(Section $section:ident { $($option:tt)* }), * $(,)?) => { | ||
| 206 | pub struct Config(weechat::config::Config); | ||
| 207 | |||
| 208 | impl Deref for Config { | ||
| 209 | type Target = weechat::config::Config; | ||
| 210 | |||
| 211 | fn deref(&self) -> &Self::Target { | ||
| 212 | &self.0 | ||
| 213 | } | ||
| 214 | } | ||
| 215 | |||
| 216 | impl DerefMut for Config { | ||
| 217 | fn deref_mut(&mut self) -> &mut Self::Target { | ||
| 218 | &mut self.0 | ||
| 219 | } | ||
| 220 | } | ||
| 221 | |||
| 222 | impl Config { | ||
| 223 | fn new(config: weechat::config::Config) -> Self { | ||
| 224 | let mut config = Config(config); | ||
| 225 | config.create_sections(); | ||
| 226 | |||
| 227 | config | ||
| 228 | } | ||
| 229 | |||
| 230 | paste::item! { | ||
| 231 | fn create_sections(&mut self) { | ||
| 232 | $( | ||
| 233 | paste::expr! { [<$section:camel Section>]::create(self) }; | ||
| 234 | )* | ||
| 235 | } | ||
| 236 | } | ||
| 237 | |||
| 238 | paste::item! { | ||
| 239 | $( | ||
| 240 | pub fn $section(&self) -> [<$section:camel Section>] { | ||
| 241 | let section_name = stringify!($section); | ||
| 242 | let section = self.0.search_section(section_name) | ||
| 243 | .expect(&format!("Couldn't find section {}", section_name)); | ||
| 244 | |||
| 245 | paste::item! { [<$section:camel Section>](section) } | ||
| 246 | } | ||
| 247 | )* | ||
| 248 | } | ||
| 249 | } | ||
| 250 | |||
| 251 | $( | ||
| 252 | section!($section { $($option)* }); | ||
| 253 | )* | ||
| 254 | } | ||
| 255 | } | ||
src/lib.rs +2
| @@ -1,5 +1,7 @@ | |||
| 1 | #![feature(async_closure)] | 1 | #![feature(async_closure)] |
| 2 | 2 | ||
| 3 | #[macro_use] mod config_macros; | ||
| 4 | |||
| 3 | mod commands; | 5 | mod commands; |
| 4 | mod config; | 6 | mod config; |
| 5 | mod debug; | 7 | mod debug; |