1use core::ffi::c_char;
4
5use thiserror::Error;
6
7type CueErrorHandle = usize;
9
10unsafe extern "C" {
11 fn cue_error_string(err: CueErrorHandle) -> *mut c_char;
12}
13
14#[derive(Debug)]
16pub struct CueError(pub(crate) CueErrorHandle);
17
18impl std::fmt::Display for CueError {
19 fn fmt(
20 &self,
21 f: &mut std::fmt::Formatter<'_>,
22 ) -> std::fmt::Result {
23 let ptr = unsafe { cue_error_string(self.0) };
24 if ptr.is_null() {
25 return f.write_str("<unknown cue error>");
26 }
27 let s = unsafe { std::ffi::CStr::from_ptr(ptr) }.to_string_lossy();
28 let result = f.write_str(&s);
29 unsafe { crate::drop::libc_free(ptr.cast()) };
30 result
31 }
32}
33
34#[derive(Debug, Error)]
36pub enum Error {
37 #[error("cue_newctx returned 0; the libcue runtime could not allocate a context")]
40 ContextCreationFailed,
41
42 #[error("string contains an interior nul byte: {0}")]
44 StringContainsNul(std::ffi::NulError),
45
46 #[error("{0}")]
48 Cue(CueError),
49
50 #[error("decoded string is not valid UTF-8: {0}")]
52 InvalidUtf8(std::str::Utf8Error),
53}