Commit a6261d01e4

a6261d01e4e1e42411dec340db301d6af0b86493

parent: 439aa33fc5

Unsigned

Damir Jelić <poljar@termina.org.uk> · 2020-07-18 12:52 UTC

weechat-matrix: Update to the latest weechat crate version.

Layout: unified · split

Cargo.lock +11 −13
@@ -132,9 +132,9 @@ checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff"
132132
133133[[package]]
134134name = "bindgen"
135version = "0.54.0"
135version = "0.54.1"
136136source = "registry+https://github.com/rust-lang/crates.io-index"
137checksum = "66c0bb6167449588ff70803f4127f0684f9063097eca5016f37eb52b92c2cf36"
137checksum = "f4d49b80beb70d76cdac92f5681e666f9a697c737c4f4117a67229a0386dc736"
138138dependencies = [
139139 "bitflags",
140140 "cexpr",
@@ -796,9 +796,9 @@ checksum = "b294d6fa9ee409a054354afc4352b0b9ef7ca222c69b8812cbea9e7d2bf3783f"
796796
797797[[package]]
798798name = "libc"
799version = "0.2.71"
799version = "0.2.72"
800800source = "registry+https://github.com/rust-lang/crates.io-index"
801checksum = "9457b06509d27052635f90d6466700c65095fdf75409b3fbdd903e988b886f49"
801checksum = "a9f8082297d534141b30c8d39e9b1773713ab50fdbe4ff30f750d063b3bfd701"
802802
803803[[package]]
804804name = "libloading"
@@ -1777,9 +1777,9 @@ dependencies = [
17771777
17781778[[package]]
17791779name = "syn"
1780version = "1.0.33"
1780version = "1.0.34"
17811781source = "registry+https://github.com/rust-lang/crates.io-index"
1782checksum = "e8d5d96e8cbb005d6959f119f773bfaebb5684296108fb32600c00cde305b2cd"
1782checksum = "936cae2873c940d92e697597c5eee105fb570cd5689c695806f672883653349b"
17831783dependencies = [
17841784 "proc-macro2",
17851785 "quote",
@@ -2203,13 +2203,15 @@ dependencies = [
22032203[[package]]
22042204name = "weechat"
22052205version = "0.1.0"
2206source = "git+https://github.com/poljar/rust-weechat#647e9a93560e6febab50b1c9456b30a8d440c22b"
2206source = "git+https://github.com/poljar/rust-weechat#7defc24d659aa920d519f47e7cb25982926647a9"
22072207dependencies = [
22082208 "async-task",
22092209 "async-trait",
22102210 "futures",
22112211 "libc",
2212 "paste",
22122213 "pipe-channel",
2214 "strum",
22132215 "weechat-macro",
22142216 "weechat-sys",
22152217]
@@ -2217,7 +2219,7 @@ dependencies = [
22172219[[package]]
22182220name = "weechat-macro"
22192221version = "0.1.0"
2220source = "git+https://github.com/poljar/rust-weechat#647e9a93560e6febab50b1c9456b30a8d440c22b"
2222source = "git+https://github.com/poljar/rust-weechat#7defc24d659aa920d519f47e7cb25982926647a9"
22212223dependencies = [
22222224 "libc",
22232225 "proc-macro2",
@@ -2232,23 +2234,19 @@ dependencies = [
22322234 "async-std",
22332235 "async-trait",
22342236 "clap",
2235 "libc",
22362237 "matrix-sdk",
2237 "paste",
2238 "strum",
22392238 "tokio",
22402239 "tracing",
22412240 "tracing-subscriber",
22422241 "url",
22432242 "uuid",
22442243 "weechat",
2245 "weechat-sys",
22462244]
22472245
22482246[[package]]
22492247name = "weechat-sys"
22502248version = "0.1.0"
2251source = "git+https://github.com/poljar/rust-weechat#647e9a93560e6febab50b1c9456b30a8d440c22b"
2249source = "git+https://github.com/poljar/rust-weechat#7defc24d659aa920d519f47e7cb25982926647a9"
22522250dependencies = [
22532251 "bindgen",
22542252 "libc",
Cargo.toml +3 −5
@@ -10,7 +10,6 @@ name = "matrix"
1010crate-type = ["cdylib"]
1111
1212[dependencies]
13libc = "0.2.71"
1413url = "2.1.1"
1514tokio = { version = "0.2.21", features = [ "rt-threaded", "sync" ] }
1615tracing = "0.1.15"
@@ -18,12 +17,11 @@ tracing-subscriber = "0.2.6"
1817async-std = { version = "1.6.2", features = [ "unstable" ] }
1918clap = "2.33.1"
2019async-trait = "0.1.36"
21paste = "0.1"
22strum = "0.18.0"
2320uuid = { version = "0.8.1", features = ["v4"] }
2421
25weechat = { git = "https://github.com/poljar/rust-weechat", features = ["async-executor"] }
26weechat-sys = { git = "https://github.com/poljar/rust-weechat" }
22[dependencies.weechat]
23git = "https://github.com/poljar/rust-weechat"
24features = ["async-executor", "config-macro"]
2725
2826[dependencies.matrix-sdk]
2927git = "https://github.com/matrix-org/matrix-rust-sdk/"
src/commands.rs +2 −2
@@ -270,7 +270,7 @@ impl Commands {
270270 weechat: &Weechat,
271271 servers: &Servers,
272272 config: &ConfigHandle,
273 ) -> Commands {
273 ) -> Result<Commands, ()> {
274274 let matrix_settings = CommandSettings::new("matrix")
275275 .description("Matrix chat protocol command.")
276276 .add_argument("server add <server-name> <hostname>[:<port>]")
@@ -301,6 +301,6 @@ Use /matrix [command] help to find out more.\n",
301301 },
302302 );
303303
304 Commands { _matrix: matrix }
304 Ok(Commands { _matrix: matrix? })
305305 }
306306}
src/config.rs +1 −1
@@ -17,7 +17,7 @@
1717
1818use crate::{MatrixServer, Servers};
1919use weechat::config::{Conf, OptionChanged, SectionReadCallback};
20use weechat::Weechat;
20use weechat::{Weechat, config};
2121
2222use std::cell::{Ref, RefCell, RefMut};
2323use std::rc::Rc;
src/config_macros.rs deleted −317
@@ -1,317 +0,0 @@
1macro_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)]
30macro_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
42macro_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
63macro_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
116macro_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/// ```
254macro_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}
src/lib.rs +1 −4
@@ -1,8 +1,5 @@
11#![feature(async_closure)]
22
3#[macro_use]
4mod config_macros;
5
63mod commands;
74mod config;
85mod debug;
@@ -143,7 +140,7 @@ impl WeechatPlugin for Matrix {
143140 fn init(weechat: &Weechat, _args: ArgsWeechat) -> Result<Self, ()> {
144141 let servers = Servers::new();
145142 let config = ConfigHandle::new(&servers);
146 let commands = Commands::hook_all(weechat, &servers, &config);
143 let commands = Commands::hook_all(weechat, &servers, &config)?;
147144
148145 // TODO move the bar creation into a separate file.
149146 let status_bar =