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

glib: Allow variable expansion in format strings passed to bool_error & result_from_gboolean #1210

Merged
merged 2 commits into from
Oct 21, 2023
Merged
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
78 changes: 42 additions & 36 deletions glib/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,49 +152,55 @@ pub trait ErrorDomain: Copy {
/// Generic error used for functions that fail without any further information
#[macro_export]
macro_rules! bool_error(
// Plain strings
($msg:expr) => {{
$crate::BoolError::new(
$msg,
file!(),
$crate::function_name!(),
line!(),
)
}};

// Format strings
($($msg:tt)*) => {{
$crate::BoolError::new(
format!($($msg)*),
file!(),
$crate::function_name!(),
line!(),
)
match ::std::format_args!($($msg)*) {
sdroege marked this conversation as resolved.
Show resolved Hide resolved
formatted => {
if let Some(s) = formatted.as_str() {
$crate::BoolError::new(
s,
file!(),
$crate::function_name!(),
line!()
)
} else {
$crate::BoolError::new(
formatted.to_string(),
file!(),
$crate::function_name!(),
line!(),
)
}
}
}
}};
);

#[macro_export]
macro_rules! result_from_gboolean(
// Plain strings
($ffi_bool:expr, $msg:expr) => {{
$crate::BoolError::from_glib(
$ffi_bool,
$msg,
file!(),
$crate::function_name!(),
line!(),
)
}};

// Format strings
($ffi_bool:expr, $($msg:tt)*) => {{
$crate::BoolError::from_glib(
$ffi_bool,
format!($($msg)*),
file!(),
$crate::function_name!(),
line!(),
)
match ::std::format_args!($($msg)*) {
formatted => {
if let Some(s) = formatted.as_str() {
$crate::BoolError::from_glib(
$ffi_bool,
s,
file!(),
$crate::function_name!(),
line!(),
)
} else {
$crate::BoolError::from_glib(
$ffi_bool,
formatted.to_string(),
file!(),
$crate::function_name!(),
line!(),
)
}
}
}


}};
);

Expand Down