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