Commit e26032e9a7

e26032e9a7bf37dfc105a6d284f505bc73d61f30

parent: d53278181b

Unsigned

Damir Jelić <poljar@termina.org.uk> · 2020-06-27 16:12 UTC

config: Remove the unused options and document the config!() macro.

Layout: unified · split

src/config.rs +16 −62
@@ -16,85 +16,36 @@
1616//! the section will do so.
1717use crate::{MatrixServer, Servers};
1818use weechat::config::{
19 BooleanOptionSettings, Conf, ConfigOption, ConfigSection,
20 ConfigSectionSettings, IntegerOptionSettings, OptionChanged, SectionHandle,
21 SectionHandleMut, SectionReadCallback, StringOptionSettings, ColorOptionSettings,
19 Conf, ConfigOption, ConfigSection, ConfigSectionSettings, OptionChanged,
20 SectionHandle, SectionHandleMut, SectionReadCallback, StringOptionSettings,
2221};
2322use weechat::Weechat;
2423
25use strum::EnumVariantNames;
26use strum::VariantNames;
27
2824use std::cell::{Ref, RefCell, RefMut};
2925use std::ops::{Deref, DerefMut};
3026use std::rc::Rc;
3127
32#[derive(Clone)]
33pub struct ConfigHandle {
34 inner: Rc<RefCell<Config>>,
35 servers: Servers,
36}
37
38#[derive(EnumVariantNames)]
39#[strum(serialize_all = "kebab_case")]
40pub enum Test {
41 First,
42 Second,
43}
44
45impl Default for Test {
46 fn default() -> Self {
47 Test::First
48 }
49}
50
51impl From<i32> for Test {
52 fn from(value: i32) -> Self {
53 match value {
54 0 => Test::First,
55 1 => Test::Second,
56 _ => unreachable!(),
57 }
58 }
59}
60
6128config!(
6229 Section look {
6330 encrypted_room_sign: String {
31 // Description.
6432 "A sign that is used to show that the current room is encrypted",
33 // Default value.
6534 "🔒",
6635 },
67
68 what: EvaluatedString {
69 "test",
70 "test2",
71 },
72
73 int_test: Integer {
74 "test",
75 5,
76 0..10,
77 },
78
79 enum_test: Enum {
80 "test",
81 Test,
82 },
83
84 bool_test: bool {
85 "description",
86 false,
87 },
88
89 color_test: Color {
90 "color",
91 "darkgray",
92 },
9336 },
9437);
9538
39/// A wrapper for our config struct that can be cloned around.
40#[derive(Clone)]
41pub struct ConfigHandle {
42 inner: Rc<RefCell<Config>>,
43 servers: Servers,
44}
45
9646impl ConfigHandle {
97 pub fn new(_weechat: &Weechat, servers: &Servers) -> ConfigHandle {
47 /// Create a new config and wrap it in our config handle.
48 pub fn new(servers: &Servers) -> ConfigHandle {
9849 let config = Weechat::config_new("matrix-rust")
9950 .expect("Can't create new config");
10051
@@ -105,6 +56,9 @@ impl ConfigHandle {
10556 servers: servers.clone(),
10657 };
10758
59 // The server section is special since it has a custom section read and
60 // write implementations to support subsections for every configured
61 // server.
10862 let server_section_options = ConfigSectionSettings::new("server")
10963 .set_write_callback(
11064 |_weechat: &Weechat,
src/config_macros.rs +114 −9
@@ -1,12 +1,4 @@
1use weechat::config::{
2 BooleanOptionSettings, Conf, ConfigOption, ConfigSection,
3 ConfigSectionSettings, IntegerOptionSettings, OptionChanged, SectionHandle,
4 SectionHandleMut, SectionReadCallback, StringOptionSettings, ColorOptionSettings,
5};
6use weechat::Weechat;
7
8use strum::VariantNames;
9
1#[allow(unused_macros)]
102macro_rules! string_create {
113 ($option_name:ident, $description:literal, $default:literal) => {
124 paste::item! {
@@ -23,6 +15,7 @@ macro_rules! string_create {
2315 };
2416}
2517
18#[allow(unused_macros)]
2619macro_rules! color_create {
2720 ($option_name:ident, $description:literal, $default:literal) => {
2821 paste::item! {
@@ -39,6 +32,7 @@ macro_rules! color_create {
3932 };
4033}
4134
35#[allow(unused_macros)]
4236macro_rules! bool_create {
4337 ($option_name:ident, $description:literal, $default:literal) => {
4438 paste::item! {
@@ -55,6 +49,7 @@ macro_rules! bool_create {
5549 };
5650}
5751
52#[allow(unused_macros)]
5853macro_rules! integer_create {
5954 ($option_name:ident, $description:literal, $default:literal, $min:literal, $max:literal) => {
6055 paste::item! {
@@ -74,6 +69,7 @@ macro_rules! integer_create {
7469 };
7570}
7671
72#[allow(unused_macros)]
7773macro_rules! enum_create {
7874 ($option_name:ident, $description:literal, $out_type:ty) => {
7975 paste::item! {
@@ -201,8 +197,117 @@ macro_rules! section {
201197 }
202198}
203199
200/// Declare a Weechat configuration file.
201///
202/// This will generate a struct called `Config` which wraps the Weechat struct
203/// of the same name. The generated struct will have accessors for every
204/// section and option that is declared.
205///
206/// The generated struct dereferences into the Weechat `Config` struct so
207/// additional sections and options can be created the usual way as well.
208///
209/// The config still needs to be created in the `init()` method of the plugin.
210///
211/// # Example
212/// ```
213///
214/// #[derive(EnumVariantNames)]
215/// #[strum(serialize_all = "kebab_case")]
216/// pub enum ServerBufferMerge {
217/// MergeWithCore,
218/// MergeWithoutCore,
219/// Independent,
220/// }
221///
222/// impl Default for ServerBufferMerge {
223/// fn default() -> Self {
224/// ServerBufferMerge::MergeWithCore
225/// }
226/// }
227///
228/// impl From<i32> for ServerBufferMerge {
229/// fn from(value: i32) -> Self {
230/// match value {
231/// 0 => ServerBufferMerge::MergeWithCore,
232/// 1 => ServerBufferMerge::MergeWithoutCore,
233/// 2 => ServerBufferMerge::Independent,
234/// _ => unreachable!(),
235/// }
236/// }
237/// }
238///
239/// config!(
240/// Section look {
241/// encrypted_room_sign: String {
242/// // Description.
243/// "A sign that is used to show that the current room is encrypted",
244///
245/// // Default value.
246/// "🔒",
247/// },
248///
249/// server_buffer: Enum {
250/// // Description.
251/// "Merge server buffers",
252///
253/// // This is an enum that needs to have the following traits
254/// // implemented:
255/// // * Default - To define the default value of the option.
256/// // * From<i32> - To convert the internal Weechat integer option
257/// // to the enum.
258/// // * VariantNames - To get the string representation of the
259/// // enum variants. This is a trait defined in the strum library,
260/// // a simple macro that derives an implementation is provided by
261/// // strum.
262/// ServerBufferMerge,
263/// },
264///
265/// quote_fg: Color {
266/// // Description.
267/// "Foreground color for Matrix style blockquotes",
268///
269/// // Default value.
270/// "lightgreen",
271/// },
272/// },
273///
274/// Section network {
275/// username: EvaluatedString {
276/// // Description.
277/// "The username that will be used to log in to the server \
278/// (note: content is evaluated, see /help eval)",
279///
280/// // Default value.
281/// "",
282/// },
283///
284/// timeout: Integer {
285/// // Description.
286/// "A timeout (in seconds) that determines how long we should wait \
287/// for a request to finish before aborting.",
288///
289/// // Default value.
290/// 30,
291///
292/// // The range that the value is allowed to have, note that both of
293/// // those are inclusive.
294/// 0..100,
295/// },
296///
297/// autoconnect: bool {
298/// // Description.
299/// "Automatically connect to the server when Weechat is starting",
300///
301/// // Default value.
302/// false,
303/// },
304/// }
305/// );
306/// ```
204307macro_rules! config {
205308 ($(Section $section:ident { $($option:tt)* }), * $(,)?) => {
309 #[allow(unused_imports)]
310 use strum::VariantNames;
206311 pub struct Config(weechat::config::Config);
207312
208313 impl Deref for Config {
src/lib.rs +26 −10
@@ -1,6 +1,7 @@
11#![feature(async_closure)]
22
3#[macro_use] mod config_macros;
3#[macro_use]
4mod config_macros;
45
56mod commands;
67mod config;
@@ -68,11 +69,14 @@ impl Matrix {
6869 servers: &mut HashMap<String, MatrixServer>,
6970 config: &ConfigHandle,
7071 ) {
72 // TODO change this to matrix.org.
7173 let server_name = "localhost";
74
7275 let mut config_borrow = config.borrow_mut();
7376 let mut section = config_borrow
7477 .search_section_mut("server")
7578 .expect("Can't get server section");
79
7680 let server = MatrixServer::new(server_name, config, &mut section);
7781 servers.insert(server_name.to_owned(), server);
7882 }
@@ -84,8 +88,10 @@ impl BarItemCallback for Servers {
8488
8589 for server in servers.values() {
8690 let server = server.inner();
91
8792 for room in server.room_buffers().values() {
8893 let room_buffer = room.weechat_buffer();
94
8995 if &room_buffer == buffer {
9096 if room.room().is_encrypted() {
9197 return server.config().look().encrypted_room_sign();
@@ -101,20 +107,16 @@ impl BarItemCallback for Servers {
101107impl WeechatPlugin for Matrix {
102108 fn init(weechat: &Weechat, _args: ArgsWeechat) -> Result<Self, ()> {
103109 let servers = Servers::new();
104 let config = ConfigHandle::new(weechat, &servers);
110 let config = ConfigHandle::new(&servers);
105111 let commands = Commands::hook_all(weechat, &servers, &config);
112
113 // TODO move the bar creation into a separate file.
106114 let status_bar =
107115 Weechat::new_bar_item("matrix_modes", servers.clone())?;
108116
117 // TODO make this configurable.
109118 tracing_subscriber::fmt().with_writer(debug::Debug).init();
110119
111 let matrix = Matrix {
112 servers: servers.clone(),
113 commands,
114 config: config.clone(),
115 status_bar,
116 };
117
118120 {
119121 let config_borrow = config.borrow();
120122 if config_borrow.read().is_err() {
@@ -129,18 +131,32 @@ impl WeechatPlugin for Matrix {
129131 }
130132 }
131133
134 let plugin = Matrix {
135 servers: servers.clone(),
136 commands,
137 config: config.clone(),
138 status_bar,
139 };
140
132141 Weechat::spawn(async move {
133142 let mut servers = servers.borrow_mut();
134143 Matrix::autoconnect(&mut servers);
135144 });
136145
137 Ok(matrix)
146 Ok(plugin)
138147 }
139148}
140149
141150impl Drop for Matrix {
142151 fn drop(&mut self) {
143152 let mut servers = self.servers.borrow_mut();
153
154 // Buffer close callbacks get called after this, so disconnect here so
155 // we don't leave all our rooms.
156 //
157 // TODO set a flag on the server as well so we don't even try to leave
158 // the rooms, once leaving the rooms is implemented when the buffer gets
159 // closed.
144160 for server in servers.values_mut() {
145161 server.disconnect();
146162 }