mirror of
https://github.com/szymon-jozef/libnotify-rs.git
synced 2026-10-27 19:20:43 +01:00
feat: add libnotify error
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -247,7 +247,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libnotify-rs"
|
name = "libnotify-rs"
|
||||||
version = "0.2.0"
|
version = "0.2.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bindgen",
|
"bindgen",
|
||||||
"glib",
|
"glib",
|
||||||
|
|||||||
37
src/api/errors.rs
Normal file
37
src/api/errors.rs
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
/// Enum mapping every possible error that can happen in this lib, with a nice enum.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum LibnotifyError {
|
||||||
|
/// [libnotify init](https://gnome.pages.gitlab.gnome.org/libnotify/func.init.html) failed
|
||||||
|
InitFail,
|
||||||
|
/// `&str` contains a null terminator. Read more at [std documentation](https://doc.rust-lang.org/std/ffi/struct.NulError.html)
|
||||||
|
NulError(std::ffi::NulError),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for LibnotifyError {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
Self::InitFail => {
|
||||||
|
write!(f, "Libnotify failed to init")
|
||||||
|
}
|
||||||
|
|
||||||
|
Self::NulError(_) => {
|
||||||
|
write!(f, "Argument contains a null terminator")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for LibnotifyError {
|
||||||
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
||||||
|
match self {
|
||||||
|
Self::InitFail => None,
|
||||||
|
Self::NulError(e) => Some(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<std::ffi::NulError> for LibnotifyError {
|
||||||
|
fn from(value: std::ffi::NulError) -> Self {
|
||||||
|
Self::NulError(value)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use crate::api::errors::LibnotifyError;
|
||||||
|
|
||||||
use super::utils::c_str_to_rs_str_and_free;
|
use super::utils::c_str_to_rs_str_and_free;
|
||||||
|
|
||||||
use super::libnotify::*;
|
use super::libnotify::*;
|
||||||
@@ -10,7 +12,7 @@ use super::libnotify::*;
|
|||||||
/// Initialized libnotify. This must be called before any other functions.
|
/// Initialized libnotify. This must be called before any other functions.
|
||||||
///
|
///
|
||||||
/// Starting from 0.8, if the provided app_name is NULL, libnotify will try to figure it out from the running application. Before it was not allowed, and was causing libnotify not to be initialized.
|
/// Starting from 0.8, if the provided app_name is NULL, libnotify will try to figure it out from the running application. Before it was not allowed, and was causing libnotify not to be initialized.
|
||||||
pub fn init<'a, T>(app_name: T) -> Result<(), Box<dyn std::error::Error>>
|
pub fn init<'a, T>(app_name: T) -> Result<(), LibnotifyError>
|
||||||
where
|
where
|
||||||
T: Into<Option<&'a str>>,
|
T: Into<Option<&'a str>>,
|
||||||
{
|
{
|
||||||
@@ -24,7 +26,7 @@ where
|
|||||||
)
|
)
|
||||||
} == 0
|
} == 0
|
||||||
{
|
{
|
||||||
return Err("Libnotify init failed".into());
|
return Err(LibnotifyError::InitFail);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -161,7 +163,7 @@ pub fn get_server_info() -> Option<ServerInfo> {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/// Sets the application name
|
/// Sets the application name
|
||||||
pub fn set_app_name(app_name: &str) -> Result<(), std::ffi::NulError> {
|
pub fn set_app_name(app_name: &str) -> Result<(), LibnotifyError> {
|
||||||
let app_name_c = std::ffi::CString::new(app_name)?;
|
let app_name_c = std::ffi::CString::new(app_name)?;
|
||||||
unsafe { notify_set_app_name(app_name_c.as_ptr()) };
|
unsafe { notify_set_app_name(app_name_c.as_ptr()) };
|
||||||
|
|
||||||
@@ -172,7 +174,7 @@ pub fn set_app_name(app_name: &str) -> Result<(), std::ffi::NulError> {
|
|||||||
/// Available since: 0.8.4
|
/// Available since: 0.8.4
|
||||||
///
|
///
|
||||||
/// app_icon is icon name or path to an image
|
/// app_icon is icon name or path to an image
|
||||||
pub fn set_app_icon(app_icon: &str) -> Result<(), std::ffi::NulError> {
|
pub fn set_app_icon(app_icon: &str) -> Result<(), LibnotifyError> {
|
||||||
let app_icon_c = std::ffi::CString::new(app_icon)?;
|
let app_icon_c = std::ffi::CString::new(app_icon)?;
|
||||||
unsafe { notify_set_app_icon(app_icon_c.as_ptr()) };
|
unsafe { notify_set_app_icon(app_icon_c.as_ptr()) };
|
||||||
|
|
||||||
@@ -195,11 +197,10 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_bad_init() {
|
fn test_bad_init() {
|
||||||
assert!(
|
assert!(matches!(
|
||||||
init("Mor\0ius")
|
init("Mor\0ius").unwrap_err(),
|
||||||
.unwrap_err()
|
LibnotifyError::NulError(_)
|
||||||
.is::<std::ffi::c_str::NulError>()
|
));
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
/// Custom error types
|
||||||
|
pub mod errors;
|
||||||
/// Same as [libnotify global functions](https://gnome.pages.gitlab.gnome.org/libnotify/index.html#functions)
|
/// Same as [libnotify global functions](https://gnome.pages.gitlab.gnome.org/libnotify/index.html#functions)
|
||||||
pub mod functions;
|
pub mod functions;
|
||||||
mod libnotify;
|
mod libnotify;
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
use crate::api::functions::{init, is_initted, set_app_icon, set_app_name, uninit};
|
use crate::api::{
|
||||||
|
errors::LibnotifyError,
|
||||||
|
functions::{init, is_initted, set_app_icon, set_app_name, uninit},
|
||||||
|
};
|
||||||
|
|
||||||
/// Notification context. Handles libnotify initializing and uninitializing.
|
/// Notification context. Handles libnotify initializing and uninitializing.
|
||||||
pub struct LibnotifyContext {}
|
pub struct LibnotifyContext {}
|
||||||
@@ -25,13 +28,13 @@ impl LibnotifyContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Sets global app name
|
/// Sets global app name
|
||||||
pub fn set_new_name(&self, app_name: &str) -> Result<(), std::ffi::NulError> {
|
pub fn set_new_name(&self, app_name: &str) -> Result<(), LibnotifyError> {
|
||||||
set_app_name(app_name)?;
|
set_app_name(app_name)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets global app icon
|
/// Sets global app icon
|
||||||
pub fn set_app_icon(&self, app_icon: &str) -> Result<(), std::ffi::NulError> {
|
pub fn set_app_icon(&self, app_icon: &str) -> Result<(), LibnotifyError> {
|
||||||
set_app_icon(app_icon)?;
|
set_app_icon(app_icon)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user