weechat 0.4.0

Weechat API bindings for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
use libc::{c_char, c_int};
use std::{
    cell::{Ref, RefCell, RefMut},
    collections::HashMap,
    ffi::CStr,
    ops::{Deref, DerefMut},
    os::raw::c_void,
    ptr,
    rc::Weak,
};

use std::marker::PhantomData;
use weechat_sys::{t_config_file, t_config_option, t_config_section, t_weechat_plugin};

use crate::{
    config::{
        config_options::{CheckCB, OptionPointers, OptionType},
        BaseConfigOption, BooleanOption, BooleanOptionSettings, ColorOption, ColorOptionSettings,
        Conf, Config, ConfigOptions, IntegerOption, IntegerOptionSettings, OptionChanged,
        StringOption, StringOptionSettings,
    },
    LossyCString, Weechat,
};

#[derive(Default)]
struct OptionDescription<'a> {
    pub name: &'a str,
    pub option_type: OptionType,
    pub description: &'a str,
    pub string_values: &'a str,
    pub min: i32,
    pub max: i32,
    pub default_value: &'a str,
    pub value: &'a str,
    pub null_allowed: bool,
}

#[allow(missing_docs)]
pub enum ConfigOption<'a> {
    Boolean(BooleanOption<'a>),
    Integer(IntegerOption<'a>),
    String(StringOption<'a>),
    Color(ColorOption<'a>),
}

impl<'a> ConfigOption<'a> {
    fn as_base_config_option(&self) -> &(dyn BaseConfigOption + 'a) {
        match self {
            ConfigOption::Color(ref o) => o,
            ConfigOption::Boolean(ref o) => o,
            ConfigOption::Integer(ref o) => o,
            ConfigOption::String(ref o) => o,
        }
    }
}

impl<'a> Deref for ConfigOption<'a> {
    type Target = dyn BaseConfigOption + 'a;
    fn deref(&self) -> &Self::Target {
        self.as_base_config_option()
    }
}

impl<'a> AsRef<dyn BaseConfigOption + 'a> for dyn BaseConfigOption + 'a {
    fn as_ref(&self) -> &(dyn BaseConfigOption + 'a) {
        self
    }
}

impl<'a> AsRef<dyn BaseConfigOption + 'a> for BooleanOption<'a> {
    fn as_ref(&self) -> &(dyn BaseConfigOption + 'a) {
        self
    }
}

impl<'a> AsRef<dyn BaseConfigOption + 'a> for ColorOption<'a> {
    fn as_ref(&self) -> &(dyn BaseConfigOption + 'a) {
        self
    }
}

impl<'a> AsRef<dyn BaseConfigOption + 'a> for IntegerOption<'a> {
    fn as_ref(&self) -> &(dyn BaseConfigOption + 'a) {
        self
    }
}

impl<'a> AsRef<dyn BaseConfigOption + 'a> for StringOption<'a> {
    fn as_ref(&self) -> &(dyn BaseConfigOption + 'a) {
        self
    }
}

impl<'a> AsRef<dyn BaseConfigOption + 'a> for ConfigOption<'a> {
    fn as_ref(&self) -> &(dyn BaseConfigOption + 'a) {
        self.as_base_config_option()
    }
}

#[derive(Debug)]
pub(crate) enum ConfigOptionPointers {
    Boolean(*const c_void),
    Integer(*const c_void),
    String(*const c_void),
    Color(*const c_void),
}

/// A mutable handle to a Weechat config section.
pub struct SectionHandleMut<'a> {
    pub(crate) inner: RefMut<'a, ConfigSection>,
}

/// A handle to a Weechat config section.
pub struct SectionHandle<'a> {
    pub(crate) inner: Ref<'a, ConfigSection>,
}

impl<'a> Deref for SectionHandle<'a> {
    type Target = ConfigSection;

    fn deref(&self) -> &Self::Target {
        &*self.inner
    }
}

impl<'a> Deref for SectionHandleMut<'a> {
    type Target = ConfigSection;

    fn deref(&self) -> &Self::Target {
        &*self.inner
    }
}

impl<'a> DerefMut for SectionHandleMut<'a> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut *self.inner
    }
}

/// Weechat Configuration section
#[derive(Debug)]
pub struct ConfigSection {
    pub(crate) ptr: *mut t_config_section,
    pub(crate) config_ptr: *mut t_config_file,
    pub(crate) weechat_ptr: *mut t_weechat_plugin,
    pub(crate) name: String,
    pub(crate) section_data: *const c_void,
    pub(crate) option_pointers: HashMap<String, ConfigOptionPointers>,
}

/// Trait for the section write callback.
///
/// A blanket implementation for pure `FnMut` functions exists, if data needs to
/// be passed to the callback implement this over your struct.
pub trait SectionWriteCallback: 'static {
    /// Callback that will be called when the section needs to be written out.
    ///
    /// # Arguments
    ///
    /// * `weechat` - A Weechat context.
    ///
    /// * `config` - A borrowed version of the Weechat configuration object.
    ///
    /// * `section` - The section that is being written, if the Config struct is
    /// contained inside of `self` make sure not to borrow the same section
    /// again.
    fn callback(&mut self, weechat: &Weechat, config: &Conf, section: &mut ConfigSection);
}

impl<T: FnMut(&Weechat, &Conf, &mut ConfigSection) + 'static> SectionWriteCallback for T {
    fn callback(&mut self, weechat: &Weechat, config: &Conf, section: &mut ConfigSection) {
        self(weechat, config, section)
    }
}

/// Trait for the section write-default callback.
///
/// A blanket implementation for pure `FnMut` functions exists, if data needs to
/// be passed to the callback implement this over your struct.
pub trait SectionWriteDefaultCallback: 'static {
    /// Callback that will be called when the section needs to be populated with
    /// default values.
    ///
    /// # Arguments
    ///
    /// * `weechat` - A Weechat context.
    ///
    /// * `config` - A borrowed version of the Weechat configuration object.
    ///
    /// * `section` - The section that is being populated with default values,
    /// if the Config struct is contained inside of `self` make sure not to
    /// borrow the same section again.
    fn callback(&mut self, weechat: &Weechat, config: &Conf, section: &mut ConfigSection);
}

impl<T: FnMut(&Weechat, &Conf, &mut ConfigSection) + 'static> SectionWriteDefaultCallback for T {
    fn callback(&mut self, weechat: &Weechat, config: &Conf, section: &mut ConfigSection) {
        self(weechat, config, section)
    }
}

/// Trait for the section read callback.
///
/// A blanket implementation for pure `FnMut` functions exists, if data needs to
/// be passed to the callback implement this over your struct.
pub trait SectionReadCallback: 'static {
    /// Callback that will be called when the section is read.
    ///
    /// Should return if the option was successfully recognized and changed.
    ///
    /// # Arguments
    ///
    /// * `weechat` - A Weechat context.
    ///
    /// * `config` - A borrowed version of the Weechat configuration object.
    ///
    /// * `section` - The section that is being populated with default values,
    /// if the Config struct is contained inside of `self` make sure not to
    /// borrow the same section again.
    ///
    /// * `option_name` - The name of the option that is currently being read.
    ///
    /// * `option_value` - The value of the option that is being read.
    fn callback(
        &mut self,
        weechat: &Weechat,
        config: &Conf,
        section: &mut ConfigSection,
        option_name: &str,
        option_value: &str,
    ) -> OptionChanged;
}

impl<T: FnMut(&Weechat, &Conf, &mut ConfigSection, &str, &str) -> OptionChanged + 'static>
    SectionReadCallback for T
{
    fn callback(
        &mut self,
        weechat: &Weechat,
        config: &Conf,
        section: &mut ConfigSection,
        option_name: &str,
        option_value: &str,
    ) -> OptionChanged {
        self(weechat, config, section, option_name, option_value)
    }
}

pub(crate) struct ConfigSectionPointers {
    pub(crate) read_cb: Option<Box<dyn SectionReadCallback>>,
    pub(crate) write_cb: Option<Box<dyn SectionWriteCallback>>,
    pub(crate) write_default_cb: Option<Box<dyn SectionWriteDefaultCallback>>,
    pub(crate) section: Option<Weak<RefCell<ConfigSection>>>,
    pub(crate) weechat_ptr: *mut t_weechat_plugin,
}

impl std::fmt::Debug for ConfigSectionPointers {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "ConfigSectionPointers {{ section_ptr: {:?} weechat_ptr: {:?}}}",
            self.section, self.weechat_ptr
        )
    }
}

/// Represents the options when creating a new config section.
#[derive(Default)]
pub struct ConfigSectionSettings {
    pub(crate) name: String,

    pub(crate) read_callback: Option<Box<dyn SectionReadCallback>>,

    /// A function called when the section is written to the disk
    pub(crate) write_callback: Option<Box<dyn SectionWriteCallback>>,

    /// A function called when default values for the section must be written to the disk
    pub(crate) write_default_callback: Option<Box<dyn SectionWriteDefaultCallback>>,
}

impl ConfigSectionSettings {
    /// Create a new config section info.
    /// This can be passed to a config which will create a new ConfigSection.
    ///
    /// #Arguments
    ///
    /// * `name` - The name that the section should get.
    pub fn new<P: Into<String>>(name: P) -> Self {
        ConfigSectionSettings {
            name: name.into(),
            ..Default::default()
        }
    }

    /// Set the function that will be called when an option from the section is
    /// read from the disk.
    ///
    /// #Arguments
    ///
    /// * `callback` - The callback for a section read operation.
    ///
    /// # Examples
    /// ```
    /// use weechat::Weechat;
    /// use weechat::config::{Conf, ConfigSection, ConfigSectionSettings, OptionChanged};
    ///
    /// let server_section_options = ConfigSectionSettings::new("server")
    ///     .set_read_callback(|_: &Weechat, config: &Conf, section: &mut ConfigSection,
    ///                         option_name: &str, option_value: &str| {
    ///         Weechat::print("Writing section");
    ///         OptionChanged::Changed
    /// });
    /// ```
    pub fn set_read_callback(mut self, callback: impl SectionReadCallback) -> Self {
        self.read_callback = Some(Box::new(callback));
        self
    }

    /// Set the function that will be called when the section is being written
    /// to the file.
    ///
    /// #Arguments
    ///
    /// * `callback` - The callback for the section write operation.
    ///
    /// # Examples
    /// ```
    /// use weechat::Weechat;
    /// use weechat::config::ConfigSectionSettings;
    ///
    /// let server_section_options = ConfigSectionSettings::new("server")
    ///     .set_write_callback(|weechat, config, section| {
    ///         Weechat::print("Writing section");
    /// });
    /// ```
    pub fn set_write_callback(
        mut self,
        callback: impl FnMut(&Weechat, &Conf, &mut ConfigSection) + 'static,
    ) -> Self {
        self.write_callback = Some(Box::new(callback));
        self
    }

    /// Set the function that will be called when default values will need to
    /// be written to to the file.
    ///
    /// #Arguments
    ///
    /// * `callback` - The callback for the section write default operation.
    pub fn set_write_default_callback(
        mut self,
        callback: impl FnMut(&Weechat, &Conf, &mut ConfigSection) + 'static,
    ) -> Self {
        self.write_default_callback = Some(Box::new(callback));
        self
    }
}

impl Drop for ConfigSection {
    fn drop(&mut self) {
        let weechat = Weechat::from_ptr(self.weechat_ptr);

        let options_free = weechat.get().config_section_free_options.unwrap();
        let section_free = weechat.get().config_section_free.unwrap();

        for (_, option_ptrs) in self.option_pointers.drain() {
            unsafe {
                match option_ptrs {
                    ConfigOptionPointers::Integer(p) => {
                        Box::from_raw(p as *mut OptionPointers<IntegerOption>);
                    }
                    ConfigOptionPointers::Boolean(p) => {
                        Box::from_raw(p as *mut OptionPointers<BooleanOption>);
                    }
                    ConfigOptionPointers::String(p) => {
                        Box::from_raw(p as *mut OptionPointers<StringOption>);
                    }
                    ConfigOptionPointers::Color(p) => {
                        Box::from_raw(p as *mut OptionPointers<ColorOption>);
                    }
                }
            }
        }

        unsafe {
            Box::from_raw(self.section_data as *mut ConfigSectionPointers);
            options_free(self.ptr);
            section_free(self.ptr);
        };
    }
}

pub(crate) type SectionReadCbT = unsafe extern "C" fn(
    pointer: *const c_void,
    _data: *mut c_void,
    _config: *mut t_config_file,
    _section: *mut t_config_section,
    option_name: *const i8,
    value: *const i8,
) -> c_int;

pub(crate) type SectionWriteCbT = unsafe extern "C" fn(
    pointer: *const c_void,
    _data: *mut c_void,
    _config: *mut t_config_file,
    section_name: *const c_char,
) -> c_int;

type WeechatOptChangeCbT = unsafe extern "C" fn(
    pointer: *const c_void,
    _data: *mut c_void,
    option_pointer: *mut t_config_option,
);

type WeechatOptCheckCbT = unsafe extern "C" fn(
    pointer: *const c_void,
    _data: *mut c_void,
    option_pointer: *mut t_config_option,
    value: *const c_char,
) -> c_int;

impl ConfigSection {
    /// Get the name of the section.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Get the config options of this section.
    pub fn options(&self) -> Vec<ConfigOption> {
        self.option_pointers
            .keys()
            .map(|option_name| self.search_option(option_name).unwrap())
            .collect()
    }

    /// Free a config option that belongs to this section.
    ///
    /// Returns an Err if the option can't be found in this section.
    ///
    /// # Arguments
    ///
    /// * `option_name` - The name of the option that should be freed.
    pub fn free_option(&mut self, option_name: &str) -> Result<(), ()> {
        let weechat = Weechat::from_ptr(self.weechat_ptr);

        let option_pointers = self.option_pointers.remove(option_name);
        if option_pointers.is_none() {
            // TODO Return a better error value here.
            return Err(());
        }

        let option = self
            .search_option(option_name)
            .expect("No option found even though option pointers are there");

        let config_option_free = weechat.get().config_option_free.unwrap();

        unsafe { config_option_free(option.get_ptr()) }

        Ok(())
    }

    /// Search for an option in this section.
    /// # Arguments
    ///
    /// * `option_name` - The name of the option to search for.
    pub fn search_option(&self, option_name: &str) -> Option<ConfigOption> {
        let weechat = Weechat::from_ptr(self.weechat_ptr);
        let config_search_option = weechat.get().config_search_option.unwrap();
        let name = LossyCString::new(option_name);

        let ptr = unsafe { config_search_option(self.config_ptr, self.ptr, name.as_ptr()) };

        if ptr.is_null() {
            return None;
        }

        let option_type = weechat.config_option_get_string(ptr, "type").unwrap();

        Some(Config::option_from_type_and_ptr(
            self.weechat_ptr,
            ptr,
            option_type.as_ref(),
        ))
    }

    /// Create a new string Weechat configuration option.
    ///
    /// Returns None if the option couldn't be created, e.g. if a option with
    /// the same name already exists.
    ///
    /// # Arguments
    ///
    /// * `settings` - Settings that decide how the option should be created.
    pub fn new_string_option(
        &mut self,
        settings: StringOptionSettings,
    ) -> Result<StringOption, ()> {
        let ret = self.new_option(
            OptionDescription {
                name: &settings.name,
                description: &settings.description,
                option_type: OptionType::String,
                default_value: &settings.default_value,
                value: &settings.default_value,
                ..Default::default()
            },
            settings.check_cb,
            settings.change_cb,
            None,
        );

        let (ptr, option_pointers) = if let Some((ptr, ptrs)) = ret {
            (ptr, ptrs)
        } else {
            return Err(());
        };

        let option_ptrs = ConfigOptionPointers::String(option_pointers);
        self.option_pointers.insert(settings.name, option_ptrs);

        let option = StringOption {
            ptr,
            weechat_ptr: self.weechat_ptr,
            _phantom: PhantomData,
        };
        Ok(option)
    }

    /// Create a new boolean Weechat configuration option.
    ///
    /// Returns None if the option couldn't be created, e.g. if a option with
    /// the same name already exists.
    ///
    /// # Arguments
    /// * `settings` - Settings that decide how the option should be created.
    pub fn new_boolean_option(
        &mut self,
        settings: BooleanOptionSettings,
    ) -> Result<BooleanOption, ()> {
        let value = if settings.default_value { "on" } else { "off" };
        let default_value = if settings.default_value { "on" } else { "off" };
        let ret = self.new_option(
            OptionDescription {
                name: &settings.name,
                description: &settings.description,
                option_type: OptionType::Boolean,
                default_value,
                value,
                ..Default::default()
            },
            None,
            settings.change_cb,
            None,
        );

        let (ptr, option_pointers) = if let Some((ptr, ptrs)) = ret {
            (ptr, ptrs)
        } else {
            return Err(());
        };

        let option_ptrs = ConfigOptionPointers::Boolean(option_pointers);
        self.option_pointers.insert(settings.name, option_ptrs);

        let option = BooleanOption {
            ptr,
            weechat_ptr: self.weechat_ptr,
            _phantom: PhantomData,
        };

        Ok(option)
    }

    /// Create a new integer Weechat configuration option.
    ///
    /// Returns None if the option couldn't be created, e.g. if a option with
    /// the same name already exists.
    ///
    /// # Arguments
    /// * `settings` - Settings that decide how the option should be created.
    pub fn new_integer_option(
        &mut self,
        settings: IntegerOptionSettings,
    ) -> Result<IntegerOption, ()> {
        let ret = self.new_option(
            OptionDescription {
                name: &settings.name,
                option_type: OptionType::Integer,
                description: &settings.description,
                string_values: &settings.string_values,
                min: settings.min,
                max: settings.max,
                default_value: &settings.default_value.to_string(),
                value: &settings.default_value.to_string(),
                ..Default::default()
            },
            None,
            settings.change_cb,
            None,
        );

        let (ptr, option_pointers) = if let Some((ptr, ptrs)) = ret {
            (ptr, ptrs)
        } else {
            return Err(());
        };

        let option_ptrs = ConfigOptionPointers::Integer(option_pointers);
        self.option_pointers.insert(settings.name, option_ptrs);

        let option = IntegerOption {
            ptr,
            weechat_ptr: self.weechat_ptr,
            _phantom: PhantomData,
        };
        Ok(option)
    }

    /// Create a new color Weechat configuration option.
    ///
    /// Returns None if the option couldn't be created, e.g. if a option with
    /// the same name already exists.
    ///
    /// # Arguments
    /// * `settings` - Settings that decide how the option should be created.
    pub fn new_color_option(&mut self, settings: ColorOptionSettings) -> Result<ColorOption, ()> {
        let ret = self.new_option(
            OptionDescription {
                name: &settings.name,
                description: &settings.description,
                option_type: OptionType::Color,
                default_value: &settings.default_value,
                value: &settings.default_value,
                ..Default::default()
            },
            None,
            settings.change_cb,
            None,
        );

        let (ptr, option_pointers) = if let Some((ptr, ptrs)) = ret {
            (ptr, ptrs)
        } else {
            return Err(());
        };

        let option_ptrs = ConfigOptionPointers::Color(option_pointers);
        self.option_pointers.insert(settings.name, option_ptrs);

        let option = ColorOption {
            ptr,
            weechat_ptr: self.weechat_ptr,
            _phantom: PhantomData,
        };
        Ok(option)
    }

    fn new_option<T>(
        &self,
        option_description: OptionDescription,
        check_cb: Option<Box<CheckCB<T>>>,
        change_cb: Option<Box<dyn FnMut(&Weechat, &T)>>,
        delete_cb: Option<Box<dyn FnMut(&Weechat, &T)>>,
    ) -> Option<(*mut t_config_option, *const c_void)>
    where
        T: ConfigOptions,
    {
        unsafe extern "C" fn c_check_cb<T>(
            pointer: *const c_void,
            _data: *mut c_void,
            option_pointer: *mut t_config_option,
            value: *const c_char,
        ) -> c_int
        where
            T: ConfigOptions,
        {
            let value = CStr::from_ptr(value).to_string_lossy();
            let pointers: &mut OptionPointers<T> = { &mut *(pointer as *mut OptionPointers<T>) };

            let weechat = Weechat::from_ptr(pointers.weechat_ptr);
            let option = T::from_ptrs(option_pointer, pointers.weechat_ptr);

            let ret = if let Some(callback) = &mut pointers.check_cb {
                callback(&weechat, &option, value)
            } else {
                true
            };

            ret as i32
        }

        unsafe extern "C" fn c_change_cb<T>(
            pointer: *const c_void,
            _data: *mut c_void,
            option_pointer: *mut t_config_option,
        ) where
            T: ConfigOptions,
        {
            let pointers: &mut OptionPointers<T> = { &mut *(pointer as *mut OptionPointers<T>) };

            let weechat = Weechat::from_ptr(pointers.weechat_ptr);
            let option = T::from_ptrs(option_pointer, pointers.weechat_ptr);

            if let Some(callback) = &mut pointers.change_cb {
                callback(&weechat, &option)
            };
        }

        unsafe extern "C" fn c_delete_cb<T>(
            pointer: *const c_void,
            _data: *mut c_void,
            option_pointer: *mut t_config_option,
        ) where
            T: ConfigOptions,
        {
            let pointers: &mut OptionPointers<T> = { &mut *(pointer as *mut OptionPointers<T>) };

            let weechat = Weechat::from_ptr(pointers.weechat_ptr);
            let option = T::from_ptrs(option_pointer, pointers.weechat_ptr);

            if let Some(callback) = &mut pointers.delete_cb {
                callback(&weechat, &option)
            };
        }

        let weechat = Weechat::from_ptr(self.weechat_ptr);

        let name = LossyCString::new(option_description.name);
        let description = LossyCString::new(option_description.description);
        let option_type = LossyCString::new(option_description.option_type.as_str());
        let string_values = LossyCString::new(option_description.string_values);
        let default_value = LossyCString::new(option_description.default_value);
        let value = LossyCString::new(option_description.value);

        let c_check_cb = match check_cb {
            Some(_) => Some(c_check_cb::<T> as WeechatOptCheckCbT),
            None => None,
        };

        let c_change_cb: Option<WeechatOptChangeCbT> = match change_cb {
            Some(_) => Some(c_change_cb::<T>),
            None => None,
        };

        let c_delete_cb: Option<WeechatOptChangeCbT> = match delete_cb {
            Some(_) => Some(c_delete_cb::<T>),
            None => None,
        };

        let option_pointers = Box::new(OptionPointers {
            weechat_ptr: self.weechat_ptr,
            check_cb,
            change_cb,
            delete_cb,
        });

        let option_pointers_ref: &OptionPointers<T> = Box::leak(option_pointers);

        let config_new_option = weechat.get().config_new_option.unwrap();
        let ptr = unsafe {
            config_new_option(
                self.config_ptr,
                self.ptr,
                name.as_ptr(),
                option_type.as_ptr(),
                description.as_ptr(),
                string_values.as_ptr(),
                option_description.min,
                option_description.max,
                default_value.as_ptr(),
                value.as_ptr(),
                option_description.null_allowed as i32,
                c_check_cb,
                option_pointers_ref as *const _ as *const c_void,
                ptr::null_mut(),
                c_change_cb,
                option_pointers_ref as *const _ as *const c_void,
                ptr::null_mut(),
                c_delete_cb,
                option_pointers_ref as *const _ as *const c_void,
                ptr::null_mut(),
            )
        };

        if ptr.is_null() {
            None
        } else {
            Some((ptr, option_pointers_ref as *const _ as *const c_void))
        }
    }
}