Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Commit

Permalink
Update Error bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
gkoz committed Jan 9, 2016
1 parent d416f1a commit c1a30e9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 77 deletions.
134 changes: 58 additions & 76 deletions src/error.rs
@@ -1,84 +1,65 @@
// Copyright 2013-2015, The Gtk-rs Project Developers.
// Copyright 2016, The Gtk-rs Project Developers.
// See the COPYRIGHT file at the top-level directory of this distribution.
// Licensed under the MIT license, see the LICENSE file or <http://opensource.org/licenses/MIT>

use std::str;
use std::ffi::CStr;
use std::fmt::{self, Formatter, Debug, Display};
use std::error;
use glib_ffi::{self, GQuark};
use glib_container::GlibContainer;
use translate::ToGlibPtr;
use std::fmt;
use std::str;
use translate::*;
use glib_ffi;

pub struct Error {
pointer: *mut glib_ffi::GError
glib_wrapper! {
pub struct Error(Boxed<glib_ffi::GError>);

match fn {
copy => |ptr| glib_ffi::g_error_copy(ptr),
free => |ptr| glib_ffi::g_error_free(ptr),
}
}

impl Error {
pub fn new_literal(domain: GQuark, code: i32, message: &str) -> Option<Error> {
let tmp_pointer = unsafe {
glib_ffi::g_error_new_literal(domain, code, message.to_glib_none().0)
};

if tmp_pointer.is_null() {
None
} else {
Some(Error{pointer: tmp_pointer})
/// Creates an error with supplied error enum variant and message.
pub fn new<T: ErrorDomain>(error: T, message: &str) -> Error {
unsafe {
from_glib_full(
glib_ffi::g_error_new_literal(T::domain(), error.code(), message.to_glib_none().0))
}
}

pub fn release(&mut self) -> () {
if !self.pointer.is_null() {
unsafe { glib_ffi::g_error_free(self.pointer) };
self.pointer = ::std::ptr::null_mut();
/// Tries to match against an error enum.
///
/// Example
/// ```ignore
/// if let Some(file_error) = error.matches::<FileError>() {
/// match file_error {
/// Exist => ...
/// Isdir => ...
/// ...
/// }
/// }
/// ```
pub fn matches<T: ErrorDomain>(&self) -> Option<T> {
if self.0.domain == T::domain() {
T::from(self.0.code)
}
}

pub fn matches(&self, domain: GQuark, code: i32) -> bool {
match unsafe { glib_ffi::g_error_matches(self.pointer, domain, code) } {
glib_ffi::GFALSE => false,
_ => true
else {
None
}
}

pub fn set(&mut self, domain: GQuark, code: i32, message: &str) -> () {
fn message(&self) -> &str {
unsafe {
glib_ffi::g_set_error_literal(&mut self.pointer, domain, code, message.to_glib_none().0)
}
}

pub fn propagate(&mut self, other: &Error) -> () {
unsafe { glib_ffi::g_propagate_error(&mut self.pointer, other.pointer) }
}

pub fn message(&self) -> &str {
let c_str = unsafe { CStr::from_ptr((*self.pointer).message) };
str::from_utf8(c_str.to_bytes()).unwrap()
}
}

impl Clone for Error {
fn clone(&self) -> Error {
let tmp_pointer = unsafe { glib_ffi::g_error_copy(self.pointer) };

if tmp_pointer.is_null() {
Error {
pointer: ::std::ptr::null_mut()
}
} else {
GlibContainer::wrap(tmp_pointer)
let bytes = CStr::from_ptr(self.0.message).to_bytes();
str::from_utf8(bytes).unwrap_or_else(|err| {
str::from_utf8(&bytes[..err.valid_up_to()]).unwrap()
})
}
}
}

impl Debug for Error {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", self.message())
}
}

impl Display for Error {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.message())
}
}
Expand All @@ -89,20 +70,21 @@ impl error::Error for Error {
}
}

impl Drop for Error {
fn drop(&mut self) {
self.release();
}
}

impl GlibContainer<*mut glib_ffi::GError> for Error {
fn wrap(pointer: *mut glib_ffi::GError) -> Error {
Error {
pointer: pointer
}
}

fn unwrap(&self) -> *mut glib_ffi::GError {
self.pointer
}
/// `GLib` error domain.
///
/// This trait is implemented by error enums and is not intended for end users.
pub trait ErrorDomain: Copy {
/// Returns the quark identifying the error domain.
///
/// As returned from `g_some_error_quark`.
fn domain() -> glib_ffi::GQuark;

/// Gets the integer representation of the variant.
fn code(self) -> i32;

/// Tries to convert an integer code to an enum variant.
///
/// By convention, the `Failed` variant, if present, is a catch-all,
/// i.e. any unrecognized codes map to it.
fn from(code: i32) -> Option<Self> where Self: Sized;
}
2 changes: 1 addition & 1 deletion src/lib.rs
Expand Up @@ -31,7 +31,7 @@ pub mod object;

mod app_info;
pub mod glib_container;
mod error;
pub mod error;
mod permission;
pub mod signal;
pub mod source;
Expand Down

0 comments on commit c1a30e9

Please sign in to comment.