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
use std::{ffi::CStr, fmt};

use browser_window_c::*;

#[derive(Debug)]
pub struct Error(cbw_Err);

impl fmt::Display for Error {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		unsafe {
			let c_msg = cbw_Err_message(&self.0);

			let result = write!(
				f,
				"[{}] {}",
				self.0.code,
				CStr::from_ptr(c_msg)
					.to_str()
					.expect("invalid utf-8 in bw_Err error message")
			);

			cbw_string_freeCstr(c_msg);

			return result;
		}
	}
}

impl std::error::Error for Error {
	fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { None }
}

impl From<cbw_Err> for Error {
	fn from(e: cbw_Err) -> Self { Self(e) }
}