Commit c4d1d79629
Unsigned
Layout: unified · split
src/config.rs +1 −5
| @@ -16,14 +16,10 @@ | |||
| 16 | //! the section will do so. | 16 | //! the section will do so. |
| 17 | 17 | ||
| 18 | use crate::{MatrixServer, Servers}; | 18 | use crate::{MatrixServer, Servers}; |
| 19 | use weechat::config::{ | 19 | use weechat::config::{Conf, OptionChanged, SectionReadCallback}; |
| 20 | Conf, ConfigOption, ConfigSection, ConfigSectionSettings, OptionChanged, | ||
| 21 | SectionHandle, SectionHandleMut, SectionReadCallback, StringOptionSettings, | ||
| 22 | }; | ||
| 23 | use weechat::Weechat; | 20 | use weechat::Weechat; |
| 24 | 21 | ||
| 25 | use std::cell::{Ref, RefCell, RefMut}; | 22 | use std::cell::{Ref, RefCell, RefMut}; |
| 26 | use std::ops::{Deref, DerefMut}; | ||
| 27 | use std::rc::Rc; | 23 | use std::rc::Rc; |
| 28 | 24 | ||
| 29 | config!( | 25 | config!( |
src/config_macros.rs +39 −93
| @@ -1,95 +1,35 @@ | |||
| 1 | #[allow(unused_macros)] | 1 | macro_rules! option_settings { |
| 2 | macro_rules! string_create { | 2 | ($option_type:ident, $option_name:ident, $description:literal, $default:literal $(,)?) => { |
| 3 | ($option_name:ident, $description:literal, $default:literal) => { | 3 | paste::expr! { |
| 4 | paste::item! { | 4 | [<$option_type OptionSettings>]::new(stringify!($option_name)) |
| 5 | fn [<create_option_ $option_name>](section: &mut SectionHandleMut) { | 5 | .description($description) |
| 6 | let option_name = stringify!($option_name); | 6 | .default_value($default) |
| 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 | } | ||
| 31 | } | 7 | } |
| 32 | }; | 8 | }; |
| 33 | } | 9 | (Integer, $option_name:ident, $description:literal, $default:literal, $min:literal..$max:literal $(,)?) => { |
| 34 | 10 | IntegerOptionSettings::new(stringify!($option_name)) | |
| 35 | #[allow(unused_macros)] | 11 | .description($description) |
| 36 | macro_rules! bool_create { | 12 | .default_value($default) |
| 37 | ($option_name:ident, $description:literal, $default:literal) => { | 13 | .min($min) |
| 38 | paste::item! { | 14 | .max($max) |
| 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 | } | ||
| 49 | }; | 15 | }; |
| 50 | } | 16 | (Enum, $option_name:ident, $description:literal, $out_type:ty $(,)?) => { |
| 51 | 17 | IntegerOptionSettings::new(stringify!($option_name)) | |
| 52 | #[allow(unused_macros)] | 18 | .description($description) |
| 53 | macro_rules! integer_create { | 19 | .default_value(<$out_type>::default() as i32) |
| 54 | ($option_name:ident, $description:literal, $default:literal, $min:literal, $max:literal) => { | 20 | .string_values(<$out_type>::VARIANTS.iter().map(|v| v.to_string()).collect::<Vec<String>>()); |
| 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); | ||
| 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 | #[allow(unused_macros)] | 25 | #[allow(unused_macros)] |
| 73 | macro_rules! enum_create { | 26 | macro_rules! option_create { |
| 74 | ($option_name:ident, $description:literal, $out_type:ty) => { | 27 | ($option_type:ident, $option_weechat_type:ident, $option_name:ident, $($args:tt)*) => { |
| 75 | paste::item! { | 28 | paste::item! { |
| 76 | fn [<create_option_ $option_name>](section: &mut SectionHandleMut) { | 29 | fn [<create_option_ $option_name>](section: &mut SectionHandleMut) { |
| 77 | let mut string_values: Vec<String> = Vec::new(); | 30 | let option_settings = option_settings!($option_type, $option_name, $($args)*); |
| 78 | 31 | section.[<new_ $option_weechat_type:lower _option>](option_settings) | |
| 79 | for value in $out_type::VARIANTS { | 32 | .expect(&format!("Can't create option {}", stringify!($option_name))); |
| 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)); | ||
| 93 | } | 33 | } |
| 94 | } | 34 | } |
| 95 | }; | 35 | }; |
| @@ -116,33 +56,33 @@ macro_rules! option_getter { | |||
| 116 | } | 56 | } |
| 117 | 57 | ||
| 118 | macro_rules! option { | 58 | macro_rules! option { |
| 119 | (String, $option_name:ident, $description:literal, $default:literal $(,)?) => { | 59 | (String, $option_name:ident, $($args:tt)*) => { |
| 120 | string_create!($option_name, $description, $default); | 60 | option_create!(String, String, $option_name, $($args)*); |
| 121 | option_getter!(String, $option_name, String); | 61 | option_getter!(String, $option_name, String); |
| 122 | }; | 62 | }; |
| 123 | 63 | ||
| 124 | (Color, $option_name:ident, $description:literal, $default:literal $(,)?) => { | 64 | (Color, $option_name:ident, $($args:tt)*) => { |
| 125 | color_create!($option_name, $description, $default); | 65 | option_create!(Color, Color, $option_name, $($args)*); |
| 126 | option_getter!(Color, $option_name, String); | 66 | option_getter!(Color, $option_name, String); |
| 127 | }; | 67 | }; |
| 128 | 68 | ||
| 129 | (bool, $option_name:ident, $description:literal, $default:literal $(,)?) => { | 69 | (bool, $option_name:ident, $($args:tt)*) => { |
| 130 | bool_create!($option_name, $description, $default); | 70 | option_create!(Boolean, Boolean, $option_name, $($args)*); |
| 131 | option_getter!(Boolean, $option_name, bool); | 71 | option_getter!(Boolean, $option_name, bool); |
| 132 | }; | 72 | }; |
| 133 | 73 | ||
| 134 | (Integer, $option_name:ident, $description:literal, $default:literal, $min:literal..$max:literal $(,)?) => { | 74 | (Integer, $option_name:ident, $($args:tt)*) => { |
| 135 | integer_create!($option_name, $description, $default, $min, $max); | 75 | option_create!(Integer, Integer, $option_name, $($args)*); |
| 136 | option_getter!(Integer, $option_name, i64); | 76 | option_getter!(Integer, $option_name, i64); |
| 137 | }; | 77 | }; |
| 138 | 78 | ||
| 139 | (Enum, $option_name:ident, $description:literal, $out_type:ty $(,)?) => { | 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 | option_getter!(Integer, $option_name, $out_type); | 81 | option_getter!(Integer, $option_name, $out_type); |
| 142 | }; | 82 | }; |
| 143 | 83 | ||
| 144 | (EvaluatedString, $option_name:ident, $description:literal, $default:literal $(,)?) => { | 84 | (EvaluatedString, $option_name:ident, $($args:tt)*) => { |
| 145 | string_create!($option_name, $description, $default); | 85 | option_create!(String, String, $option_name, $($args)*); |
| 146 | 86 | ||
| 147 | paste::item! { | 87 | paste::item! { |
| 148 | pub fn [<$option_name>](&self) -> String { | 88 | pub fn [<$option_name>](&self) -> String { |
| @@ -310,6 +250,12 @@ macro_rules! config { | |||
| 310 | ($config_name:literal, $(Section $section:ident { $($option:tt)* }), * $(,)?) => { | 250 | ($config_name:literal, $(Section $section:ident { $($option:tt)* }), * $(,)?) => { |
| 311 | #[allow(unused_imports)] | 251 | #[allow(unused_imports)] |
| 312 | use strum::VariantNames; | 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 | pub struct Config(weechat::config::Config); | 259 | pub struct Config(weechat::config::Config); |
| 314 | 260 | ||
| 315 | impl Deref for Config { | 261 | impl Deref for Config { |