Skip to main content

nexus_bits/
error.rs

1// nexus-bits/src/error.rs
2
3//! Error types for bit field operations.
4
5use core::fmt;
6
7/// Value exceeds field capacity.
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub struct Overflow<T> {
10    /// The value that was too large.
11    pub value: T,
12    /// Maximum value the field can hold.
13    pub max: T,
14}
15
16impl<T: fmt::Display> fmt::Display for Overflow<T> {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        write!(f, "value {} exceeds max {}", self.value, self.max)
19    }
20}
21
22/// Field overflow during pack with field name context.
23#[derive(Debug, Clone, Copy, PartialEq, Eq)]
24pub struct FieldOverflow<T> {
25    /// Field name that overflowed.
26    pub field: &'static str,
27    /// The overflow details.
28    pub overflow: Overflow<T>,
29}
30
31impl<T: fmt::Display> fmt::Display for FieldOverflow<T> {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        write!(f, "field '{}': {}", self.field, self.overflow)
34    }
35}
36
37/// Unknown discriminant during unpack.
38#[derive(Debug, Clone, Copy, PartialEq, Eq)]
39pub struct UnknownDiscriminant<T> {
40    /// Field or context name.
41    /// Empty string for top-level enum discriminant.
42    pub field: &'static str,
43    /// The discriminant value that wasn't recognized.
44    pub value: T,
45}
46
47impl<T: fmt::Display> fmt::Display for UnknownDiscriminant<T> {
48    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49        if self.field.is_empty() {
50            write!(f, "unknown discriminant: {}", self.value)
51        } else {
52            write!(
53                f,
54                "field '{}': unknown discriminant {}",
55                self.field, self.value
56            )
57        }
58    }
59}