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
mod bindings;

use std::{error::Error, ffi::CStr, fmt, ptr, slice, str};

pub use crate::bindings::*;

/**************************************************************
 * Implementations for C structs that are also useful in Rust *
 *************************************************** */

impl cbw_CStrSlice {
	pub fn empty() -> Self {
		Self {
			len: 0,
			data: ptr::null(),
		}
	}
}

impl From<&str> for cbw_CStrSlice {
	fn from(string: &str) -> Self {
		Self {
			data: string.as_bytes().as_ptr() as _,
			len: string.len() as _,
		}
	}
}

impl<'a> Into<&'a str> for cbw_CStrSlice {
	fn into(self) -> &'a str {
		let raw: &[u8] = unsafe { slice::from_raw_parts(self.data as _, self.len as _) };

		#[cfg(debug_assertions)]
		return str::from_utf8(raw).expect("Invalid UTF-8");
		#[cfg(not(debug_assertions))]
		return unsafe { str::from_utf8_unchecked(raw) };
	}
}

impl Into<String> for cbw_CStrSlice {
	fn into(self) -> String {
		let str: &str = self.into();

		str.to_owned()
	}
}

impl cbw_StrSlice {
	pub fn empty() -> Self {
		Self {
			len: 0,
			data: ptr::null_mut(),
		}
	}
}

impl<'a> Into<&'a str> for cbw_StrSlice {
	fn into(self) -> &'a str {
		let raw: &[u8] = unsafe { slice::from_raw_parts(self.data as _, self.len as _) };

		#[cfg(debug_assertions)]
		return str::from_utf8(raw).expect("Invalid UTF-8");
		#[cfg(not(debug_assertions))]
		return unsafe { str::from_utf8_unchecked(raw) };
	}
}

impl Into<String> for cbw_StrSlice {
	fn into(self) -> String {
		let str: &str = self.into();

		str.to_owned()
	}
}

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

impl fmt::Display for cbw_Err {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		unsafe {
			let msg_ptr = (self.alloc_message.unwrap())(self.code, self.data);
			let cstr = CStr::from_ptr(msg_ptr);
			write!(f, "{}", cstr.to_string_lossy().as_ref())
		}
	}
}