Skip to content

Commit a408860

Browse files
committed
Add error domain example
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
1 parent 205d5c8 commit a408860

File tree

10 files changed

+136
-5
lines changed

10 files changed

+136
-5
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
HEADERS = \
22
include/ex/ex.h \
3+
include/ex/error.h \
34
include/ex/flags.h \
45
include/ex/color.h \
56
include/ex/foo.h \
@@ -13,6 +14,9 @@ RUST_SOURCES = \
1314
src/color/ffi.rs \
1415
src/color/imp.rs \
1516
src/color/mod.rs \
17+
src/error/ffi.rs \
18+
src/error/imp.rs \
19+
src/error/mod.rs \
1620
src/flags/ffi.rs \
1721
src/flags/imp.rs \
1822
src/flags/mod.rs \

include/ex/error.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef __ERROR_H_
2+
#define __ERROR_H_
3+
4+
#include <glib-object.h>
5+
6+
G_BEGIN_DECLS
7+
8+
typedef enum ExError
9+
{
10+
EX_ERROR_INVALID_ARGUMENT,
11+
EX_ERROR_FAILED,
12+
} ExError;
13+
14+
#define EX_ERROR (ex_error_quark())
15+
16+
GQuark ex_error_quark (void);
17+
18+
G_END_DECLS
19+
20+
#endif /* __ERROR_H_ */

include/ex/ex.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef __EX_H__
22
#define __EX_H__
33

4+
#include "error.h"
45
#include "flags.h"
56
#include "color.h"
67
#include "foo.h"

src/color/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use imp::ffi;
66
#[cfg(feature = "bindings")]
77
mod ffi;
88

9-
use glib::{StaticType, Type, translate::*};
9+
use glib::{translate::*, StaticType, Type};
1010

1111
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy)]
1212
#[non_exhaustive]

src/error/ffi.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
use libc::c_int;
2+
3+
pub type ExError = c_int;
4+
5+
pub const EX_ERROR_INVALID_ARGUMENT: ExError = 0;
6+
pub const EX_ERROR_FAILED: ExError = 1;
7+
8+
extern "C" {
9+
pub fn ex_error_quark() -> glib::ffi::GQuark;
10+
}

src/error/imp.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#[derive(Clone, Copy, Debug, PartialEq, Eq, glib::GErrorDomain)]
2+
#[gerror_domain(name = "ExError")]
3+
#[repr(C)]
4+
pub enum Error {
5+
InvalidArgument,
6+
Failed,
7+
}
8+
9+
pub(crate) mod ffi {
10+
use glib::translate::*;
11+
12+
pub type ExError = i32;
13+
14+
pub const EX_ERROR_INVALID_ARGUMENT: ExError = super::Error::InvalidArgument as i32;
15+
pub const EX_ERROR_FAILED: ExError = super::Error::Failed as i32;
16+
17+
#[no_mangle]
18+
pub unsafe extern "C" fn ex_error_quark() -> glib::ffi::GQuark {
19+
<super::Error as glib::error::ErrorDomain>::domain().into_glib()
20+
}
21+
}

src/error/mod.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#[cfg(not(feature = "bindings"))]
2+
pub mod imp;
3+
#[cfg(not(feature = "bindings"))]
4+
use imp::ffi;
5+
6+
#[cfg(feature = "bindings")]
7+
mod ffi;
8+
9+
use glib::translate::*;
10+
11+
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Clone, Copy)]
12+
#[non_exhaustive]
13+
pub enum Error {
14+
InvalidArgument,
15+
Failed,
16+
__Unknown(i32),
17+
}
18+
19+
impl IntoGlib for Error {
20+
type GlibType = ffi::ExError;
21+
22+
fn into_glib(self) -> ffi::ExError {
23+
match self {
24+
Error::InvalidArgument => ffi::EX_ERROR_INVALID_ARGUMENT,
25+
Error::Failed => ffi::EX_ERROR_FAILED,
26+
Error::__Unknown(value) => value,
27+
}
28+
}
29+
}
30+
31+
impl FromGlib<ffi::ExError> for Error {
32+
unsafe fn from_glib(value: ffi::ExError) -> Self {
33+
match value {
34+
0 => Error::InvalidArgument,
35+
1 => Error::Failed,
36+
value => Error::__Unknown(value),
37+
}
38+
}
39+
}
40+
41+
impl glib::error::ErrorDomain for Error {
42+
fn domain() -> glib::Quark {
43+
unsafe { from_glib(ffi::ex_error_quark()) }
44+
}
45+
46+
fn code(self) -> i32 {
47+
self.into_glib()
48+
}
49+
50+
fn from(code: i32) -> Option<Self> {
51+
unsafe { Some(Self::from_glib(code)) }
52+
}
53+
}
54+
55+
#[cfg(test)]
56+
mod tests {
57+
use super::*;
58+
59+
#[test]
60+
fn test_error() {
61+
let err = glib::Error::new(Error::Failed, "We are all mad here.");
62+
assert!(err.is::<Error>());
63+
assert!(matches!(err.kind::<Error>(), Some(Error::Failed)));
64+
}
65+
}

src/flags/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use imp::ffi;
66
#[cfg(feature = "bindings")]
77
mod ffi;
88

9-
use glib::{StaticType, Type, translate::*};
9+
use glib::{translate::*, StaticType, Type};
1010

1111
use bitflags::bitflags;
1212

@@ -52,6 +52,5 @@ mod tests {
5252
let v = e.value(1).unwrap();
5353
assert_eq!(v.name(), "Some");
5454
assert_eq!(v.nick(), "some");
55-
5655
}
5756
}

src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
pub mod bar;
2+
pub mod color;
3+
pub mod error;
4+
pub mod flags;
25
pub mod foo;
36
pub mod nameable;
47
pub mod rstring;
58
pub mod shared_rstring;
6-
pub mod color;
7-
pub mod flags;

test.vala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ void on_incremented (int val, int inc) {
44
stdout.printf ("incremented to %d by %d\n", val, inc);
55
}
66

7+
void throw_err() throws Ex.Error {
8+
throw new Ex.Error.FAILED ("something went wrong");
9+
}
10+
711
public int main (string[] args) {
812
var foo = new Ex.Foo ("foo's name");
913
foo.incremented.connect (on_incremented);
@@ -41,5 +45,11 @@ public int main (string[] args) {
4145
var ss2 = ss.ref ();
4246
stdout.printf ("shared rstring 2: %s\n", ss2.get ());
4347

48+
try {
49+
throw_err();
50+
assert_not_reached();
51+
} catch (Ex.Error e) {
52+
assert_error(e, Ex.Error.quark(), Ex.Error.FAILED);
53+
}
4454
return 0;
4555
}

0 commit comments

Comments
 (0)