Skip to main content

macroonz_compiler/bounded/
type_contract.rs

1//! The bounded home's trait surface: how each refusal reads and preserves its concrete cause.
2//!
3//! A construction refusal here is an ordinary error and nothing more.
4
5use super::{Empty, KeyedRosterAssignmentError, KeyedRosterError, NonEmptyError, Overflow};
6use core::error::Error;
7use core::fmt::{self, Display, Formatter};
8
9impl Display for Overflow {
10    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
11        write!(
12            formatter,
13            "{} items offered where at most {} fit",
14            self.offered, self.capacity
15        )
16    }
17}
18
19impl Error for Overflow {}
20
21impl Display for Empty {
22    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
23        formatter.write_str("no item offered where at least one is required")
24    }
25}
26
27impl Error for Empty {}
28
29impl Display for NonEmptyError {
30    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
31        match self {
32            Self::Empty(empty) => Display::fmt(empty, formatter),
33            Self::Overflow(overflow) => Display::fmt(overflow, formatter),
34        }
35    }
36}
37
38impl Error for NonEmptyError {
39    fn source(&self) -> Option<&(dyn Error + 'static)> {
40        match self {
41            Self::Empty(empty) => Some(empty),
42            Self::Overflow(overflow) => Some(overflow),
43        }
44    }
45}
46
47impl From<Empty> for NonEmptyError {
48    fn from(empty: Empty) -> Self {
49        Self::Empty(empty)
50    }
51}
52
53impl From<Overflow> for NonEmptyError {
54    fn from(overflow: Overflow) -> Self {
55        Self::Overflow(overflow)
56    }
57}
58
59impl<K, const N: usize> Display for KeyedRosterError<K, N> {
60    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
61        match self {
62            Self::Empty(empty) => Display::fmt(empty, formatter),
63            Self::Overflow(overflow) => Display::fmt(overflow, formatter),
64            Self::DuplicateKeys(duplicates) if duplicates.count() == 1 => {
65                formatter.write_str("one caller-declared key occurred more than once")
66            }
67            Self::DuplicateKeys(duplicates) => write!(
68                formatter,
69                "{} caller-declared keys occurred more than once",
70                duplicates.count()
71            ),
72        }
73    }
74}
75
76impl<K: fmt::Debug, const N: usize> Error for KeyedRosterError<K, N> {
77    fn source(&self) -> Option<&(dyn Error + 'static)> {
78        match self {
79            Self::Empty(empty) => Some(empty),
80            Self::Overflow(overflow) => Some(overflow),
81            Self::DuplicateKeys(_) => None,
82        }
83    }
84}
85
86impl<K, S, const N: usize> Display for KeyedRosterAssignmentError<K, S, N> {
87    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
88        match self {
89            Self::Empty(empty) => Display::fmt(empty, formatter),
90            Self::Overflow(overflow) => Display::fmt(overflow, formatter),
91            Self::ForeignReferences(foreign) if foreign.count() == 1 => {
92                formatter.write_str("one offered payload references a key outside the denominator")
93            }
94            Self::ForeignReferences(foreign) => write!(
95                formatter,
96                "{} offered payloads reference keys outside the denominator",
97                foreign.count()
98            ),
99            Self::DuplicateReferences(duplicates) if duplicates.count() == 1 => formatter
100                .write_str("one denominator key is referenced by more than one offered payload"),
101            Self::DuplicateReferences(duplicates) => write!(
102                formatter,
103                "{} denominator keys are referenced by more than one offered payload",
104                duplicates.count()
105            ),
106            Self::ReusedPayloadSeats(duplicates) if duplicates.count() == 1 => {
107                formatter.write_str("one caller-declared payload-seat key is used more than once")
108            }
109            Self::ReusedPayloadSeats(duplicates) => write!(
110                formatter,
111                "{} caller-declared payload-seat keys are used more than once",
112                duplicates.count()
113            ),
114            Self::MissingMembers(missing) if missing.count() == 1 => {
115                formatter.write_str("one denominator member has no offered payload")
116            }
117            Self::MissingMembers(missing) => write!(
118                formatter,
119                "{} denominator members have no offered payload",
120                missing.count()
121            ),
122        }
123    }
124}
125
126impl<K: fmt::Debug, S: fmt::Debug, const N: usize> Error for KeyedRosterAssignmentError<K, S, N> {
127    fn source(&self) -> Option<&(dyn Error + 'static)> {
128        match self {
129            Self::Empty(empty) => Some(empty),
130            Self::Overflow(overflow) => Some(overflow),
131            Self::ForeignReferences(_)
132            | Self::DuplicateReferences(_)
133            | Self::ReusedPayloadSeats(_)
134            | Self::MissingMembers(_) => None,
135        }
136    }
137}