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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
//! Error handling interface.
//!
//! This module holds the generic error and result types to interface with `ctru_sys` and the [`ctru-rs`](crate) safe wrapper.

use std::borrow::Cow;
use std::error;
use std::ffi::CStr;
use std::fmt;
use std::ops::{ControlFlow, FromResidual, Try};

use ctru_sys::result::{R_DESCRIPTION, R_LEVEL, R_MODULE, R_SUMMARY};

/// Custom type alias for generic [`ctru-rs`](crate) operations.
///
/// This type is compatible with [`ctru_sys::Result`] codes.
pub type Result<T> = ::std::result::Result<T, Error>;

/// Validity checker of raw [`ctru_sys::Result`] codes.
///
/// This struct supports the "try" syntax (`?`) to convert to an [`Error::Os`].
///
/// # Example
///
/// ```
/// use ctru::error::{Result, ResultCode};
///
/// pub fn main() -> Result<()> {
/// #   let _runner = test_runner::GdbRunner::default();
///     // We run an unsafe function which returns a `ctru_sys::Result`.
///     let result: ctru_sys::Result = unsafe { ctru_sys::hidInit() };
///
///     // The result code is parsed and any possible error gets returned by the function.
///     ResultCode(result)?;
///     Ok(())
/// }
/// ```
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord)]
#[repr(transparent)]
pub struct ResultCode(pub ctru_sys::Result);

impl Try for ResultCode {
    type Output = ();
    type Residual = Error;

    fn from_output(_: Self::Output) -> Self {
        Self(0)
    }

    fn branch(self) -> ControlFlow<Self::Residual, Self::Output> {
        // Wait timeouts aren't counted as "failures" in libctru, but an unfinished task means unsafety for us.
        // Luckily all summary cases are for system failures (except RS_SUCCESS).
        // I don't know if there are any cases in libctru where a Result holds a "failing" summary but a "success" code, so we'll just check for both.
        if ctru_sys::R_FAILED(self.0) || ctru_sys::R_SUMMARY(self.0) != ctru_sys::RS_SUCCESS {
            ControlFlow::Break(self.into())
        } else {
            ControlFlow::Continue(())
        }
    }
}

impl FromResidual for ResultCode {
    fn from_residual(e: <Self as Try>::Residual) -> Self {
        match e {
            Error::Os(result) => Self(result),
            _ => unreachable!(),
        }
    }
}

impl<T> FromResidual<Error> for Result<T> {
    fn from_residual(e: Error) -> Self {
        Err(e)
    }
}

/// The generic error enum returned by [`ctru-rs`](crate) functions.
///
/// This error enum supports parsing and displaying [`ctru_sys::Result`] codes.
#[non_exhaustive]
pub enum Error {
    /// Raw [`ctru_sys::Result`] codes.
    Os(ctru_sys::Result),
    /// Generic [`libc`] errors.
    Libc(String),
    /// Requested service is already active and cannot be activated again.
    ServiceAlreadyActive,
    /// `stdout` is already being redirected.
    OutputAlreadyRedirected,
    /// The buffer provided by the user to store some data is shorter than required.
    BufferTooShort {
        /// Length of the buffer provided by the user.
        provided: usize,
        /// Size of the requested data (in bytes).
        wanted: usize,
    },
    /// An error that doesn't fit into the other categories.
    Other(String),
}

impl Error {
    /// Create an [`Error`] out of the last set value in `errno`.
    ///
    /// This can be used to get a human-readable error string from calls to `libc` functions.
    pub(crate) fn from_errno() -> Self {
        let error_str = unsafe {
            let errno = ctru_sys::errno();
            let str_ptr = libc::strerror(errno);

            // Safety: strerror should always return a valid string,
            // even if the error number is unknown
            CStr::from_ptr(str_ptr)
        };

        // Copy out of the error string, since it may be changed by other libc calls later
        Self::Libc(error_str.to_string_lossy().into())
    }

    /// Check if the error is a timeout.
    pub fn is_timeout(&self) -> bool {
        match *self {
            Error::Os(code) => R_DESCRIPTION(code) == ctru_sys::RD_TIMEOUT,
            _ => false,
        }
    }
}

impl From<ctru_sys::Result> for Error {
    fn from(err: ctru_sys::Result) -> Self {
        Error::Os(err)
    }
}

impl From<ResultCode> for Error {
    fn from(err: ResultCode) -> Self {
        Self::Os(err.0)
    }
}

impl fmt::Debug for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            &Self::Os(err) => f
                .debug_struct("Error")
                .field("raw", &format_args!("{err:#08X}"))
                .field("level", &result_code_level_str(err))
                .field("module", &result_code_module_str(err))
                .field("summary", &result_code_summary_str(err))
                .field("description", &result_code_description_str(err))
                .finish(),
            Self::Libc(err) => f.debug_tuple("Libc").field(err).finish(),
            Self::ServiceAlreadyActive => f.debug_tuple("ServiceAlreadyActive").finish(),
            Self::OutputAlreadyRedirected => f.debug_tuple("OutputAlreadyRedirected").finish(),
            Self::BufferTooShort { provided, wanted } => f
                .debug_struct("BufferTooShort")
                .field("provided", provided)
                .field("wanted", wanted)
                .finish(),
            Self::Other(err) => f.debug_tuple("Other").field(err).finish(),
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            // TODO: should we consider using ctru_sys::osStrError here as well?
            // It might do some of the work for us or provide additional details
            &Self::Os(err) => write!(
                f,
                "libctru result code 0x{err:08X}: [{} {}] {}: {}",
                result_code_level_str(err),
                result_code_module_str(err),
                result_code_summary_str(err),
                result_code_description_str(err)
            ),
            Self::Libc(err) => write!(f, "{err}"),
            Self::ServiceAlreadyActive => write!(f, "service already active"),
            Self::OutputAlreadyRedirected => {
                write!(f, "output streams are already redirected to 3dslink")
            }
            Self::BufferTooShort{provided, wanted} => write!(f, "the provided buffer's length is too short (length = {provided}) to hold the wanted data (size = {wanted})"),
            Self::Other(err) => write!(f, "{err}"),
        }
    }
}

impl error::Error for Error {}

fn result_code_level_str(result: ctru_sys::Result) -> Cow<'static, str> {
    use ctru_sys::{
        RL_FATAL, RL_INFO, RL_PERMANENT, RL_REINITIALIZE, RL_RESET, RL_STATUS, RL_SUCCESS,
        RL_TEMPORARY, RL_USAGE,
    };

    Cow::Borrowed(match R_LEVEL(result) {
        RL_SUCCESS => "success",
        RL_INFO => "info",
        RL_FATAL => "fatal",
        RL_RESET => "reset",
        RL_REINITIALIZE => "reinitialize",
        RL_USAGE => "usage",
        RL_PERMANENT => "permanent",
        RL_TEMPORARY => "temporary",
        RL_STATUS => "status",
        code => return Cow::Owned(format!("(unknown level: {code:#x})")),
    })
}

fn result_code_summary_str(result: ctru_sys::Result) -> Cow<'static, str> {
    use ctru_sys::{
        RS_CANCELED, RS_INTERNAL, RS_INVALIDARG, RS_INVALIDRESVAL, RS_INVALIDSTATE, RS_NOP,
        RS_NOTFOUND, RS_NOTSUPPORTED, RS_OUTOFRESOURCE, RS_STATUSCHANGED, RS_SUCCESS,
        RS_WOULDBLOCK, RS_WRONGARG,
    };

    Cow::Borrowed(match R_SUMMARY(result) {
        RS_SUCCESS => "success",
        RS_NOP => "nop",
        RS_WOULDBLOCK => "would_block",
        RS_OUTOFRESOURCE => "out_of_resource",
        RS_NOTFOUND => "not_found",
        RS_INVALIDSTATE => "invalid_state",
        RS_NOTSUPPORTED => "not_supported",
        RS_INVALIDARG => "invalid_arg",
        RS_WRONGARG => "wrong_arg",
        RS_CANCELED => "canceled",
        RS_STATUSCHANGED => "status_changed",
        RS_INTERNAL => "internal",
        RS_INVALIDRESVAL => "invalid_res_val",
        code => return Cow::Owned(format!("(unknown summary: {code:#x})")),
    })
}

fn result_code_description_str(result: ctru_sys::Result) -> Cow<'static, str> {
    use ctru_sys::{
        RD_ALREADY_DONE, RD_ALREADY_EXISTS, RD_ALREADY_INITIALIZED, RD_BUSY, RD_CANCEL_REQUESTED,
        RD_INVALID_ADDRESS, RD_INVALID_COMBINATION, RD_INVALID_ENUM_VALUE, RD_INVALID_HANDLE,
        RD_INVALID_POINTER, RD_INVALID_RESULT_VALUE, RD_INVALID_SELECTION, RD_INVALID_SIZE,
        RD_MISALIGNED_ADDRESS, RD_MISALIGNED_SIZE, RD_NOT_AUTHORIZED, RD_NOT_FOUND,
        RD_NOT_IMPLEMENTED, RD_NOT_INITIALIZED, RD_NO_DATA, RD_OUT_OF_MEMORY, RD_OUT_OF_RANGE,
        RD_SUCCESS, RD_TIMEOUT, RD_TOO_LARGE,
    };

    Cow::Borrowed(match R_DESCRIPTION(result) {
        RD_SUCCESS => "success",
        RD_INVALID_RESULT_VALUE => "invalid_result_value",
        RD_TIMEOUT => "timeout",
        RD_OUT_OF_RANGE => "out_of_range",
        RD_ALREADY_EXISTS => "already_exists",
        RD_CANCEL_REQUESTED => "cancel_requested",
        RD_NOT_FOUND => "not_found",
        RD_ALREADY_INITIALIZED => "already_initialized",
        RD_NOT_INITIALIZED => "not_initialized",
        RD_INVALID_HANDLE => "invalid_handle",
        RD_INVALID_POINTER => "invalid_pointer",
        RD_INVALID_ADDRESS => "invalid_address",
        RD_NOT_IMPLEMENTED => "not_implemented",
        RD_OUT_OF_MEMORY => "out_of_memory",
        RD_MISALIGNED_SIZE => "misaligned_size",
        RD_MISALIGNED_ADDRESS => "misaligned_address",
        RD_BUSY => "busy",
        RD_NO_DATA => "no_data",
        RD_INVALID_COMBINATION => "invalid_combination",
        RD_INVALID_ENUM_VALUE => "invalid_enum_value",
        RD_INVALID_SIZE => "invalid_size",
        RD_ALREADY_DONE => "already_done",
        RD_NOT_AUTHORIZED => "not_authorized",
        RD_TOO_LARGE => "too_large",
        RD_INVALID_SELECTION => "invalid_selection",
        code => {
            let error = unsafe { CStr::from_ptr(ctru_sys::osStrError(result)) }.to_str();
            match error {
                Ok(err) => err,
                Err(_) => return Cow::Owned(format!("(unknown description: {code:#x})")),
            }
        }
    })
}

fn result_code_module_str(result: ctru_sys::Result) -> Cow<'static, str> {
    use ctru_sys::{
        RM_AC, RM_ACC, RM_ACT, RM_AM, RM_AM_LOW, RM_APPLET, RM_APPLICATION, RM_AVD, RM_BOSS,
        RM_CAM, RM_CARD, RM_CARDNOR, RM_CARD_SPI, RM_CEC, RM_CODEC, RM_COMMON, RM_CONFIG, RM_CSND,
        RM_CUP, RM_DBG, RM_DBM, RM_DD, RM_DI, RM_DLP, RM_DMNT, RM_DSP, RM_EC, RM_ENC, RM_FATFS,
        RM_FILE_SERVER, RM_FND, RM_FRIENDS, RM_FS, RM_FSI, RM_GD, RM_GPIO, RM_GSP, RM_GYROSCOPE,
        RM_HID, RM_HIO, RM_HIO_LOW, RM_HTTP, RM_I2C, RM_INVALIDRESVAL, RM_IR, RM_KERNEL, RM_L2B,
        RM_LDR, RM_LOADER_SERVER, RM_MC, RM_MCU, RM_MIC, RM_MIDI, RM_MP, RM_MPWL, RM_MVD, RM_NDM,
        RM_NEIA, RM_NEWS, RM_NEX, RM_NFC, RM_NFP, RM_NGC, RM_NIM, RM_NPNS, RM_NS, RM_NWM, RM_OLV,
        RM_OS, RM_PDN, RM_PI, RM_PIA, RM_PL, RM_PM, RM_PM_LOW, RM_PS, RM_PTM, RM_PXI, RM_QTM,
        RM_RDT, RM_RO, RM_ROMFS, RM_SDMC, RM_SND, RM_SOC, RM_SPI, RM_SPM, RM_SRV, RM_SSL, RM_SWC,
        RM_TCB, RM_TEST, RM_UART, RM_UDS, RM_UPDATER, RM_UTIL, RM_VCTL, RM_WEB_BROWSER,
    };

    Cow::Borrowed(match R_MODULE(result) {
        RM_COMMON => "common",
        RM_KERNEL => "kernel",
        RM_UTIL => "util",
        RM_FILE_SERVER => "file_server",
        RM_LOADER_SERVER => "loader_server",
        RM_TCB => "tcb",
        RM_OS => "os",
        RM_DBG => "dbg",
        RM_DMNT => "dmnt",
        RM_PDN => "pdn",
        RM_GSP => "gsp",
        RM_I2C => "i2c",
        RM_GPIO => "gpio",
        RM_DD => "dd",
        RM_CODEC => "codec",
        RM_SPI => "spi",
        RM_PXI => "pxi",
        RM_FS => "fs",
        RM_DI => "di",
        RM_HID => "hid",
        RM_CAM => "cam",
        RM_PI => "pi",
        RM_PM => "pm",
        RM_PM_LOW => "pm_low",
        RM_FSI => "fsi",
        RM_SRV => "srv",
        RM_NDM => "ndm",
        RM_NWM => "nwm",
        RM_SOC => "soc",
        RM_LDR => "ldr",
        RM_ACC => "acc",
        RM_ROMFS => "romfs",
        RM_AM => "am",
        RM_HIO => "hio",
        RM_UPDATER => "updater",
        RM_MIC => "mic",
        RM_FND => "fnd",
        RM_MP => "mp",
        RM_MPWL => "mpwl",
        RM_AC => "ac",
        RM_HTTP => "http",
        RM_DSP => "dsp",
        RM_SND => "snd",
        RM_DLP => "dlp",
        RM_HIO_LOW => "hio_low",
        RM_CSND => "csnd",
        RM_SSL => "ssl",
        RM_AM_LOW => "am_low",
        RM_NEX => "nex",
        RM_FRIENDS => "friends",
        RM_RDT => "rdt",
        RM_APPLET => "applet",
        RM_NIM => "nim",
        RM_PTM => "ptm",
        RM_MIDI => "midi",
        RM_MC => "mc",
        RM_SWC => "swc",
        RM_FATFS => "fatfs",
        RM_NGC => "ngc",
        RM_CARD => "card",
        RM_CARDNOR => "cardnor",
        RM_SDMC => "sdmc",
        RM_BOSS => "boss",
        RM_DBM => "dbm",
        RM_CONFIG => "config",
        RM_PS => "ps",
        RM_CEC => "cec",
        RM_IR => "ir",
        RM_UDS => "uds",
        RM_PL => "pl",
        RM_CUP => "cup",
        RM_GYROSCOPE => "gyroscope",
        RM_MCU => "mcu",
        RM_NS => "ns",
        RM_NEWS => "news",
        RM_RO => "ro",
        RM_GD => "gd",
        RM_CARD_SPI => "card_spi",
        RM_EC => "ec",
        RM_WEB_BROWSER => "web_browser",
        RM_TEST => "test",
        RM_ENC => "enc",
        RM_PIA => "pia",
        RM_ACT => "act",
        RM_VCTL => "vctl",
        RM_OLV => "olv",
        RM_NEIA => "neia",
        RM_NPNS => "npns",
        RM_AVD => "avd",
        RM_L2B => "l2b",
        RM_MVD => "mvd",
        RM_NFC => "nfc",
        RM_UART => "uart",
        RM_SPM => "spm",
        RM_QTM => "qtm",
        RM_NFP => "nfp",
        RM_APPLICATION => "application",
        RM_INVALIDRESVAL => "invalid_res_val",
        code => return Cow::Owned(format!("(unknown module: {code:#x})")),
    })
}