Skip to main content

j2k_core/
rejection.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2
3//! Typed capability and adapter-contract rejection reasons.
4
5use core::fmt;
6
7/// Stable category for a backend request rejected before execution.
8#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
9#[non_exhaustive]
10pub enum CapabilityRejectionKind {
11    /// The requested pixel or sample format is unsupported.
12    UnsupportedFormat,
13    /// Component sampling is unsupported.
14    UnsupportedSampling,
15    /// Sample precision or coded bitplane count is unsupported.
16    UnsupportedBitDepth,
17    /// The requested decode or encode operation is unsupported.
18    UnsupportedOperation,
19    /// A required prepared execution plan is absent or incompatible.
20    MissingPreparedPlan,
21    /// The compressed container or transfer shape is unsupported.
22    UnsupportedContainer,
23    /// Validated dimensions, ranges, or component geometry are inconsistent.
24    GeometryMismatch,
25    /// A bounded backend resource or address range cannot represent the request.
26    ResourceLimit,
27    /// A request belongs to a different device, context, or session.
28    ContextMismatch,
29    /// Checked internal ownership or execution state violated its contract.
30    ContractViolation,
31}
32
33/// Typed internal rejection rendered to stable text at an adapter boundary.
34#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
35pub struct CapabilityRejection {
36    kind: CapabilityRejectionKind,
37    reason: &'static str,
38}
39
40impl CapabilityRejection {
41    const fn new(kind: CapabilityRejectionKind, reason: &'static str) -> Self {
42        Self { kind, reason }
43    }
44
45    /// Reject an unsupported pixel or sample format.
46    #[must_use]
47    pub const fn unsupported_format(reason: &'static str) -> Self {
48        Self::new(CapabilityRejectionKind::UnsupportedFormat, reason)
49    }
50
51    /// Reject unsupported component sampling.
52    #[must_use]
53    pub const fn unsupported_sampling(reason: &'static str) -> Self {
54        Self::new(CapabilityRejectionKind::UnsupportedSampling, reason)
55    }
56
57    /// Reject unsupported sample precision or coded bitplanes.
58    #[must_use]
59    pub const fn unsupported_bit_depth(reason: &'static str) -> Self {
60        Self::new(CapabilityRejectionKind::UnsupportedBitDepth, reason)
61    }
62
63    /// Reject an unsupported operation.
64    #[must_use]
65    pub const fn unsupported_operation(reason: &'static str) -> Self {
66        Self::new(CapabilityRejectionKind::UnsupportedOperation, reason)
67    }
68
69    /// Reject an absent or incompatible prepared plan.
70    #[must_use]
71    pub const fn missing_prepared_plan(reason: &'static str) -> Self {
72        Self::new(CapabilityRejectionKind::MissingPreparedPlan, reason)
73    }
74
75    /// Reject an unsupported compressed container or transfer shape.
76    #[must_use]
77    pub const fn unsupported_container(reason: &'static str) -> Self {
78        Self::new(CapabilityRejectionKind::UnsupportedContainer, reason)
79    }
80
81    /// Reject inconsistent dimensions, ranges, or component geometry.
82    #[must_use]
83    pub const fn geometry_mismatch(reason: &'static str) -> Self {
84        Self::new(CapabilityRejectionKind::GeometryMismatch, reason)
85    }
86
87    /// Reject a request outside a bounded backend resource or address limit.
88    #[must_use]
89    pub const fn resource_limit(reason: &'static str) -> Self {
90        Self::new(CapabilityRejectionKind::ResourceLimit, reason)
91    }
92
93    /// Reject a request bound to a different device, context, or session.
94    #[must_use]
95    pub const fn context_mismatch(reason: &'static str) -> Self {
96        Self::new(CapabilityRejectionKind::ContextMismatch, reason)
97    }
98
99    /// Reject checked internal ownership or execution state.
100    #[must_use]
101    pub const fn contract_violation(reason: &'static str) -> Self {
102        Self::new(CapabilityRejectionKind::ContractViolation, reason)
103    }
104
105    /// Typed rejection category.
106    #[must_use]
107    pub const fn kind(self) -> CapabilityRejectionKind {
108        self.kind
109    }
110
111    /// Stable diagnostic rendered by the public adapter error.
112    #[must_use]
113    pub const fn reason(self) -> &'static str {
114        self.reason
115    }
116}
117
118impl fmt::Display for CapabilityRejection {
119    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
120        formatter.write_str(self.reason)
121    }
122}