Commit f6f6346137

f6f63461372eda63abe39e473e78db535ed3961f

parent: c552c43bcb

Unsigned

Damir Jelić <poljar@termina.org.uk> · 2020-04-05 12:28 UTC

config: Use the callback trait.

Layout: unified · split

src/config.rs +78 −73
@@ -14,9 +14,10 @@
1414//! The config options created here will be alive as long as the plugin is
1515//! loaded so they don't need to be freed manually. The drop implementation of
1616//! the section will do so.
17use crate::{MatrixServer, Servers};
17use crate::{MatrixServer, Servers, ServersHandle};
1818use weechat::config::{
1919 Conf, ConfigSection, ConfigSectionSettings, OptionChanged,
20 SectionReadCallback,
2021};
2122use weechat::Weechat;
2223
@@ -24,93 +25,94 @@ use std::cell::{Ref, RefCell, RefMut};
2425use std::rc::{Rc, Weak};
2526
2627#[derive(Clone)]
27pub struct Config(Rc<RefCell<weechat::config::Config>>);
28pub struct Config {
29 inner: Rc<RefCell<weechat::config::Config>>,
30 servers: Servers,
31}
2832
2933#[derive(Clone, Default)]
30pub struct ConfigHandle(Weak<RefCell<weechat::config::Config>>);
34pub struct ConfigHandle {
35 inner: Weak<RefCell<weechat::config::Config>>,
36 servers: ServersHandle,
37}
38
39impl SectionReadCallback for Config {
40 fn callback(
41 &mut self,
42 _: &Weechat,
43 _: &Conf,
44 section: &mut ConfigSection,
45 option_name: &str,
46 option_value: &str,
47 ) -> OptionChanged {
48 if option_name.is_empty() {
49 return OptionChanged::Error;
50 }
51
52 let option_args: Vec<&str> = option_name.splitn(2, '.').collect();
53
54 if option_args.len() != 2 {
55 return OptionChanged::Error;
56 }
57
58 let server_name = option_args[0];
59
60 {
61 let mut servers_borrow = self.servers.borrow_mut();
62
63 // We are reading the config, if the server doesn't yet exists
64 // we need to create it before setting the option and running
65 // the option change callback.
66 if !servers_borrow.contains_key(server_name) {
67 let server = MatrixServer::new(server_name, &self, section);
68 servers_borrow.insert(server_name.to_owned(), server);
69 }
70 }
71
72 let option = section.search_option(option_name);
73
74 if let Some(o) = option {
75 o.set(option_value, true)
76 } else {
77 OptionChanged::NotFound
78 }
79 }
80}
3181
3282impl ConfigHandle {
3383 pub fn upgrade(&self) -> Config {
34 Config(
35 self.0
84 Config {
85 inner: self
86 .inner
3687 .upgrade()
3788 .expect("Config got deleted before plugin unload"),
38 )
39 }
40}
41
42fn server_write_cb(
43 _weechat: &Weechat,
44 config: &Conf,
45 section: &mut ConfigSection,
46) {
47 config.write_section(section.name());
48
49 for option in section.options() {
50 config.write_option(option);
89 servers: self.servers.upgrade(),
90 }
5191 }
5292}
5393
5494impl Config {
55 pub fn new(weechat: &Weechat, servers: &Servers) -> Config {
95 pub fn new(_weechat: &Weechat, servers: &Servers) -> Config {
5696 let config = Weechat::config_new("matrix-rust")
5797 .expect("Can't create new config");
5898
59 let servers = servers.clone_weak();
60
61 let config = Config(Rc::new(RefCell::new(config)));
62
63 let weak_config = config.clone_weak();
99 let config = Config {
100 inner: Rc::new(RefCell::new(config)),
101 servers: servers.clone(),
102 };
64103
65104 let server_section_options = ConfigSectionSettings::new("server")
66 .set_write_callback(server_write_cb)
67 .set_read_callback(
68 move |_, _config, section, option_name, value| {
69 let config = weak_config.clone();
70 let servers = servers.clone();
71
72 if option_name.is_empty() {
73 return OptionChanged::Error;
74 }
75
76 let option_args: Vec<&str> =
77 option_name.splitn(2, '.').collect();
78
79 if option_args.len() != 2 {
80 return OptionChanged::Error;
81 }
82
83 let server_name = option_args[0];
84
85 {
86 let servers = servers.upgrade();
87 let mut servers_borrow = servers.borrow_mut();
88
89 // We are reading the config, if the server doesn't yet exists
90 // we need to create it before setting the option and running
91 // the option change callback.
92 if !servers_borrow.contains_key(server_name) {
93 let config = config.upgrade();
94 let server = MatrixServer::new(
95 server_name,
96 &config,
97 section,
98 );
99 servers_borrow
100 .insert(server_name.to_owned(), server);
101 }
102 }
103
104 let option = section.search_option(option_name);
105
106 if let Some(o) = option {
107 o.set(value, true)
108 } else {
109 OptionChanged::NotFound
105 .set_write_callback(
106 |_weechat: &Weechat,
107 config: &Conf,
108 section: &mut ConfigSection| {
109 config.write_section(section.name());
110 for option in section.options() {
111 config.write_option(option);
110112 }
111113 },
112 );
113
114 )
115 .set_read_callback(config.clone());
114116 {
115117 let mut config_borrow = config.borrow_mut();
116118
@@ -123,14 +125,17 @@ impl Config {
123125 }
124126
125127 pub fn borrow(&self) -> Ref<'_, weechat::config::Config> {
126 self.0.borrow()
128 self.inner.borrow()
127129 }
128130
129131 pub fn borrow_mut(&self) -> RefMut<'_, weechat::config::Config> {
130 self.0.borrow_mut()
132 self.inner.borrow_mut()
131133 }
132134
133135 pub fn clone_weak(&self) -> ConfigHandle {
134 ConfigHandle(Rc::downgrade(&self.0))
136 ConfigHandle {
137 inner: Rc::downgrade(&self.inner),
138 servers: self.servers.clone_weak(),
139 }
135140 }
136141}