Skip to main content

ax_api/
error.rs

1#[cfg(feature = "fs")]
2use ax_fs_ng::VfsError;
3use ax_io::IoError;
4#[cfg(feature = "net")]
5use ax_net::NetError;
6use ax_runtime::RuntimeError;
7
8/// Errors owned by the public ArceOS API facade.
9#[derive(Clone, Copy, Debug, Eq, PartialEq, thiserror::Error)]
10pub enum ApiError {
11    /// A filesystem API failed in the VFS domain.
12    #[cfg(feature = "fs")]
13    #[error(transparent)]
14    Vfs(#[from] VfsError),
15    /// A socket API failed in the network domain.
16    #[cfg(feature = "net")]
17    #[error(transparent)]
18    Net(#[from] NetError),
19    /// A runtime-owned console operation failed.
20    #[error(transparent)]
21    Runtime(#[from] RuntimeError),
22    /// The scheduler rejected a priority update.
23    #[error("failed to update the current task priority")]
24    PriorityUpdateFailed,
25    /// The scheduler rejected an affinity update.
26    #[error("failed to update the current task affinity")]
27    AffinityUpdateFailed,
28}
29
30/// A result returned by the public ArceOS API facade.
31pub type ApiResult<T = ()> = Result<T, ApiError>;
32
33impl From<ApiError> for IoError {
34    fn from(error: ApiError) -> Self {
35        match error {
36            #[cfg(feature = "fs")]
37            ApiError::Vfs(error) => vfs_error_to_io_error(error),
38            #[cfg(feature = "net")]
39            ApiError::Net(error) => error.into(),
40            ApiError::Runtime(error) => runtime_error_to_io_error(error),
41            ApiError::PriorityUpdateFailed | ApiError::AffinityUpdateFailed => Self::BadState,
42        }
43    }
44}
45
46#[cfg(feature = "fs")]
47fn vfs_error_to_io_error(error: VfsError) -> IoError {
48    match error {
49        VfsError::AlreadyExists => IoError::AlreadyExists,
50        VfsError::BadAddress => IoError::BadAddress,
51        VfsError::BadFileDescriptor => IoError::BadFileDescriptor,
52        VfsError::BadState => IoError::BadState,
53        VfsError::CrossesDevices => IoError::CrossesDevices,
54        VfsError::DirectoryNotEmpty => IoError::DirectoryNotEmpty,
55        VfsError::FilesystemLoop => IoError::FilesystemLoop,
56        VfsError::FileTooLarge => IoError::FileTooLarge,
57        VfsError::InvalidData => IoError::InvalidData,
58        VfsError::InvalidInput => IoError::InvalidInput,
59        VfsError::Interrupted => IoError::Interrupted,
60        VfsError::Io => IoError::Io,
61        VfsError::IsADirectory => IoError::IsADirectory,
62        VfsError::NameTooLong => IoError::NameTooLong,
63        VfsError::NoMemory => IoError::NoMemory,
64        VfsError::NoSuchDevice => IoError::NoSuchDevice,
65        VfsError::NoSuchDeviceOrAddress => IoError::NoSuchDeviceOrAddress,
66        VfsError::NotADirectory => IoError::NotADirectory,
67        VfsError::NotATty => IoError::NotATty,
68        VfsError::NotFound => IoError::NotFound,
69        VfsError::OperationNotPermitted => IoError::OperationNotPermitted,
70        VfsError::OperationNotSupported => IoError::OperationNotSupported,
71        VfsError::PermissionDenied => IoError::PermissionDenied,
72        VfsError::ReadOnlyFilesystem => IoError::ReadOnlyFilesystem,
73        VfsError::ResourceBusy => IoError::ResourceBusy,
74        VfsError::StorageFull => IoError::StorageFull,
75        VfsError::TimedOut => IoError::TimedOut,
76        VfsError::Unsupported => IoError::Unsupported,
77        VfsError::WouldBlock => IoError::WouldBlock,
78    }
79}
80
81fn runtime_error_to_io_error(error: RuntimeError) -> IoError {
82    match error {
83        RuntimeError::ConsoleFailedClosed => IoError::BadState,
84        RuntimeError::SerialNotStarted => IoError::BadState,
85        RuntimeError::SerialControlBusy => IoError::ResourceBusy,
86        RuntimeError::WouldBlock => IoError::WouldBlock,
87        RuntimeError::OperationNotSupported => IoError::OperationNotSupported,
88        RuntimeError::InvalidCpu { .. } => IoError::InvalidInput,
89        _ => IoError::Io,
90    }
91}