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
use crate::{
config::{
config_options::{ConfigOptions, FromPtrs, HidenConfigOptionT},
BaseConfigOption, ConfigSection,
},
Weechat,
};
use std::{borrow::Cow, ffi::CStr, marker::PhantomData};
use weechat_sys::{t_config_option, t_weechat_plugin};
type StringCheckCb = Option<Box<dyn FnMut(&Weechat, &StringOption, Cow<str>) -> bool>>;
/// Settings for a new string option.
#[derive(Default)]
pub struct StringOptionSettings {
pub(crate) name: String,
pub(crate) description: String,
pub(crate) default_value: String,
pub(crate) change_cb: Option<Box<dyn FnMut(&Weechat, &StringOption)>>,
pub(crate) check_cb: StringCheckCb,
}
impl StringOptionSettings {
/// Create new settings that can be used to create a new string option.
///
/// # Arguments
///
/// * `name` - The name of the new option.
pub fn new<N: Into<String>>(name: N) -> Self {
StringOptionSettings {
name: name.into(),
..Default::default()
}
}
/// Set the description of the option.
///
/// # Arguments
///
/// * `description` - The description of the new option.
pub fn description<D: Into<String>>(mut self, descritpion: D) -> Self {
self.description = descritpion.into();
self
}
/// Set the default value of the option.
///
/// This is the value the option will have if it isn't set by the user. If
/// the option is reset, the option will take this value.
///
/// # Arguments
///
/// * `value` - The value that should act as the default value.
pub fn default_value<V: Into<String>>(mut self, value: V) -> Self {
self.default_value = value.into();
self
}
/// Set the callback that will run when the value of the option changes.
///
/// # Arguments
///
/// * `callback` - The callback that will be run.
///
/// # Examples
/// ```
/// use weechat::Weechat;
/// use weechat::config::StringOptionSettings;
///
/// let settings = StringOptionSettings::new("address")
/// .set_change_callback(|weechat, option| {
/// Weechat::print("Option changed");
/// });
/// ```
pub fn set_change_callback(
mut self,
callback: impl FnMut(&Weechat, &StringOption) + 'static,
) -> Self {
self.change_cb = Some(Box::new(callback));
self
}
/// Set a callback to check the validity of the string option.
///
/// # Arguments
///
/// * `callback` - The callback that will be run.
///
/// # Examples
/// ```
/// use weechat::config::StringOptionSettings;
///
/// let settings = StringOptionSettings::new("address")
/// .set_check_callback(|weechat, option, value| {
/// value.starts_with("http")
/// });
/// ```
pub fn set_check_callback(
mut self,
callback: impl FnMut(&Weechat, &StringOption, Cow<str>) -> bool + 'static,
) -> Self {
self.check_cb = Some(Box::new(callback));
self
}
}
/// A config option with a string value.
pub struct StringOption<'a> {
pub(crate) ptr: *mut t_config_option,
pub(crate) weechat_ptr: *mut t_weechat_plugin,
pub(crate) _phantom: PhantomData<&'a ConfigSection>,
}
impl<'a> StringOption<'a> {
/// Get the value of the option.
pub fn value(&self) -> Cow<str> {
let weechat = self.get_weechat();
let config_string = weechat.get().config_string.unwrap();
unsafe {
let string = config_string(self.get_ptr());
CStr::from_ptr(string).to_string_lossy()
}
}
}
impl<'a> FromPtrs for StringOption<'a> {
fn from_ptrs(option_ptr: *mut t_config_option, weechat_ptr: *mut t_weechat_plugin) -> Self {
StringOption {
ptr: option_ptr,
weechat_ptr,
_phantom: PhantomData,
}
}
}
impl<'a> HidenConfigOptionT for StringOption<'a> {
fn get_ptr(&self) -> *mut t_config_option {
self.ptr
}
fn get_weechat(&self) -> Weechat {
Weechat::from_ptr(self.weechat_ptr)
}
}
impl<'a> BaseConfigOption for StringOption<'a> {}
impl<'a> ConfigOptions for StringOption<'_> {}