bp3d_os/module/error.rs
1// Copyright (c) 2025, BlockProject 3D
2//
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without modification,
6// are permitted provided that the following conditions are met:
7//
8// * Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13// * Neither the name of BlockProject 3D nor the names of its contributors
14// may be used to endorse or promote products derived from this software
15// without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29//! This module describes possible errors when attempting to load modules.
30
31use bp3d_util::simple_error;
32use std::fmt::{Display, Formatter};
33use std::str::Utf8Error;
34
35#[derive(Debug, Clone, Eq, PartialEq)]
36/// Describes an incompatible RUSTC version when attempting to load Rust based modules.
37pub struct IncompatibleRustc {
38 /// The expected RUSTC version.
39 pub expected: &'static str,
40
41 /// The RUSTC version stored in the module which failed to load.
42 pub actual: String,
43}
44
45impl Display for IncompatibleRustc {
46 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
47 write!(
48 f,
49 "expected version {}, got version {}",
50 self.expected, self.actual
51 )
52 }
53}
54
55#[derive(Debug, Clone, Eq, PartialEq)]
56/// Describes an incompatible dependency (a dependency which may produce ABI issues) when attempting
57/// to load Rust based modules.
58pub struct IncompatibleDependency {
59 /// The name of the dependency which is incompatible.
60 pub name: String,
61
62 /// The version of the dependency imported by the module which failed to load.
63 pub actual_version: String,
64
65 /// The version of the dependency used by this [ModuleLoader](super::ModuleLoader).
66 pub expected_version: String,
67}
68
69impl Display for IncompatibleDependency {
70 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
71 write!(
72 f,
73 "expected version {}, got version {} for dependency '{}'",
74 self.expected_version, self.actual_version, self.name
75 )
76 }
77}
78
79simple_error! {
80 /// Type of error when using modules.
81 pub Error {
82 /// The module was not found (argument: module name).
83 NotFound(String) => "module not found ({})",
84
85 /// An unexpected NULL character was found.
86 Null => "unexpected null character in string",
87
88 /// Missing DEPS metadata key for a Rust based module.
89 MissingDepsForRust => "missing DEPS metadata key for a Rust module",
90
91 /// Missing FEATURES metadata key for a Rust based module.
92 MissingFeaturesForRust => "missing FEATURES metadata key for a Rust module",
93
94 /// Missing RUSTC version key for a Rust based module.
95 MissingVersionForRust => "missing RUSTC metadata key for a Rust module",
96
97 /// Missing NAME key for a module.
98 MissingModuleName => "missing NAME metadata key",
99
100 /// Missing VERSION key for a module.
101 MissingModuleVersion => "missing VERSION metadata key",
102
103 /// Missing bp3d_os_module_<name>_init symbol for a Rust based module.
104 MissingModuleInitForRust => "missing module init function for a Rust module",
105
106 /// The given string was not UTF8.
107 InvalidUtf8(Utf8Error) => "invalid utf8: {}",
108
109 /// The RUSTC version in the module metadata does not match the RUSTC version used to build
110 /// this [ModuleLoader](super::ModuleLoader).
111 RustcVersionMismatch(IncompatibleRustc) => "incompatible rustc version: {}",
112
113 /// Invalid format for the DEPS metadata key.
114 InvalidDepFormat => "invalid dependency format",
115
116 /// Incompatible dependency API found.
117 IncompatibleDep(IncompatibleDependency) => "incompatible dependency: {}",
118
119 /// Unmatched feature-set.
120 IncompatibleFeatureSet(String) => "incompatible feature-set for dependency: {}",
121
122 /// An IO error.
123 Io(std::io::Error) => "io error: {}",
124
125 /// The module does not contain a valid metadata string.
126 MissingMetadata => "missing module metadata",
127
128 /// The metadata stored in the module has an invalid format.
129 InvalidMetadata => "invalid module metadata format"
130 }
131}