1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use std::str;
use std::ffi::CStr;
use std::fmt::{self, Formatter, Debug, Display};
use std::error;
use ffi::{self, GQuark};
use glib_container::GlibContainer;
use translate::ToGlibPtr;
pub struct Error {
pointer: *mut ffi::GError
}
impl Error {
pub fn new_literal(domain: GQuark, code: i32, message: &str) -> Option<Error> {
let tmp_pointer = unsafe {
ffi::g_error_new_literal(domain, code, message.to_glib_none().0)
};
if tmp_pointer.is_null() {
None
} else {
Some(Error{pointer: tmp_pointer})
}
}
pub fn release(&mut self) -> () {
if !self.pointer.is_null() {
unsafe { ffi::g_error_free(self.pointer) };
self.pointer = ::std::ptr::null_mut();
}
}
pub fn matches(&self, domain: GQuark, code: i32) -> bool {
match unsafe { ffi::g_error_matches(self.pointer, domain, code) } {
ffi::GFALSE => false,
_ => true
}
}
pub fn set(&mut self, domain: GQuark, code: i32, message: &str) -> () {
unsafe {
ffi::g_set_error_literal(&mut self.pointer, domain, code, message.to_glib_none().0)
}
}
pub fn propagate(&mut self, other: &Error) -> () {
unsafe { 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 { ffi::g_error_copy(self.pointer) };
if tmp_pointer.is_null() {
Error {
pointer: ::std::ptr::null_mut()
}
} else {
GlibContainer::wrap(tmp_pointer)
}
}
}
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 {
write!(f, "{}", self.message())
}
}
impl error::Error for Error {
fn description(&self) -> &str {
self.message()
}
}
impl Drop for Error {
fn drop(&mut self) {
self.release();
}
}
impl GlibContainer<*mut ffi::GError> for Error {
fn wrap(pointer: *mut ffi::GError) -> Error {
Error {
pointer: pointer
}
}
fn unwrap(&self) -> *mut ffi::GError {
self.pointer
}
}