Skip to main content

lion_core/
error.rs

1// Copyright (C) 2026 HaiyangLi
2// SPDX-License-Identifier: AGPL-3.0-or-later
3//! Unified error type for lion-core operations.
4
5/// Unified error type for lion-core operations.
6///
7/// Collects all domain error types so callers only need
8/// to handle a single error type.
9#[derive(Debug)]
10pub enum Error {
11    /// Capability operation error (invalid holder, empty rights, etc.)
12    Capability(crate::CapabilityError),
13
14    /// Policy operation error
15    Policy(crate::PolicyError),
16
17    /// Rights operation error
18    Rights(crate::RightsError),
19
20    /// Step execution error
21    Step(crate::StepError),
22
23    /// State operation error
24    State(crate::state::StateError),
25
26    /// Kernel operation error
27    Kernel(crate::state::KernelError),
28}
29
30impl std::fmt::Display for Error {
31    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32        match self {
33            Error::Capability(e) => write!(f, "capability: {e}"),
34            Error::Policy(e) => write!(f, "policy: {e}"),
35            Error::Rights(e) => write!(f, "rights: {e}"),
36            Error::Step(e) => write!(f, "step: {e}"),
37            Error::State(e) => write!(f, "state: {e}"),
38            Error::Kernel(e) => write!(f, "kernel: {e}"),
39        }
40    }
41}
42
43#[cfg(not(extract))]
44impl std::error::Error for Error {
45    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
46        match self {
47            Error::Capability(source) => Some(source),
48            Error::Policy(source) => Some(source),
49            _ => None,
50        }
51    }
52}
53
54impl From<crate::CapabilityError> for Error {
55    fn from(e: crate::CapabilityError) -> Self {
56        Error::Capability(e)
57    }
58}
59
60impl From<crate::PolicyError> for Error {
61    fn from(e: crate::PolicyError) -> Self {
62        Error::Policy(e)
63    }
64}
65
66impl From<crate::RightsError> for Error {
67    fn from(e: crate::RightsError) -> Self {
68        Error::Rights(e)
69    }
70}
71
72impl From<crate::StepError> for Error {
73    fn from(e: crate::StepError) -> Self {
74        Error::Step(e)
75    }
76}
77
78impl From<crate::state::StateError> for Error {
79    fn from(e: crate::state::StateError) -> Self {
80        Error::State(e)
81    }
82}
83
84impl From<crate::state::KernelError> for Error {
85    fn from(e: crate::state::KernelError) -> Self {
86        Error::Kernel(e)
87    }
88}