| @@ -1,317 +0,0 @@ |
| 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) |
| 7 | | } |
| 8 | | }; |
| 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) |
| 15 | | }; |
| 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( |
| 21 | | <$out_type>::VARIANTS |
| 22 | | .iter() |
| 23 | | .map(|v| v.to_string()) |
| 24 | | .collect::<Vec<String>>(), |
| 25 | | ); |
| 26 | | }; |
| 27 | | } |
| 28 | | |
| 29 | | #[allow(unused_macros)] |
| 30 | | macro_rules! option_create { |
| 31 | | ($option_type:ident, $option_weechat_type:ident, $option_name:ident, $($args:tt)*) => { |
| 32 | | paste::item! { |
| 33 | | fn [<create_option_ $option_name>](section: &mut SectionHandleMut) { |
| 34 | | let option_settings = option_settings!($option_type, $option_name, $($args)*); |
| 35 | | section.[<new_ $option_weechat_type:lower _option>](option_settings) |
| 36 | | .expect(&format!("Can't create option {}", stringify!($option_name))); |
| 37 | | } |
| 38 | | } |
| 39 | | }; |
| 40 | | } |
| 41 | | |
| 42 | | macro_rules! option_getter { |
| 43 | | ($option_type:ident, $option_name:ident, $output_type:ty) => { |
| 44 | | paste::item! { |
| 45 | | #[allow(dead_code)] |
| 46 | | pub fn [<$option_name>](&self) -> $output_type { |
| 47 | | let option_name = stringify!($option_name); |
| 48 | | |
| 49 | | if let ConfigOption::[<$option_type>](o) = self.0.search_option(option_name) |
| 50 | | .expect(&format!("Couldn't find option {} in section {}", |
| 51 | | option_name, self.0.name())) |
| 52 | | { |
| 53 | | $output_type::from(o.value()) |
| 54 | | } else { |
| 55 | | panic!("Incorect option type for option {} in section {}", |
| 56 | | option_name, self.0.name()); |
| 57 | | } |
| 58 | | } |
| 59 | | } |
| 60 | | }; |
| 61 | | } |
| 62 | | |
| 63 | | macro_rules! option { |
| 64 | | (String, $option_name:ident, $($args:tt)*) => { |
| 65 | | option_create!(String, String, $option_name, $($args)*); |
| 66 | | option_getter!(String, $option_name, String); |
| 67 | | }; |
| 68 | | |
| 69 | | (Color, $option_name:ident, $($args:tt)*) => { |
| 70 | | option_create!(Color, Color, $option_name, $($args)*); |
| 71 | | option_getter!(Color, $option_name, String); |
| 72 | | }; |
| 73 | | |
| 74 | | (bool, $option_name:ident, $($args:tt)*) => { |
| 75 | | option_create!(Boolean, Boolean, $option_name, $($args)*); |
| 76 | | option_getter!(Boolean, $option_name, bool); |
| 77 | | }; |
| 78 | | |
| 79 | | (Integer, $option_name:ident, $($args:tt)*) => { |
| 80 | | option_create!(Integer, Integer, $option_name, $($args)*); |
| 81 | | option_getter!(Integer, $option_name, i64); |
| 82 | | }; |
| 83 | | |
| 84 | | (Enum, $option_name:ident, $description:literal, $out_type:ty $(,)?) => { |
| 85 | | option_create!(Enum, Integer, $option_name, $description, $out_type); |
| 86 | | option_getter!(Integer, $option_name, $out_type); |
| 87 | | }; |
| 88 | | |
| 89 | | (EvaluatedString, $option_name:ident, $($args:tt)*) => { |
| 90 | | option_create!(String, String, $option_name, $($args)*); |
| 91 | | |
| 92 | | paste::item! { |
| 93 | | pub fn [<$option_name>](&self) -> String { |
| 94 | | let option_name = stringify!($option_name); |
| 95 | | |
| 96 | | let option = self.0.search_option(option_name) |
| 97 | | .expect(&format!("Couldn't find option {} in section {}", |
| 98 | | option_name, self.0.name())); |
| 99 | | |
| 100 | | if let ConfigOption::String(o) = option { |
| 101 | | Weechat::eval_string_expression(&o.value()) |
| 102 | | .expect(&format!( |
| 103 | | "Can't evaluate string expression for option {} in section {}", |
| 104 | | option_name, |
| 105 | | self.0.name()) |
| 106 | | ) |
| 107 | | } else { |
| 108 | | panic!("Incorect option type for option {} in section {}", |
| 109 | | option_name, self.0.name()); |
| 110 | | } |
| 111 | | } |
| 112 | | } |
| 113 | | }; |
| 114 | | } |
| 115 | | |
| 116 | | macro_rules! section { |
| 117 | | ($section:ident { $($option_name:ident: $option_type:ident {$($option:tt)*}), * $(,)? }) => { |
| 118 | | paste::item! { |
| 119 | | pub struct [<$section:camel Section>]<'a>(SectionHandle<'a>); |
| 120 | | |
| 121 | | impl<'a> [<$section:camel Section>]<'a> { |
| 122 | | pub fn create(config: &mut Config) { |
| 123 | | let section_settings = ConfigSectionSettings::new(stringify!($section)); |
| 124 | | |
| 125 | | let mut $section = config.new_section(section_settings) |
| 126 | | .expect(&format!("Can't create config section {}", stringify!($section))); |
| 127 | | |
| 128 | | [<$section:camel Section>]::create_options(&mut $section); |
| 129 | | } |
| 130 | | |
| 131 | | pub fn create_options(section: &mut SectionHandleMut) { |
| 132 | | $( |
| 133 | | [<$section:camel Section>]::[<create_option_ $option_name>](section); |
| 134 | | )* |
| 135 | | } |
| 136 | | |
| 137 | | $( |
| 138 | | option!($option_type, $option_name, $($option)*); |
| 139 | | )* |
| 140 | | } |
| 141 | | } |
| 142 | | } |
| 143 | | } |
| 144 | | |
| 145 | | /// Declare a Weechat configuration file. |
| 146 | | /// |
| 147 | | /// This will generate a struct called `Config` which wraps the Weechat struct |
| 148 | | /// of the same name. The generated struct will have accessors for every |
| 149 | | /// section and option that is declared. |
| 150 | | /// |
| 151 | | /// The generated struct dereferences into the Weechat `Config` struct so |
| 152 | | /// additional sections and options can be created the usual way as well. |
| 153 | | /// |
| 154 | | /// The config still needs to be created in the `init()` method of the plugin. |
| 155 | | /// |
| 156 | | /// # Example |
| 157 | | /// ``` |
| 158 | | /// |
| 159 | | /// #[derive(EnumVariantNames)] |
| 160 | | /// #[strum(serialize_all = "kebab_case")] |
| 161 | | /// pub enum ServerBufferMerge { |
| 162 | | /// MergeWithCore, |
| 163 | | /// MergeWithoutCore, |
| 164 | | /// Independent, |
| 165 | | /// } |
| 166 | | /// |
| 167 | | /// impl Default for ServerBufferMerge { |
| 168 | | /// fn default() -> Self { |
| 169 | | /// ServerBufferMerge::MergeWithCore |
| 170 | | /// } |
| 171 | | /// } |
| 172 | | /// |
| 173 | | /// impl From<i32> for ServerBufferMerge { |
| 174 | | /// fn from(value: i32) -> Self { |
| 175 | | /// match value { |
| 176 | | /// 0 => ServerBufferMerge::MergeWithCore, |
| 177 | | /// 1 => ServerBufferMerge::MergeWithoutCore, |
| 178 | | /// 2 => ServerBufferMerge::Independent, |
| 179 | | /// _ => unreachable!(), |
| 180 | | /// } |
| 181 | | /// } |
| 182 | | /// } |
| 183 | | /// |
| 184 | | /// config!( |
| 185 | | /// // The name of the config |
| 186 | | /// "my-plugin", |
| 187 | | /// Section look { |
| 188 | | /// encrypted_room_sign: String { |
| 189 | | /// // Description. |
| 190 | | /// "A sign that is used to show that the current room is encrypted", |
| 191 | | /// |
| 192 | | /// // Default value. |
| 193 | | /// "🔒", |
| 194 | | /// }, |
| 195 | | /// |
| 196 | | /// server_buffer: Enum { |
| 197 | | /// // Description. |
| 198 | | /// "Merge server buffers", |
| 199 | | /// |
| 200 | | /// // This is an enum that needs to have the following traits |
| 201 | | /// // implemented: |
| 202 | | /// // * Default - To define the default value of the option. |
| 203 | | /// // * From<i32> - To convert the internal Weechat integer option |
| 204 | | /// // to the enum. |
| 205 | | /// // * VariantNames - To get the string representation of the |
| 206 | | /// // enum variants. This is a trait defined in the strum library, |
| 207 | | /// // a simple macro that derives an implementation is provided by |
| 208 | | /// // strum. |
| 209 | | /// ServerBufferMerge, |
| 210 | | /// }, |
| 211 | | /// |
| 212 | | /// quote_fg: Color { |
| 213 | | /// // Description. |
| 214 | | /// "Foreground color for Matrix style blockquotes", |
| 215 | | /// |
| 216 | | /// // Default value. |
| 217 | | /// "lightgreen", |
| 218 | | /// }, |
| 219 | | /// }, |
| 220 | | /// |
| 221 | | /// Section network { |
| 222 | | /// username: EvaluatedString { |
| 223 | | /// // Description. |
| 224 | | /// "The username that will be used to log in to the server \ |
| 225 | | /// (note: content is evaluated, see /help eval)", |
| 226 | | /// |
| 227 | | /// // Default value. |
| 228 | | /// "", |
| 229 | | /// }, |
| 230 | | /// |
| 231 | | /// timeout: Integer { |
| 232 | | /// // Description. |
| 233 | | /// "A timeout (in seconds) that determines how long we should wait \ |
| 234 | | /// for a request to finish before aborting.", |
| 235 | | /// |
| 236 | | /// // Default value. |
| 237 | | /// 30, |
| 238 | | /// |
| 239 | | /// // The range that the value is allowed to have, note that both of |
| 240 | | /// // those are inclusive. |
| 241 | | /// 0..100, |
| 242 | | /// }, |
| 243 | | /// |
| 244 | | /// autoconnect: bool { |
| 245 | | /// // Description. |
| 246 | | /// "Automatically connect to the server when Weechat is starting", |
| 247 | | /// |
| 248 | | /// // Default value. |
| 249 | | /// false, |
| 250 | | /// }, |
| 251 | | /// } |
| 252 | | /// ); |
| 253 | | /// ``` |
| 254 | | macro_rules! config { |
| 255 | | ($config_name:literal, $(Section $section:ident { $($option:tt)* }), * $(,)?) => { |
| 256 | | #[allow(unused_imports)] |
| 257 | | use strum::VariantNames; |
| 258 | | use std::ops::{Deref, DerefMut}; |
| 259 | | #[allow(unused_imports)] |
| 260 | | use weechat::config::{ |
| 261 | | SectionHandle, SectionHandleMut, StringOptionSettings, |
| 262 | | ConfigOption, ConfigSection, ConfigSectionSettings, |
| 263 | | BooleanOptionSettings, IntegerOptionSettings, ColorOptionSettings, |
| 264 | | }; |
| 265 | | pub struct Config(weechat::config::Config); |
| 266 | | |
| 267 | | impl Deref for Config { |
| 268 | | type Target = weechat::config::Config; |
| 269 | | |
| 270 | | fn deref(&self) -> &Self::Target { |
| 271 | | &self.0 |
| 272 | | } |
| 273 | | } |
| 274 | | |
| 275 | | impl DerefMut for Config { |
| 276 | | fn deref_mut(&mut self) -> &mut Self::Target { |
| 277 | | &mut self.0 |
| 278 | | } |
| 279 | | } |
| 280 | | |
| 281 | | impl Config { |
| 282 | | fn new() -> Result<Self, ()> { |
| 283 | | let config = Weechat::config_new($config_name)?; |
| 284 | | let mut config = Config(config); |
| 285 | | |
| 286 | | config.create_sections(); |
| 287 | | |
| 288 | | Ok(config) |
| 289 | | } |
| 290 | | |
| 291 | | paste::item! { |
| 292 | | fn create_sections(&mut self) { |
| 293 | | $( |
| 294 | | paste::expr! { [<$section:camel Section>]::create(self) }; |
| 295 | | )* |
| 296 | | } |
| 297 | | } |
| 298 | | |
| 299 | | paste::item! { |
| 300 | | $( |
| 301 | | #[allow(dead_code)] |
| 302 | | pub fn $section(&self) -> [<$section:camel Section>] { |
| 303 | | let section_name = stringify!($section); |
| 304 | | let section = self.0.search_section(section_name) |
| 305 | | .expect(&format!("Couldn't find section {}", section_name)); |
| 306 | | |
| 307 | | paste::item! { [<$section:camel Section>](section) } |
| 308 | | } |
| 309 | | )* |
| 310 | | } |
| 311 | | } |
| 312 | | |
| 313 | | $( |
| 314 | | section!($section { $($option)* }); |
| 315 | | )* |
| 316 | | } |
| 317 | | } |