| @@ -16,69 +16,188 @@ |
| 16 | 16 | //! the section will do so. |
| 17 | 17 | use crate::{MatrixServer, Servers}; |
| 18 | 18 | use weechat::config::{ |
| 19 | | Conf, ConfigSection, ConfigSectionSettings, OptionChanged, |
| 20 | | SectionReadCallback, |
| 19 | Conf, ConfigOption, ConfigSection, ConfigSectionSettings, OptionChanged, |
| 20 | SectionHandle, SectionHandleMut, SectionReadCallback, StringOptionSettings, |
| 21 | 21 | }; |
| 22 | 22 | use weechat::Weechat; |
| 23 | 23 | |
| 24 | 24 | use std::cell::{Ref, RefCell, RefMut}; |
| 25 | use std::ops::{Deref, DerefMut}; |
| 25 | 26 | use std::rc::Rc; |
| 26 | 27 | |
| 27 | 28 | #[derive(Clone)] |
| 28 | | pub struct Config { |
| 29 | | inner: Rc<RefCell<weechat::config::Config>>, |
| 29 | pub struct ConfigHandle { |
| 30 | inner: Rc<RefCell<Config>>, |
| 30 | 31 | servers: Servers, |
| 31 | 32 | } |
| 32 | 33 | |
| 33 | | impl SectionReadCallback for Config { |
| 34 | | fn callback( |
| 35 | | &mut self, |
| 36 | | _: &Weechat, |
| 37 | | _: &Conf, |
| 38 | | section: &mut ConfigSection, |
| 39 | | option_name: &str, |
| 40 | | option_value: &str, |
| 41 | | ) -> OptionChanged { |
| 42 | | if option_name.is_empty() { |
| 43 | | return OptionChanged::Error; |
| 34 | macro_rules! option { |
| 35 | (StringOption, $option_name:ident, $description:literal, $default:literal) => { |
| 36 | paste::item! { |
| 37 | fn [<create_option_ $option_name>](section: &mut SectionHandleMut) { |
| 38 | let option_name = stringify!($option_name); |
| 39 | let option_settings = StringOptionSettings::new(option_name) |
| 40 | .description($description) |
| 41 | .default_value($default); |
| 42 | |
| 43 | section.new_string_option(option_settings) |
| 44 | .expect(&format!("Can't create option {}", option_name)); |
| 45 | } |
| 44 | 46 | } |
| 45 | 47 | |
| 46 | | let option_args: Vec<&str> = option_name.splitn(2, '.').collect(); |
| 48 | paste::item! { |
| 49 | pub fn [<$option_name>](&self) -> String { |
| 50 | let option_name = stringify!($option_name); |
| 47 | 51 | |
| 48 | | if option_args.len() != 2 { |
| 49 | | return OptionChanged::Error; |
| 52 | let option = self.0.search_option(option_name) |
| 53 | .expect(&format!("Couldn't find option {} in section {}", |
| 54 | option_name, self.0.name())); |
| 55 | |
| 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 | } |
| 62 | } |
| 50 | 63 | } |
| 64 | }; |
| 51 | 65 | |
| 52 | | let server_name = option_args[0]; |
| 66 | (EvaluatedStringOption, $option_name:ident, $description:literal, $default:literal) => { |
| 67 | paste::item! { |
| 68 | fn [<create_option_ $option_name>](section: &mut SectionHandleMut) { |
| 69 | 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); |
| 53 | 73 | |
| 54 | | { |
| 55 | | let mut servers_borrow = self.servers.borrow_mut(); |
| 74 | section.new_string_option(option_settings) |
| 75 | .expect(&format!("Can't create option {}", option_name)); |
| 76 | } |
| 77 | } |
| 56 | 78 | |
| 57 | | // We are reading the config, if the server doesn't yet exists |
| 58 | | // we need to create it before setting the option and running |
| 59 | | // the option change callback. |
| 60 | | if !servers_borrow.contains_key(server_name) { |
| 61 | | let server = MatrixServer::new(server_name, &self, section); |
| 62 | | servers_borrow.insert(server_name.to_owned(), server); |
| 79 | paste::item! { |
| 80 | pub fn [<$option_name>](&self) -> String { |
| 81 | let option_name = stringify!($option_name); |
| 82 | |
| 83 | let option = self.0.search_option(option_name) |
| 84 | .expect(&format!("Couldn't find option {} in section {}", |
| 85 | option_name, self.0.name())); |
| 86 | |
| 87 | if let ConfigOption::String(o) = option { |
| 88 | Weechat::eval_string_expression(&o.value()) |
| 89 | .expect(&format!( |
| 90 | "Can't evaluate string expression for option {} in section {}", |
| 91 | option_name, |
| 92 | self.0.name()) |
| 93 | ) |
| 94 | } else { |
| 95 | panic!("Incorect option type for option {} in section {}", |
| 96 | option_name, self.0.name()); |
| 97 | } |
| 63 | 98 | } |
| 64 | 99 | } |
| 100 | }; |
| 101 | } |
| 65 | 102 | |
| 66 | | let option = section.search_option(option_name); |
| 103 | macro_rules! section { |
| 104 | ($section:ident { $({$option_type:ident, $option_name:ident, $($option:tt)*}), * }) => { |
| 105 | paste::item! { |
| 106 | pub struct [<$section:camel Section>]<'a>(SectionHandle<'a>); |
| 67 | 107 | |
| 68 | | if let Some(o) = option { |
| 69 | | o.set(option_value, true) |
| 70 | | } else { |
| 71 | | OptionChanged::NotFound |
| 108 | impl<'a> [<$section:camel Section>]<'a> { |
| 109 | pub fn create(config: &mut Config) { |
| 110 | let section_settings = ConfigSectionSettings::new(stringify!($section)); |
| 111 | |
| 112 | let mut $section = config.new_section(section_settings) |
| 113 | .expect(&format!("Can't create config section {}", stringify!($section))); |
| 114 | |
| 115 | [<$section:camel Section>]::create_options(&mut $section); |
| 116 | } |
| 117 | |
| 118 | pub fn create_options(section: &mut SectionHandleMut) { |
| 119 | $( |
| 120 | [<$section:camel Section>]::[<create_option_ $option_name>](section); |
| 121 | )* |
| 122 | } |
| 123 | |
| 124 | $( |
| 125 | option!($option_type, $option_name, $($option)*); |
| 126 | )* |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | macro_rules! config { |
| 133 | ($(Section $section:ident { $({$($option:tt)*}), * }), *) => { |
| 134 | pub struct Config(weechat::config::Config); |
| 135 | |
| 136 | impl Deref for Config { |
| 137 | type Target = weechat::config::Config; |
| 138 | |
| 139 | fn deref(&self) -> &Self::Target { |
| 140 | &self.0 |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | impl DerefMut for Config { |
| 145 | fn deref_mut(&mut self) -> &mut Self::Target { |
| 146 | &mut self.0 |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | impl Config { |
| 151 | fn new(config: weechat::config::Config) -> Self { |
| 152 | let mut config = Config(config); |
| 153 | config.create_sections(); |
| 154 | |
| 155 | config |
| 156 | } |
| 157 | |
| 158 | paste::item! { |
| 159 | fn create_sections(&mut self) { |
| 160 | $( |
| 161 | paste::expr! { [<$section:camel Section>]::create(self) }; |
| 162 | )* |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | paste::item! { |
| 167 | $( |
| 168 | pub fn $section(&self) -> [<$section:camel Section>] { |
| 169 | let section_name = stringify!($section); |
| 170 | let section = self.0.search_section(section_name) |
| 171 | .expect(&format!("Couldn't find section {}", section_name)); |
| 172 | |
| 173 | paste::item! { [<$section:camel Section>](section) } |
| 174 | } |
| 175 | )* |
| 176 | } |
| 72 | 177 | } |
| 178 | |
| 179 | $( |
| 180 | section!($section { $({$($option)*}), * }); |
| 181 | )* |
| 73 | 182 | } |
| 74 | 183 | } |
| 75 | 184 | |
| 76 | | impl Config { |
| 77 | | pub fn new(_weechat: &Weechat, servers: &Servers) -> Config { |
| 185 | config!( |
| 186 | Section look { |
| 187 | {StringOption, encrypted_room_sign, |
| 188 | "A sign that is used to show that the current room is encrypted", |
| 189 | "🔒"} |
| 190 | } |
| 191 | ); |
| 192 | |
| 193 | impl ConfigHandle { |
| 194 | pub fn new(_weechat: &Weechat, servers: &Servers) -> ConfigHandle { |
| 78 | 195 | let config = Weechat::config_new("matrix-rust") |
| 79 | 196 | .expect("Can't create new config"); |
| 80 | 197 | |
| 81 | | let config = Config { |
| 198 | let config = Config::new(config); |
| 199 | |
| 200 | let config = ConfigHandle { |
| 82 | 201 | inner: Rc::new(RefCell::new(config)), |
| 83 | 202 | servers: servers.clone(), |
| 84 | 203 | }; |
| @@ -95,6 +214,7 @@ impl Config { |
| 95 | 214 | }, |
| 96 | 215 | ) |
| 97 | 216 | .set_read_callback(config.clone()); |
| 217 | |
| 98 | 218 | { |
| 99 | 219 | let mut config_borrow = config.borrow_mut(); |
| 100 | 220 | |
| @@ -106,11 +226,54 @@ impl Config { |
| 106 | 226 | config |
| 107 | 227 | } |
| 108 | 228 | |
| 109 | | pub fn borrow(&self) -> Ref<'_, weechat::config::Config> { |
| 229 | pub fn borrow(&self) -> Ref<'_, Config> { |
| 110 | 230 | self.inner.borrow() |
| 111 | 231 | } |
| 112 | 232 | |
| 113 | | pub fn borrow_mut(&self) -> RefMut<'_, weechat::config::Config> { |
| 233 | pub fn borrow_mut(&self) -> RefMut<'_, Config> { |
| 114 | 234 | self.inner.borrow_mut() |
| 115 | 235 | } |
| 116 | 236 | } |
| 237 | |
| 238 | impl SectionReadCallback for ConfigHandle { |
| 239 | fn callback( |
| 240 | &mut self, |
| 241 | _: &Weechat, |
| 242 | _: &Conf, |
| 243 | section: &mut ConfigSection, |
| 244 | option_name: &str, |
| 245 | option_value: &str, |
| 246 | ) -> OptionChanged { |
| 247 | if option_name.is_empty() { |
| 248 | return OptionChanged::Error; |
| 249 | } |
| 250 | |
| 251 | let option_args: Vec<&str> = option_name.splitn(2, '.').collect(); |
| 252 | |
| 253 | if option_args.len() != 2 { |
| 254 | return OptionChanged::Error; |
| 255 | } |
| 256 | |
| 257 | let server_name = option_args[0]; |
| 258 | |
| 259 | { |
| 260 | let mut servers_borrow = self.servers.borrow_mut(); |
| 261 | |
| 262 | // We are reading the config, if the server doesn't yet exists |
| 263 | // we need to create it before setting the option and running |
| 264 | // the option change callback. |
| 265 | if !servers_borrow.contains_key(server_name) { |
| 266 | let server = MatrixServer::new(server_name, &self, section); |
| 267 | servers_borrow.insert(server_name.to_owned(), server); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | let option = section.search_option(option_name); |
| 272 | |
| 273 | if let Some(o) = option { |
| 274 | o.set(option_value, true) |
| 275 | } else { |
| 276 | OptionChanged::NotFound |
| 277 | } |
| 278 | } |
| 279 | } |