Skip to content
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

Merged
merged 5 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/weechat-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ readme = "README.md"
repository = "https://github.com/poljar/rust-weechat"

[build-dependencies]
bindgen = "0.60.1"
bindgen = "0.70.0"

[dependencies]
libc = "0.2.132"
2 changes: 1 addition & 1 deletion crates/weechat-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn build(file: &str) -> Result<Bindings, BindgenError> {
"WEECHAT_HOOK_SIGNAL_INT",
"WEECHAT_HOOK_SIGNAL_POINTER",
];
let mut builder = bindgen::Builder::default().rustfmt_bindings(true);
let mut builder = bindgen::Builder::default();
Copy link
Owner

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?

Copy link
Author

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 is Rustfmt anyway
https://docs.rs/bindgen/0.70.1/bindgen/struct.Builder.html#method.rustfmt_bindings

Copy link
Author

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)

Copy link
Owner

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.

Copy link
Author

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


builder = builder.header(file);

Expand Down
402 changes: 325 additions & 77 deletions crates/weechat-sys/src/weechat-plugin.h

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions crates/weechat/src/buffer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -759,13 +759,13 @@ impl Buffer<'_> {
/// Display a message on the buffer.
pub fn print(&self, message: &str) {
let weechat = self.weechat();
let printf_date_tags = weechat.get().printf_date_tags.unwrap();
let printf_date_tags = weechat.get().printf_datetime_tags.unwrap();

let fmt_str = LossyCString::new("%s");
let c_message = LossyCString::new(message);

unsafe {
printf_date_tags(self.ptr(), 0, ptr::null(), fmt_str.as_ptr(), c_message.as_ptr())
printf_date_tags(self.ptr(), 0, 0, ptr::null(), fmt_str.as_ptr(), c_message.as_ptr())
}
}

Expand All @@ -781,15 +781,15 @@ impl Buffer<'_> {
/// * `message` - The message that will be displayed.
pub fn print_date_tags(&self, date: i64, tags: &[&str], message: &str) {
let weechat = self.weechat();
let printf_date_tags = weechat.get().printf_date_tags.unwrap();
let printf_date_tags = weechat.get().printf_datetime_tags.unwrap();

let fmt_str = LossyCString::new("%s");
let tags = tags.join(",");
let tags = LossyCString::new(tags);
let message = LossyCString::new(message);

unsafe {
printf_date_tags(self.ptr(), date, tags.as_ptr(), fmt_str.as_ptr(), message.as_ptr())
printf_date_tags(self.ptr(), date, 0, tags.as_ptr(), fmt_str.as_ptr(), message.as_ptr())
}
}

Expand Down
10 changes: 8 additions & 2 deletions crates/weechat/src/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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"),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced unreachable!() macro with todo!() since the first one doesn't allow any message, but I'm thinking it would be better to avoid panicking, maybe returning Result<_> would be better, need to check signature usages across lib

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, a Result might be the best option here.

Copy link
Author

Choose a reason for hiding this comment

The 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 Result<T, ConfigError> and return them

}
}
fn return_value_to_error(return_value: c_int) -> std::io::Result<()> {
Expand Down
12 changes: 5 additions & 7 deletions crates/weechat/src/config/config_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ use weechat_sys::{t_config_option, t_weechat_plugin};

use crate::{config::OptionChanged, LossyCString, Weechat};

#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Clone, Default)]
#[allow(missing_docs)]
pub enum OptionType {
Boolean,
Integer,
#[default]
String,
Color,
Enum,
}

impl TryFrom<&str> for OptionType {
Expand All @@ -21,6 +23,7 @@ impl TryFrom<&str> for OptionType {
"integer" => OptionType::Integer,
"string" => OptionType::String,
"color" => OptionType::Color,
"enum" => OptionType::Enum,
_ => return Err("Invalid option type"),
};

Expand All @@ -35,16 +38,11 @@ impl OptionType {
OptionType::Integer => "integer",
OptionType::String => "string",
OptionType::Color => "color",
OptionType::Enum => "enum",
}
}
}

impl Default for OptionType {
fn default() -> Self {
OptionType::String
}
}

pub trait FromPtrs {
/// Returns the raw pointer to the config option.
fn from_ptrs(option_ptr: *mut t_config_option, weechat_ptr: *mut t_weechat_plugin) -> Self;
Expand Down
167 changes: 167 additions & 0 deletions crates/weechat/src/config/enum.rs
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<'_> {}
28 changes: 0 additions & 28 deletions crates/weechat/src/config/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ pub struct IntegerOptionSettings {

pub(crate) max: i32,

pub(crate) string_values: String,

pub(crate) change_cb: Option<Box<dyn FnMut(&Weechat, &IntegerOption)>>,
}

Expand Down Expand Up @@ -61,32 +59,6 @@ impl IntegerOptionSettings {
self
}

/// Set the string values of the option.
///
/// This setting decides if the integer option should act as an enum taking
/// symbolic values.
///
/// # Arguments
///
/// * `values` - The values that should act as the symbolic values.
///
/// # Examples
/// ```no_run
/// use weechat::config::IntegerOptionSettings;
///
/// let settings = IntegerOptionSettings::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 minimal value of the integer option.
///
/// # Arguments
Expand Down
2 changes: 2 additions & 0 deletions crates/weechat/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ mod color;
#[allow(clippy::module_inception)]
mod config;
mod config_options;
mod r#enum;
mod integer;
mod section;
mod string;
Expand All @@ -38,6 +39,7 @@ pub use crate::config::{
config::{Conf, Config, ConfigReloadCallback, OptionChanged},
config_options::{BaseConfigOption, ConfigOptions, OptionType},
integer::{IntegerOption, IntegerOptionSettings},
r#enum::{EnumOption, EnumOptionSettings},
section::{
ConfigOption, ConfigSection, ConfigSectionSettings, SectionHandle, SectionHandleMut,
SectionReadCallback, SectionWriteCallback, SectionWriteDefaultCallback,
Expand Down
Loading