Next Generation WASM Microkernel Operating System
1// Copyright 2025 Jonas Kruckenberg
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8use core::fmt::{Display, Formatter};
9
10#[derive(Debug)]
11pub enum Error {
12 /// Failed to convert number
13 TryFromInt(core::num::TryFromIntError),
14 /// Failed to parse device tree blob
15 Fdt(k23_fdt::Error),
16 /// Failed to parse kernel elf
17 Elf(&'static str),
18 /// The system was not able to allocate memory needed for the operation.
19 NoMemory,
20 /// Failed to start secondary hart
21 #[cfg(any(target_arch = "riscv64", target_arch = "riscv32"))]
22 FailedToStartSecondaryHart(k23_riscv::sbi::Error),
23 TryFromSlice(core::array::TryFromSliceError),
24}
25impl From<core::num::TryFromIntError> for Error {
26 fn from(err: core::num::TryFromIntError) -> Self {
27 Error::TryFromInt(err)
28 }
29}
30impl From<k23_fdt::Error> for Error {
31 fn from(err: k23_fdt::Error) -> Self {
32 Error::Fdt(err)
33 }
34}
35impl From<core::array::TryFromSliceError> for Error {
36 fn from(err: core::array::TryFromSliceError) -> Self {
37 Error::TryFromSlice(err)
38 }
39}
40
41impl Display for Error {
42 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
43 match self {
44 Error::NoMemory => write!(
45 f,
46 "The system was not able to allocate memory needed for the operation"
47 ),
48 Error::TryFromInt(_) => write!(f, "Failed to convert number"),
49 Error::Fdt(err) => write!(f, "Failed to parse device tree blob: {err}"),
50 Error::Elf(err) => write!(f, "Failed to parse kernel elf: {err}"),
51 #[cfg(any(target_arch = "riscv64", target_arch = "riscv32"))]
52 Error::FailedToStartSecondaryHart(err) => {
53 write!(f, "Failed to start secondary hart: {err}")
54 }
55 Error::TryFromSlice(err) => write!(f, "failed to parse slice: {err}"),
56 }
57 }
58}