Debian Codegraph @

third_party/rust/alsa/src/error.rs

raw
1 #![macro_use]
2
3 use libc::{c_void, c_int, c_char, free};
4 use std::{fmt, str};
5 use std::ffi::CStr;
6 use std::error::Error as StdError;
7
8 /// ALSA error
9 ///
10 /// Most ALSA functions can return a negative error code.
11 /// If so, then that error code is wrapped into this `Error` struct.
12 /// An Error is also returned in case ALSA returns a string that
13 /// cannot be translated into Rust's UTF-8 strings.
14 #[derive(Debug, Clone, PartialEq, Copy)]
15 pub struct Error(&'static str, nix::Error);
16
17 pub type Result<T> = ::std::result::Result<T, Error>;
18
19 macro_rules! acheck {
20 ($f: ident ( $($x: expr),* ) ) => {{
21 let r = unsafe { alsa::$f( $($x),* ) };
22 if r < 0 { Err(Error::new(stringify!($f), -r as ::libc::c_int)) }
23 else { Ok(r) }
24 }}
25 }
26
27 pub fn from_const<'a>(func: &'static str, s: *const c_char) -> Result<&'a str> {
28 if s.is_null() { return Err(invalid_str(func)) };
29 let cc = unsafe { CStr::from_ptr(s) };
30 str::from_utf8(cc.to_bytes()).map_err(|_| invalid_str(func))
31 }
32
33 pub fn from_alloc(func: &'static str, s: *mut c_char) -> Result<String> {
34 if s.is_null() { return Err(invalid_str(func)) };
35 let c = unsafe { CStr::from_ptr(s) };
36 let ss = str::from_utf8(c.to_bytes()).map_err(|_| {
37 unsafe { free(s as *mut c_void); }
38 invalid_str(func)
39 })?.to_string();
40 unsafe { free(s as *mut c_void); }
41 Ok(ss)
42 }
43
44 pub fn from_code(func: &'static str, r: c_int) -> Result<c_int> {
45 if r < 0 { Err(Error::new(func, r)) }
46 else { Ok(r) }
47 }
48
49 impl Error {
50 pub fn new(func: &'static str, res: c_int) -> Error {
51 let errno = nix::errno::Errno::from_i32(res as i32);
52 Error(func, errno)
53 }
54
55 pub fn unsupported(func: &'static str) -> Error {
56 Error(func, nix::Error::ENOTSUP)
57 }
58
59 /// The function which failed.
60 pub fn func(&self) -> &'static str { self.0 }
61
62
63 /// Underlying error
64 ///
65 /// Match this against the re-export of `nix::Error` in this crate, not against a specific version
66 /// of the nix crate. The nix crate version might be updated with minor updates of this library.
67 pub fn errno(&self) -> nix::Error { self.1 }
68
69 /// Underlying error
70 ///
71 /// Match this against the re-export of `nix::Error` in this crate, not against a specific version
72 /// of the nix crate. The nix crate version might be updated with minor updates of this library.
73 pub fn nix_error(&self) -> nix::Error { self.1 }
74 }
75
76 pub fn invalid_str(func: &'static str) -> Error { Error(func, nix::Error::EILSEQ) }
77
78 impl StdError for Error {
79 fn source(&self) -> Option<&(dyn StdError + 'static)> { Some(&self.1) }
80 fn description(&self) -> &str { "ALSA error" }
81 }
82
83 impl fmt::Display for Error {
84 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
85 write!(f, "ALSA function '{}' failed with error '{}'", self.0, self.1)
86 }
87 }
88
89 impl From<Error> for fmt::Error {
90 fn from(_: Error) -> fmt::Error { fmt::Error }
91 }
92
93
94 #[test]
95 fn broken_pcm_name() {
96 use std::ffi::CString;
97 let e = crate::PCM::open(&*CString::new("this_PCM_does_not_exist").unwrap(), crate::Direction::Playback, false).err().unwrap();
98 assert_eq!(e.func(), "snd_pcm_open");
99 assert_eq!(e.errno(), nix::errno::Errno::ENOENT);
100 }