Skip to main content

macroonz_compiler/bounded/
type_contract.rs

1//! The bounded home's trait surface: how each refusal reads, and how the two of them join.
2//!
3//! A construction refusal here is an ordinary error and nothing more.
4
5use super::{Empty, 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}