Debian Codegraph @

third_party/rust/pulse/src/error.rs

raw
1 // Copyright © 2017 Mozilla Foundation
2 //
3 // This program is made available under an ISC-style license. See the
4 // accompanying file LICENSE for details.
5
6 use ffi;
7 use std::ffi::CStr;
8
9 #[macro_export]
10 macro_rules! error_result {
11 ($t:expr, $err:expr) => {
12 if $err >= 0 {
13 Ok($t)
14 } else {
15 Err(ErrorCode::from_error_result($err))
16 }
17 };
18 }
19
20 #[derive(Debug, PartialEq)]
21 pub struct ErrorCode {
22 err: ffi::pa_error_code_t,
23 }
24
25 impl ErrorCode {
26 pub fn from_error_result(err: i32) -> Self {
27 debug_assert!(err < 0);
28 ErrorCode {
29 err: (-err) as ffi::pa_error_code_t,
30 }
31 }
32
33 pub fn from_error_code(err: ffi::pa_error_code_t) -> Self {
34 debug_assert!(err > 0);
35 ErrorCode { err: err }
36 }
37
38 fn desc(&self) -> &'static str {
39 let cstr = unsafe { CStr::from_ptr(ffi::pa_strerror(self.err)) };
40 cstr.to_str().unwrap()
41 }
42 }
43
44 impl ::std::error::Error for ErrorCode {
45 fn description(&self) -> &str {
46 self.desc()
47 }
48 }
49
50 impl ::std::fmt::Display for ErrorCode {
51 fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
52 write!(f, "{:?}: {}", self, self.desc())
53 }
54 }