Skip to content

Commit

Permalink
refactor: Use a custom error type instead of failure
Browse files Browse the repository at this point in the history
  • Loading branch information
orhun committed Oct 17, 2020
1 parent 4b25576 commit e0b52cc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
7 changes: 2 additions & 5 deletions Cargo.toml
@@ -1,13 +1,10 @@
[package]
authors = ["Marcel Müller <neikos@neikos.email>"]
name = "textwidth"
version = "1.1.0"
description = "Get the width of a piece of text for a font through xlib"
authors = ["Marcel Müller <neikos@neikos.email>"]
license = "MIT"


[dependencies]
failure = "0.1.1"
edition = "2018"

[dependencies.x11]
version = "2.18.0"
Expand Down
22 changes: 16 additions & 6 deletions src/lib.rs
@@ -1,14 +1,24 @@
extern crate x11;
#[macro_use]
extern crate failure;

use std::error::Error;
use std::ffi::CString;
use std::fmt;
use std::mem;
use std::os::raw::{c_char, c_int};
use std::ptr;

use x11::xlib;

#[derive(Debug)]
struct XError(String);

impl fmt::Display for XError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "X Error: {}", self.0)
}
}

impl Error for XError {}

enum Data {
FontSet {
display: *mut xlib::Display,
Expand All @@ -30,13 +40,13 @@ impl Context {
///
/// The font string should be of the X11 form, as selected by `fontsel`.
/// XFT is not supported!
pub fn new(name: &str) -> Result<Context, failure::Error> {
pub fn new(name: &str) -> Result<Context, Box<dyn Error>> {
unsafe {
let name: CString = CString::new(name)?;

let dpy = xlib::XOpenDisplay(ptr::null());
if dpy.is_null() {
return Err(format_err!("Could not open display"));
return Err(Box::new(XError("Could not open display".into())));
}

let missing_ptr: *mut *mut c_char = mem::uninitialized();
Expand Down Expand Up @@ -65,7 +75,7 @@ impl Context {

if xfont.is_null() {
xlib::XCloseDisplay(dpy);
return Err(format_err!("Could not load font: {:?}", name))?;
return Err(Box::new(XError(format!("Could not load font: {:?}", name))))?;
}

return Ok(Context {
Expand Down

0 comments on commit e0b52cc

Please sign in to comment.