ferrunix_core/error.rs
1//! All errors that might happen.
2#![allow(clippy::module_name_repetitions)]
3
4use thiserror::Error;
5
6use crate::cycle_detection::ValidationError;
7use crate::types::BoxErr;
8
9/// Errors happening during resolving of types.
10#[derive(Debug, Error)]
11#[non_exhaustive]
12pub enum ResolveError {
13 /// Some of the required dependencies are missing.
14 #[error("missing dependencies")]
15 DependenciesMissing,
16
17 /// Validation failed. Check the underlying error.
18 #[error("validation failed: {0}")]
19 Validation(#[from] ValidationError),
20
21 /// Type is missing, possibly never registered.
22 #[error("type missing: {typename}")]
23 TypeMissing {
24 /// Name of the type that was attempted to be resolved.
25 typename: &'static str,
26 },
27
28 /// The constructor returned an error.
29 #[error("error from constructor: {0}")]
30 Ctor(BoxErr),
31
32 /// An error in the implementation happened.
33 #[error("implementation error: {0}")]
34 Impl(#[from] ImplErrors),
35}
36
37/// Implementation errors. These errors represent internal errors that are usually
38/// the fault of the people implementing `ferrunix`, not the users.
39#[derive(Debug, Error)]
40#[non_exhaustive]
41pub enum ImplErrors {
42 /// The type returned by the constructor is different from the type that we cast it to.
43 #[error("type mismatch")]
44 TypeMismatch,
45}
46
47impl ResolveError {
48 /// Returns true if the underlying error was produced by a failure in one of the constructors
49 /// registered.
50 pub fn is_ctor_err(&self) -> bool {
51 matches!(self, Self::Ctor(_))
52 }
53
54 /// Returns the underlying error that was produced by the failure in one of the registered
55 /// constructors, or `None` if it's another error.
56 pub fn ctor_err(&self) -> Option<&BoxErr> {
57 match self {
58 Self::Ctor(error) => Some(error),
59 _ => None,
60 }
61 }
62}