Skip to main content

cue_rs/
error.rs

1//! Error types returned by cue-rs operations.
2
3use core::ffi::c_char;
4
5use thiserror::Error;
6
7/// Opaque handle type matching `typedef uintptr_t cue_error` from libcue.
8type CueErrorHandle = usize;
9
10unsafe extern "C" {
11    fn cue_error_string(err: CueErrorHandle) -> *mut c_char;
12}
13
14/// A libcue error handle (`cue_error`).
15#[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/// Errors that can occur when working with CUE values.
35#[derive(Debug, Error)]
36pub enum Error {
37    /// `cue_newctx` returned 0; the libcue runtime could not allocate a
38    /// context.
39    #[error("cue_newctx returned 0; the libcue runtime could not allocate a context")]
40    ContextCreationFailed,
41
42    /// The string passed to `cue_compile_string` contains an interior nul byte.
43    #[error("string contains an interior nul byte: {0}")]
44    StringContainsNul(std::ffi::NulError),
45
46    /// A libcue operation returned a `cue_error` handle.
47    #[error("{0}")]
48    Cue(CueError),
49
50    /// A string decoded from libcue was not valid UTF-8.
51    #[error("decoded string is not valid UTF-8: {0}")]
52    InvalidUtf8(std::str::Utf8Error),
53}