-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
update header and bindings to weechat #36
Changes from all commits
b8608f4
a7476f4
e3d8899
aa85da4
fef7f57
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,8 @@ use crate::{ | |
ConfigSection, ConfigSectionPointers, ConfigSectionSettings, SectionHandle, | ||
SectionHandleMut, SectionReadCbT, SectionWriteCbT, | ||
}, | ||
BaseConfigOption, BooleanOption, ColorOption, ConfigOption, IntegerOption, StringOption, | ||
BaseConfigOption, BooleanOption, ColorOption, ConfigOption, EnumOption, IntegerOption, | ||
StringOption, | ||
}, | ||
LossyCString, Weechat, | ||
}; | ||
|
@@ -320,7 +321,12 @@ impl Config { | |
weechat_ptr, | ||
_phantom: PhantomData, | ||
}), | ||
_ => unreachable!(), | ||
"enum" => ConfigOption::Enum(EnumOption { | ||
ptr: option_ptr, | ||
weechat_ptr, | ||
_phantom: PhantomData, | ||
}), | ||
_ => todo!("Outdated option_from_type_and_ptr"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replaced There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I'll leave as it is for now, the behaviour will be same but now with a clear message. I need to learn more what's appropriate for Weechat plugin to do (e.g. if panicking is safe at all). looking through config I think I'll try to change all returns/unwraps/expects to |
||
} | ||
} | ||
fn return_value_to_error(return_value: c_int) -> std::io::Result<()> { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
use std::marker::PhantomData; | ||
|
||
use weechat_sys::{t_config_option, t_weechat_plugin}; | ||
|
||
use crate::{ | ||
config::{ | ||
config_options::{ConfigOptions, FromPtrs, HiddenConfigOptionT}, | ||
BaseConfigOption, ConfigSection, | ||
}, | ||
Weechat, | ||
}; | ||
|
||
/// Settings for a new enum option. | ||
#[derive(Default)] | ||
pub struct EnumOptionSettings { | ||
pub(crate) name: String, | ||
|
||
pub(crate) description: String, | ||
|
||
pub(crate) default_value: i32, | ||
|
||
pub(crate) min: i32, | ||
|
||
pub(crate) max: i32, | ||
|
||
pub(crate) string_values: String, | ||
|
||
pub(crate) change_cb: Option<Box<dyn FnMut(&Weechat, &EnumOption)>>, | ||
} | ||
|
||
impl EnumOptionSettings { | ||
/// Create new settings that can be used to create a new enum option. | ||
/// | ||
/// An enum option is represented as integer (index of enum value) internally | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `name` - The name of the new option. | ||
pub fn new<N: Into<String>>(name: N) -> Self { | ||
EnumOptionSettings { 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, description: D) -> Self { | ||
self.description = description.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<i32>>(mut self, value: V) -> Self { | ||
self.default_value = value.into(); | ||
self | ||
} | ||
|
||
/// Set minimal value of the enum option. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `value` - The values that should act as minimal valid value. | ||
pub fn min(mut self, value: i32) -> Self { | ||
self.min = value; | ||
self | ||
} | ||
|
||
/// Set maximum value of the enum option. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `value` - The values that should act as maximal valid value. | ||
pub fn max(mut self, value: i32) -> Self { | ||
self.max = value; | ||
self | ||
} | ||
|
||
/// Set the string values of the option. | ||
/// | ||
/// # Arguments | ||
/// | ||
/// * `values` - The values that should act as the symbolic values. | ||
/// | ||
/// # Examples | ||
/// ```no_run | ||
/// use weechat::config::EnumOptionSettings; | ||
/// | ||
/// let settings = EnumOptionSettings::new("server_buffer") | ||
/// .string_values(vec!["independent", "merged"]); | ||
/// ``` | ||
pub fn string_values<I, T>(mut self, values: I) -> Self | ||
where | ||
I: IntoIterator<Item = T>, | ||
T: Into<String>, | ||
{ | ||
let vec: Vec<String> = values.into_iter().map(Into::into).collect(); | ||
self.string_values = vec.join("|"); | ||
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::EnumOptionSettings; | ||
/// | ||
/// let settings = EnumOptionSettings::new("address") | ||
/// .set_change_callback(|weechat, option| { | ||
/// Weechat::print("Option changed"); | ||
/// }); | ||
/// ``` | ||
pub fn set_change_callback( | ||
mut self, | ||
callback: impl FnMut(&Weechat, &EnumOption) + 'static, | ||
) -> Self { | ||
self.change_cb = Some(Box::new(callback)); | ||
self | ||
} | ||
} | ||
|
||
/// A config option with a enum value. | ||
pub struct EnumOption<'a> { | ||
pub(crate) ptr: *mut t_config_option, | ||
pub(crate) weechat_ptr: *mut t_weechat_plugin, | ||
pub(crate) _phantom: PhantomData<&'a ConfigSection>, | ||
} | ||
|
||
impl<'a> EnumOption<'a> { | ||
/// Get the value of the option. | ||
pub fn value(&self) -> i32 { | ||
let weechat = self.get_weechat(); | ||
let config_enum = weechat.get().config_enum.unwrap(); | ||
unsafe { config_enum(self.get_ptr()) } | ||
} | ||
} | ||
|
||
impl<'a> FromPtrs for EnumOption<'a> { | ||
fn from_ptrs(option_ptr: *mut t_config_option, weechat_ptr: *mut t_weechat_plugin) -> Self { | ||
EnumOption { ptr: option_ptr, weechat_ptr, _phantom: PhantomData } | ||
} | ||
} | ||
|
||
impl<'a> HiddenConfigOptionT for EnumOption<'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 EnumOption<'a> {} | ||
impl<'a> ConfigOptions for EnumOption<'_> {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the rationale for removing the formatting?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This option is deprecated and
Builder::formatter
is preferred, but the default formatter isRustfmt
anywayhttps://docs.rs/bindgen/0.70.1/bindgen/struct.Builder.html#method.rustfmt_bindings
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(I should've noted that in commit message, my fault)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah indeed, thanks for letting me know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mentioned in commit now as well