use thiserror::Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
#[repr(u8)]
pub enum DCPError {
#[error("insufficient data for parsing")]
InsufficientData = 1,
#[error("invalid magic number")]
InvalidMagic = 2,
#[error("unknown message type")]
UnknownMessageType = 3,
#[error("tool not found")]
ToolNotFound = 4,
#[error("schema validation failed")]
ValidationFailed = 5,
#[error("hash mismatch")]
HashMismatch = 6,
#[error("signature invalid")]
SignatureInvalid = 7,
#[error("nonce reused")]
NonceReused = 8,
#[error("timestamp expired")]
TimestampExpired = 9,
#[error("checksum mismatch")]
ChecksumMismatch = 10,
#[error("backpressure")]
Backpressure = 11,
#[error("out of bounds")]
OutOfBounds = 12,
#[error("internal error")]
InternalError = 13,
#[error("resource exhausted")]
ResourceExhausted = 14,
#[error("session not found")]
SessionNotFound = 15,
#[error("capability denied")]
CapabilityDenied = 16,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
#[repr(u8)]
pub enum SecurityError {
#[error("invalid signature")]
InvalidSignature = 1,
#[error("expired timestamp")]
ExpiredTimestamp = 2,
#[error("replay attack detected")]
ReplayAttack = 3,
#[error("insufficient capabilities")]
InsufficientCapabilities = 4,
#[error("security state capacity exceeded")]
CapacityExceeded = 5,
#[error("validation failed")]
ValidationFailed = 6,
#[error("argument hash mismatch")]
ArgsHashMismatch = 7,
}
#[repr(C, packed)]
#[derive(Debug, Clone, Copy)]
pub struct ErrorResponse {
pub category: u8,
pub code: u8,
pub context_len: u16,
}
impl ErrorResponse {
pub const SIZE: usize = 4;
pub fn new(category: u8, code: u8) -> Self {
Self {
category,
code,
context_len: 0,
}
}
pub fn from_dcp_error(err: DCPError) -> Self {
Self::new(1, err as u8)
}
pub fn from_security_error(err: SecurityError) -> Self {
Self::new(2, err as u8)
}
}