use ruvix_types::KernelError;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CapError {
InvalidHandle,
Revoked,
InsufficientRights,
RightsEscalation,
DelegationDepthExceeded,
TableFull,
InvalidTarget,
CannotGrant,
CannotRevoke,
StaleHandle,
ObjectMismatch,
InternalError,
}
impl CapError {
#[inline]
#[must_use]
pub const fn as_str(&self) -> &'static str {
match self {
Self::InvalidHandle => "Invalid capability handle",
Self::Revoked => "Capability has been revoked",
Self::InsufficientRights => "Insufficient capability rights",
Self::RightsEscalation => "Cannot grant more rights than held",
Self::DelegationDepthExceeded => "Maximum delegation depth exceeded",
Self::TableFull => "Capability table is full",
Self::InvalidTarget => "Invalid target task",
Self::CannotGrant => "Capability cannot be granted",
Self::CannotRevoke => "Capability cannot be revoked",
Self::StaleHandle => "Stale handle (generation mismatch)",
Self::ObjectMismatch => "Object ID mismatch",
Self::InternalError => "Internal capability manager error",
}
}
#[inline]
#[must_use]
pub const fn to_kernel_error(&self) -> KernelError {
match self {
Self::InvalidHandle => KernelError::InvalidCapability,
Self::Revoked => KernelError::CapabilityRevoked,
Self::InsufficientRights => KernelError::InsufficientRights,
Self::RightsEscalation => KernelError::InsufficientRights,
Self::DelegationDepthExceeded => KernelError::DelegationDepthExceeded,
Self::TableFull => KernelError::LimitExceeded,
Self::InvalidTarget => KernelError::NotFound,
Self::CannotGrant => KernelError::CannotGrant,
Self::CannotRevoke => KernelError::NotPermitted,
Self::StaleHandle => KernelError::StaleHandle,
Self::ObjectMismatch => KernelError::InvalidCapability,
Self::InternalError => KernelError::InternalError,
}
}
}
pub type CapResult<T> = Result<T, CapError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cap_error_to_kernel_error() {
assert_eq!(
CapError::InvalidHandle.to_kernel_error(),
KernelError::InvalidCapability
);
assert_eq!(
CapError::DelegationDepthExceeded.to_kernel_error(),
KernelError::DelegationDepthExceeded
);
}
}