h4llow3En / mac-notification-sys

✉️ A simple wrapper to deliver or schedule macOS Notifications in Rust
98 stars 28 forks source link

Returning `failure::error::Error`forces clients to add `failure` crate as a dependency #11

Closed lelandjansen closed 5 years ago

lelandjansen commented 5 years ago

The rust-lang-nursery/failure crate provides convenient error handling in Rust and was recently used by mac-notification-sys in v0.2. However, returning a failure::error::Error in public methods such as send_notification forces clients who wish to handle the error without calling unwrap() to use the failure crate and add it as a project dependency. This ticket is a recommendation/request to remove the failure dependency and revert to the error_chain handling in v0.1.* (or vanilla error handling).

One workaround would be to use map_err and pass the display message to our own type, but this is not optimal.

Thanks for the awesome crate :)

hoodie commented 5 years ago

I recently removed failure from one of my crates too, perhaps we ought to get rid of it here

hoodie commented 5 years ago

@lelandjansen could you give the branch feature/error-interface a try? if that's find we could call that 0.3.0

lelandjansen commented 5 years ago

@lelandjansen could you give the branch feature/error-interface a try?

@hoodie lgtm

Thank you for getting together this PR! I tested your change on the test program below as well as my own project.

# Cargo.toml
[package]
name = "notifications"
version = "0.1.0"
authors = ["Leland Jansen <hello@lelandjansen.com>"]
edition = "2018"

[dependencies]
mac-notification-sys = { git = "https://github.com/hoodie/mac-notification-sys", branch = "feature/error-interface" }
// src/lib.rs
use std::fmt;
use std::error;

#[derive(Debug)]
pub enum MyError {
    MacOs(mac_notification_sys::error::Error),
}

fn notify() -> Result<(), MyError> {
    mac_notification_sys::send_notification("Test", &None, "Testing...", &None)?;
    Ok(())
}

impl error::Error for MyError {}

impl fmt::Display for MyError {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        write!(formatter, "{}", self)
    }
}

impl From<mac_notification_sys::error::Error> for MyError {
    fn from(err: mac_notification_sys::error::Error) -> Self {
        MyError::MacOs(err)
    }
}

mod test {
    use super::*;

    #[test]
    fn notifies() {
        assert!(notify().is_ok());
    }
}
lelandjansen commented 5 years ago

Resolved by #12.