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"
132 132
133[[package]] 133[[package]]
134name = "bindgen" 134name = "bindgen"
135version = "0.54.0" 135version = "0.54.1"
136source = "registry+https://github.com/rust-lang/crates.io-index" 136source = "registry+https://github.com/rust-lang/crates.io-index"
137checksum = "66c0bb6167449588ff70803f4127f0684f9063097eca5016f37eb52b92c2cf36" 137checksum = "f4d49b80beb70d76cdac92f5681e666f9a697c737c4f4117a67229a0386dc736"
138dependencies = [ 138dependencies = [
139 "bitflags", 139 "bitflags",
140 "cexpr", 140 "cexpr",
@@ -796,9 +796,9 @@ checksum = "b294d6fa9ee409a054354afc4352b0b9ef7ca222c69b8812cbea9e7d2bf3783f"
796 796
797[[package]] 797[[package]]
798name = "libc" 798name = "libc"
799version = "0.2.71" 799version = "0.2.72"
800source = "registry+https://github.com/rust-lang/crates.io-index" 800source = "registry+https://github.com/rust-lang/crates.io-index"
801checksum = "9457b06509d27052635f90d6466700c65095fdf75409b3fbdd903e988b886f49" 801checksum = "a9f8082297d534141b30c8d39e9b1773713ab50fdbe4ff30f750d063b3bfd701"
802 802
803[[package]] 803[[package]]
804name = "libloading" 804name = "libloading"
@@ -1777,9 +1777,9 @@ dependencies = [
1777 1777
1778[[package]] 1778[[package]]
1779name = "syn" 1779name = "syn"
1780version = "1.0.33" 1780version = "1.0.34"
1781source = "registry+https://github.com/rust-lang/crates.io-index" 1781source = "registry+https://github.com/rust-lang/crates.io-index"
1782checksum = "e8d5d96e8cbb005d6959f119f773bfaebb5684296108fb32600c00cde305b2cd" 1782checksum = "936cae2873c940d92e697597c5eee105fb570cd5689c695806f672883653349b"
1783dependencies = [ 1783dependencies = [
1784 "proc-macro2", 1784 "proc-macro2",
1785 "quote", 1785 "quote",
@@ -2203,13 +2203,15 @@ dependencies = [
2203[[package]] 2203[[package]]
2204name = "weechat" 2204name = "weechat"
2205version = "0.1.0" 2205version = "0.1.0"
2206source = "git+https://github.com/poljar/rust-weechat#647e9a93560e6febab50b1c9456b30a8d440c22b" 2206source = "git+https://github.com/poljar/rust-weechat#7defc24d659aa920d519f47e7cb25982926647a9"
2207dependencies = [ 2207dependencies = [
2208 "async-task", 2208 "async-task",
2209 "async-trait", 2209 "async-trait",
2210 "futures", 2210 "futures",
2211 "libc", 2211 "libc",
2212 "paste",
2212 "pipe-channel", 2213 "pipe-channel",
2214 "strum",
2213 "weechat-macro", 2215 "weechat-macro",
2214 "weechat-sys", 2216 "weechat-sys",
2215] 2217]
@@ -2217,7 +2219,7 @@ dependencies = [
2217[[package]] 2219[[package]]
2218name = "weechat-macro" 2220name = "weechat-macro"
2219version = "0.1.0" 2221version = "0.1.0"
2220source = "git+https://github.com/poljar/rust-weechat#647e9a93560e6febab50b1c9456b30a8d440c22b" 2222source = "git+https://github.com/poljar/rust-weechat#7defc24d659aa920d519f47e7cb25982926647a9"
2221dependencies = [ 2223dependencies = [
2222 "libc", 2224 "libc",
2223 "proc-macro2", 2225 "proc-macro2",
@@ -2232,23 +2234,19 @@ dependencies = [
2232 "async-std", 2234 "async-std",
2233 "async-trait", 2235 "async-trait",
2234 "clap", 2236 "clap",
2235 "libc",
2236 "matrix-sdk", 2237 "matrix-sdk",
2237 "paste",
2238 "strum",
2239 "tokio", 2238 "tokio",
2240 "tracing", 2239 "tracing",
2241 "tracing-subscriber", 2240 "tracing-subscriber",
2242 "url", 2241 "url",
2243 "uuid", 2242 "uuid",
2244 "weechat", 2243 "weechat",
2245 "weechat-sys",
2246] 2244]
2247 2245
2248[[package]] 2246[[package]]
2249name = "weechat-sys" 2247name = "weechat-sys"
2250version = "0.1.0" 2248version = "0.1.0"
2251source = "git+https://github.com/poljar/rust-weechat#647e9a93560e6febab50b1c9456b30a8d440c22b" 2249source = "git+https://github.com/poljar/rust-weechat#7defc24d659aa920d519f47e7cb25982926647a9"
2252dependencies = [ 2250dependencies = [
2253 "bindgen", 2251 "bindgen",
2254 "libc", 2252 "libc",
Cargo.toml +3 −5
@@ -10,7 +10,6 @@ name = "matrix"
10crate-type = ["cdylib"] 10crate-type = ["cdylib"]
11 11
12[dependencies] 12[dependencies]
13libc = "0.2.71"
14url = "2.1.1" 13url = "2.1.1"
15tokio = { version = "0.2.21", features = [ "rt-threaded", "sync" ] } 14tokio = { version = "0.2.21", features = [ "rt-threaded", "sync" ] }
16tracing = "0.1.15" 15tracing = "0.1.15"
@@ -18,12 +17,11 @@ tracing-subscriber = "0.2.6"
18async-std = { version = "1.6.2", features = [ "unstable" ] } 17async-std = { version = "1.6.2", features = [ "unstable" ] }
19clap = "2.33.1" 18clap = "2.33.1"
20async-trait = "0.1.36" 19async-trait = "0.1.36"
21paste = "0.1"
22strum = "0.18.0"
23uuid = { version = "0.8.1", features = ["v4"] } 20uuid = { version = "0.8.1", features = ["v4"] }
24 21
25weechat = { git = "https://github.com/poljar/rust-weechat", features = ["async-executor"] } 22[dependencies.weechat]
26weechat-sys = { git = "https://github.com/poljar/rust-weechat" } 23git = "https://github.com/poljar/rust-weechat"
24features = ["async-executor", "config-macro"]
27 25
28[dependencies.matrix-sdk] 26[dependencies.matrix-sdk]
29git = "https://github.com/matrix-org/matrix-rust-sdk/" 27git = "https://github.com/matrix-org/matrix-rust-sdk/"
src/commands.rs +2 −2
@@ -270,7 +270,7 @@ impl Commands {
270 weechat: &Weechat, 270 weechat: &Weechat,
271 servers: &Servers, 271 servers: &Servers,
272 config: &ConfigHandle, 272 config: &ConfigHandle,
273 ) -> Commands { 273 ) -> Result<Commands, ()> {
274 let matrix_settings = CommandSettings::new("matrix") 274 let matrix_settings = CommandSettings::new("matrix")
275 .description("Matrix chat protocol command.") 275 .description("Matrix chat protocol command.")
276 .add_argument("server add <server-name> <hostname>[:<port>]") 276 .add_argument("server add <server-name> <hostname>[:<port>]")
@@ -301,6 +301,6 @@ Use /matrix [command] help to find out more.\n",
301 }, 301 },
302 ); 302 );
303 303
304 Commands { _matrix: matrix } 304 Ok(Commands { _matrix: matrix? })
305 } 305 }
306} 306}
src/config.rs +1 −1
@@ -17,7 +17,7 @@
17 17
18use crate::{MatrixServer, Servers}; 18use crate::{MatrixServer, Servers};
19use weechat::config::{Conf, OptionChanged, SectionReadCallback}; 19use weechat::config::{Conf, OptionChanged, SectionReadCallback};
20use weechat::Weechat; 20use weechat::{Weechat, config};
21 21
22use std::cell::{Ref, RefCell, RefMut}; 22use std::cell::{Ref, RefCell, RefMut};
23use std::rc::Rc; 23use 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 @@
1#![feature(async_closure)] 1#![feature(async_closure)]
2 2
3#[macro_use]
4mod config_macros;
5
6mod commands; 3mod commands;
7mod config; 4mod config;
8mod debug; 5mod debug;
@@ -143,7 +140,7 @@ impl WeechatPlugin for Matrix {
143 fn init(weechat: &Weechat, _args: ArgsWeechat) -> Result<Self, ()> { 140 fn init(weechat: &Weechat, _args: ArgsWeechat) -> Result<Self, ()> {
144 let servers = Servers::new(); 141 let servers = Servers::new();
145 let config = ConfigHandle::new(&servers); 142 let config = ConfigHandle::new(&servers);
146 let commands = Commands::hook_all(weechat, &servers, &config); 143 let commands = Commands::hook_all(weechat, &servers, &config)?;
147 144
148 // TODO move the bar creation into a separate file. 145 // TODO move the bar creation into a separate file.
149 let status_bar = 146 let status_bar =