Skip to content
This repository has been archived by the owner on Mar 7, 2020. It is now read-only.

Commit

Permalink
chore/dependencies: update unwrap to 1.2.0
Browse files Browse the repository at this point in the history
As explained in MAID-2922, this allows us to take advantage of the
unwrap_err! macro.

Also switch from `error_chain` to `quick_error`.
  • Loading branch information
Marcin S authored and octol committed Aug 29, 2018
1 parent d7c25c4 commit 26f3559
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 284 deletions.
343 changes: 118 additions & 225 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Expand Up @@ -10,13 +10,13 @@ repository = "https://github.com/maidsafe/system_uri"
version = "0.4.0"

[dependencies]
error-chain = "0.11.0-rc"
ffi_utils = { version = "~0.4.0", optional = true }
quick-error = "~1.2.2"
ffi_utils = { version = "~0.8.0", optional = true }
libc = { version = "~0.2.33", optional = true }

[dev-dependencies]
rand = "~0.3.18"
unwrap = "~1.1.0"
unwrap = "~1.2.0"

[target.'cfg(target_os = "windows")'.dependencies]
winreg = "~0.4.0"
Expand Down
14 changes: 3 additions & 11 deletions examples/test.rs
Expand Up @@ -9,6 +9,8 @@

extern crate rand;
extern crate system_uri;
#[macro_use]
extern crate unwrap;

use rand::Rng;
use std::env;
Expand All @@ -19,7 +21,7 @@ use system_uri::{install, open, App, SystemUriError};

fn install_and_open() -> Result<(), SystemUriError> {
let mut rng = rand::thread_rng();
let exec = String::from(std::env::current_exe().unwrap().to_str().unwrap());
let exec = String::from(unwrap!(unwrap!(std::env::current_exe()).to_str()));
let app = App::new(
"net.maidsafe.example".to_string(),
"MaidSafe Ltd.".to_string(),
Expand Down Expand Up @@ -61,16 +63,6 @@ fn main() {
if let Err(ref e) = install_and_open() {
println!("error: {}", e);

for e in e.iter().skip(1) {
println!("caused by: {}", e);
}

// The backtrace is not always generated. Try to run this example
// with `RUST_BACKTRACE=1`.
if let Some(backtrace) = e.backtrace() {
println!("backtrace: {:?}", backtrace);
}

::std::process::exit(1);
}
}
14 changes: 3 additions & 11 deletions examples/test64.rs
Expand Up @@ -10,6 +10,8 @@
extern crate ffi_utils;
extern crate rand;
extern crate system_uri;
#[macro_use]
extern crate unwrap;

use ffi_utils::base64_encode;
use rand::Rng;
Expand All @@ -21,7 +23,7 @@ use system_uri::{install, open, App, SystemUriError};

fn install_and_open() -> Result<(), SystemUriError> {
let mut rng = rand::thread_rng();
let exec = String::from(std::env::current_exe().unwrap().to_str().unwrap());
let exec = String::from(unwrap!(unwrap!(std::env::current_exe()).to_str()));
let app = App::new(
"net.maidsafe.example".to_string(),
"MaidSafe Ltd.".to_string(),
Expand Down Expand Up @@ -63,16 +65,6 @@ fn main() {
if let Err(ref e) = install_and_open() {
println!("error: {}", e);

for e in e.iter().skip(1) {
println!("caused by: {}", e);
}

// The backtrace is not always generated. Try to run this example
// with `RUST_BACKTRACE=1`.
if let Some(backtrace) = e.backtrace() {
println!("backtrace: {:?}", backtrace);
}

::std::process::exit(1);
}
}
6 changes: 3 additions & 3 deletions src/ffi.rs
Expand Up @@ -9,7 +9,7 @@

#![allow(unsafe_code)]

use super::errors::*;
use super::errors::Error;
use super::{install as rust_install, open as rust_open, App};
use ffi_utils::{
catch_unwind_cb, from_c_str, vec_clone_from_raw_parts, ErrorCode, FfiResult, FFI_RESULT_OK,
Expand All @@ -26,7 +26,7 @@ pub unsafe extern "C" fn open_uri(
user_data: *mut c_void,
o_cb: extern "C" fn(*mut c_void, *const FfiResult),
) {
catch_unwind_cb(user_data, o_cb, || -> Result<()> {
catch_unwind_cb(user_data, o_cb, || -> Result<(), Error> {
let uri = from_c_str(uri)?;
rust_open(uri)?;
o_cb(user_data, FFI_RESULT_OK);
Expand All @@ -48,7 +48,7 @@ pub unsafe extern "C" fn install(
user_data: *mut c_void,
o_cb: extern "C" fn(*mut c_void, *const FfiResult),
) {
catch_unwind_cb(user_data, o_cb, || -> Result<()> {
catch_unwind_cb(user_data, o_cb, || -> Result<(), Error> {
let mut exec = String::new();
let args = vec_clone_from_raw_parts(exec_args, exec_args_len);
for arg in args {
Expand Down
47 changes: 30 additions & 17 deletions src/lib.rs
Expand Up @@ -83,11 +83,9 @@
feature = "clippy",
allow(use_debug, too_many_arguments, needless_return)
)]
// `error_chain!` can recurse deeply
#![recursion_limit = "1024"]

#[macro_use]
extern crate error_chain;
extern crate quick_error;

#[cfg(any(target_os = "macos", feature = "ffi"))]
extern crate libc;
Expand All @@ -102,22 +100,37 @@ mod app;
pub use app::App;

mod errors {
use ffi_utils::StringError;
use std::io;
use std::str::Utf8Error;

error_chain! {
types {
Error, ErrorKind, ChainErr, Result;
}

foreign_links {
Utf8Error(Utf8Error);
}

errors {
/// The SystemURI error used to wrap problems
SystemUriError(t: String) {
description("System URI Error")
display("Could not execute: {}", t)
quick_error! {
/// System URI error variants.
#[derive(Debug)]
pub enum Error {
/// IO error.
IoError(error: io::Error) {
description("Io error")
display("I/O error: {}", error)
from()
}
/// String error.
StringError(error: StringError) {
description("String error")
display("String error: {:?}", error)
from()
}
/// Utf-8 error.
Utf8Error(error: Utf8Error) {
description(error.description())
display("Utf-8 error: {}", error)
from()
}
/// Unexpected error.
Unexpected(s: &'static str) {
description(s)
display("Unexpected error: {}", s)
from()
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/linux.rs
Expand Up @@ -9,15 +9,15 @@

use app::App;

use errors::*;
use errors::Error;
use std::fs::{create_dir_all, File};
use std::io::Write;
use std::path::PathBuf;
use std::process::Command;
use xdg_basedir::dirs::get_data_home;

/// Open a given URI.
pub fn open<S: Into<String>>(uri: S) -> Result<()> {
pub fn open<S: Into<String>>(uri: S) -> Result<(), Error> {
let uri = uri.into();

let output = Command::new("xdg-open")
Expand Down Expand Up @@ -45,7 +45,7 @@ fn clean_string(input: &str) -> String {
///
/// `app` should contain all fields necessary for registering URIs on all systems. `schemes` should
/// provide a list of schemes (the initial part of a URI, like `https`).
pub fn install(app: &App, schemes: &[String]) -> Result<()> {
pub fn install(app: &App, schemes: &[String]) -> Result<(), Error> {
let home = get_data_home().chain_err(|| "Home directory not found")?;
let ascii_name = format!(
"{}-{}.desktop",
Expand Down
11 changes: 4 additions & 7 deletions src/macos.rs
Expand Up @@ -11,7 +11,7 @@

use app::App;

use errors::*;
use errors::Error;
use libc;
use std::process::Command;

Expand Down Expand Up @@ -57,11 +57,8 @@ fn convert_to_cfstring(content: &str) -> CFStringRef {
}

/// Open a given URI.
pub fn open(uri: String) -> Result<()> {
let output = Command::new("open")
.arg(uri)
.output()
.chain_err(|| "Could not execute open")?;
pub fn open(uri: String) -> Result<(), Error> {
let output = Command::new("open").arg(uri).output()?;

if output.status.success() {
Ok(())
Expand All @@ -74,7 +71,7 @@ pub fn open(uri: String) -> Result<()> {
///
/// `app` should contain all fields necessary for registering URIs on all systems. `schemes` should
/// provide a list of schemes (the initial part of a URI, like `https`).
pub fn install(app: &App, schemes: &[String]) -> Result<()> {
pub fn install(app: &App, schemes: &[String]) -> Result<(), Error> {
let bundle_id = convert_to_cfstring(app.bundle_id.as_str());
for scheme in schemes {
// FIXME: do we have any way to learn this failed?
Expand Down
6 changes: 3 additions & 3 deletions src/windows.rs
Expand Up @@ -16,7 +16,7 @@ use self::winreg::enums::HKEY_CURRENT_USER;
use self::winreg::RegKey;
use app::App;

use errors::*;
use errors::Error;
use std::ffi::OsStr;
use std::os::windows::ffi::OsStrExt;
use std::path::Path;
Expand Down Expand Up @@ -46,7 +46,7 @@ extern "system" {
///
/// `app` should contain all fields necessary for registering URIs on all systems. `schemes` should
/// provide a list of schemes (the initial part of a URI, like `https`).
pub fn install(app: &App, schemes: &[String]) -> Result<()> {
pub fn install(app: &App, schemes: &[String]) -> Result<(), Error> {
// but we can't write on root, we'll have to do it for the curent user only
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
for protocol in schemes {
Expand All @@ -73,7 +73,7 @@ pub fn install(app: &App, schemes: &[String]) -> Result<()> {

/// Open a given URI.
#[allow(unsafe_code)]
pub fn open(uri: String) -> Result<()> {
pub fn open(uri: String) -> Result<(), Error> {
let err = unsafe {
ShellExecuteW(
ptr::null_mut(),
Expand Down
2 changes: 1 addition & 1 deletion tests/integration_test.rs
Expand Up @@ -124,7 +124,7 @@ fn ffi_install_and_check() {
// let's copy the executable to a path
// with a white space to test it's supported
let exec_with_white_space = format!("{} after_white_space", exec);
assert!(std::fs::copy(exec.clone(), exec_with_white_space.clone()).unwrap() > 0);
assert!(unwrap!(std::fs::copy(exec.clone(), exec_with_white_space.clone())) > 0);

let schema = gen_rand_schema();
let schema_cstr = unwrap!(CString::new(schema.clone()));
Expand Down

0 comments on commit 26f3559

Please sign in to comment.