1use libmtp_sys as ffi;
4use std::string::FromUtf8Error;
5use thiserror::Error as ErrorTrait;
6
7#[derive(Debug, Clone, Copy)]
10pub enum MtpErrorKind {
11 General,
12 PtpLayer,
13 UsbLayer,
14 MemoryAllocation,
15 NoDeviceAttached,
16 StorageFull,
17 Connecting,
18 Cancelled,
19}
20
21impl MtpErrorKind {
22 pub(crate) fn from_error_number(error_code: ffi::LIBMTP_error_number_t) -> Option<Self> {
23 match error_code {
24 ffi::LIBMTP_error_number_t_LIBMTP_ERROR_NONE => None,
25 ffi::LIBMTP_error_number_t_LIBMTP_ERROR_GENERAL => Some(Self::General),
26 ffi::LIBMTP_error_number_t_LIBMTP_ERROR_PTP_LAYER => Some(Self::PtpLayer),
27 ffi::LIBMTP_error_number_t_LIBMTP_ERROR_USB_LAYER => Some(Self::UsbLayer),
28 ffi::LIBMTP_error_number_t_LIBMTP_ERROR_MEMORY_ALLOCATION => {
29 Some(Self::MemoryAllocation)
30 }
31 ffi::LIBMTP_error_number_t_LIBMTP_ERROR_NO_DEVICE_ATTACHED => {
32 Some(Self::NoDeviceAttached)
33 }
34 ffi::LIBMTP_error_number_t_LIBMTP_ERROR_STORAGE_FULL => Some(Self::StorageFull),
35 ffi::LIBMTP_error_number_t_LIBMTP_ERROR_CONNECTING => Some(Self::Connecting),
36 ffi::LIBMTP_error_number_t_LIBMTP_ERROR_CANCELLED => Some(Self::Cancelled),
37 _ => None,
38 }
39 }
40}
41
42#[derive(Debug, Clone, ErrorTrait)]
45pub enum Error {
46 #[error("Unknown error (possibly a libmtp undocumented error)")]
48 Unknown,
49
50 #[error("Internal libmtp ({kind:?}): {text}")]
53 MtpError { kind: MtpErrorKind, text: String },
54
55 #[error("Utf8 error ({source})")]
57 Utf8Error { source: FromUtf8Error },
58}
59
60impl Default for Error {
61 fn default() -> Self {
62 Error::Unknown
63 }
64}
65
66impl Error {
67 pub(crate) unsafe fn from_latest_error(mut list: *const ffi::LIBMTP_error_t) -> Option<Self> {
68 if list.is_null() {
69 None
70 } else {
71 while !(*list).next.is_null() {
72 list = (*list).next;
73 }
74
75 let error_t = &*list;
76
77 let kind = MtpErrorKind::from_error_number(error_t.errornumber)?;
78 let u8vec = cstr_to_u8vec!(error_t.error_text);
79 let text = String::from_utf8_lossy(&u8vec).into_owned();
80
81 Some(Error::MtpError { kind, text })
82 }
83 }
84}
85
86impl From<FromUtf8Error> for Error {
87 fn from(source: FromUtf8Error) -> Self {
88 Error::Utf8Error { source }
89 }
90}