ove/error.rs
1// Copyright (C) 2026 Kamil Lulko <kamil.lulko@gmail.com>
2//
3// SPDX-License-Identifier: GPL-3.0-or-later
4//
5// This file is part of oveRTOS.
6
7//! oveRTOS error types and the [`Result`] alias.
8
9/// oveRTOS error codes, mapped from C `OVE_ERR_*` defines.
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub enum Error {
12 /// The requested resource or subsystem has not been registered (`OVE_ERR_NOT_REGISTERED`).
13 NotRegistered,
14 /// One or more parameters passed to the API were invalid (`OVE_ERR_INVALID_PARAM`).
15 InvalidParam,
16 /// Heap or static allocation failed (`OVE_ERR_NO_MEMORY`).
17 NoMemory,
18 /// The operation did not complete within the allowed time (`OVE_ERR_TIMEOUT`).
19 Timeout,
20 /// The requested operation is not supported by this platform (`OVE_ERR_NOT_SUPPORTED`).
21 NotSupported,
22 /// The queue was full and the item could not be enqueued (`OVE_ERR_QUEUE_FULL`).
23 QueueFull,
24 /// ML inference or model loading failed (`OVE_ERR_ML_FAILED`).
25 MlFailed,
26 /// Remote peer refused the connection (`OVE_ERR_NET_REFUSED`).
27 NetRefused,
28 /// Network or host is unreachable (`OVE_ERR_NET_UNREACHABLE`).
29 NetUnreachable,
30 /// Local address already in use (`OVE_ERR_NET_ADDR_IN_USE`).
31 NetAddrInUse,
32 /// Connection was reset by the remote peer (`OVE_ERR_NET_RESET`).
33 NetReset,
34 /// DNS name resolution failed (`OVE_ERR_NET_DNS_FAIL`).
35 NetDnsFail,
36 /// Connection closed by the remote peer (`OVE_ERR_NET_CLOSED`).
37 NetClosed,
38 /// An error code not covered by the above variants; the raw code is preserved.
39 Unknown(i32),
40}
41
42/// Convenience alias for `core::result::Result<T, Error>`.
43pub type Result<T> = core::result::Result<T, Error>;
44
45/// Timeout value meaning "wait forever".
46pub const WAIT_FOREVER: u32 = u32::MAX;
47
48impl Error {
49 /// Convert a C return code to `Result<()>`.
50 /// Zero (OVE_OK) maps to `Ok(())`, negative values map to the
51 /// corresponding `Error` variant.
52 pub fn from_code(code: i32) -> Result<()> {
53 match code {
54 0 => Ok(()),
55 -1 => Err(Error::NotRegistered),
56 -2 => Err(Error::InvalidParam),
57 -3 => Err(Error::NoMemory),
58 -4 => Err(Error::Timeout),
59 -5 => Err(Error::NotSupported),
60 -6 => Err(Error::QueueFull),
61 -7 => Err(Error::MlFailed),
62 -8 => Err(Error::NetRefused),
63 -9 => Err(Error::NetUnreachable),
64 -10 => Err(Error::NetAddrInUse),
65 -11 => Err(Error::NetReset),
66 -12 => Err(Error::NetDnsFail),
67 -13 => Err(Error::NetClosed),
68 other => Err(Error::Unknown(other)),
69 }
70 }
71}
72
73impl core::fmt::Display for Error {
74 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
75 match self {
76 Error::NotRegistered => write!(f, "not registered"),
77 Error::InvalidParam => write!(f, "invalid parameter"),
78 Error::NoMemory => write!(f, "out of memory"),
79 Error::Timeout => write!(f, "timeout"),
80 Error::NotSupported => write!(f, "not supported"),
81 Error::QueueFull => write!(f, "queue full"),
82 Error::MlFailed => write!(f, "ML inference failed"),
83 Error::NetRefused => write!(f, "connection refused"),
84 Error::NetUnreachable => write!(f, "network unreachable"),
85 Error::NetAddrInUse => write!(f, "address in use"),
86 Error::NetReset => write!(f, "connection reset"),
87 Error::NetDnsFail => write!(f, "DNS resolution failed"),
88 Error::NetClosed => write!(f, "connection closed"),
89 Error::Unknown(c) => write!(f, "unknown error ({})", c),
90 }
91 }
92}