Skip to main content

macroonz_compiler/host/
type_contract.rs

1//! The constant answer this home's one roster settles, the contracts a capture refusal stands under, and what an expansion delivers to be emitted.
2
3use super::types::{CaptureError, Emittable};
4use crate::closure::PartitionCargo;
5use crate::expansion::Expansion;
6use crate::kind::Kind;
7use crate::token::CaptureBound;
8use core::fmt;
9
10impl CaptureError {
11    /// This row's position in the declared roster, written ahead of the material it carries.
12    ///
13    /// Appended and never renumbered: the byte stands inside every related identity derived over a refused capture.
14    #[must_use]
15    pub const fn slot(&self) -> u8 {
16        match self {
17            Self::Unbounded { .. } => 0,
18            Self::Unread { .. } => 1,
19        }
20    }
21}
22
23impl From<CaptureBound> for CaptureError {
24    fn from(bound: CaptureBound) -> Self {
25        Self::Unbounded { bound }
26    }
27}
28
29impl fmt::Display for CaptureError {
30    fn fmt(&self, into: &mut fmt::Formatter<'_>) -> fmt::Result {
31        match self {
32            Self::Unbounded { bound } => write!(into, "{bound}"),
33            Self::Unread { cause, .. } => write!(into, "{cause}"),
34        }
35    }
36}
37
38impl core::error::Error for CaptureError {
39    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
40        match self {
41            Self::Unbounded { bound } => Some(bound),
42            Self::Unread { cause, .. } => Some(cause),
43        }
44    }
45}
46
47impl<K: Kind> Emittable for Expansion<K> {
48    /// One cargo: an expansion carries exactly one declaration-site delivery, the one its own closure proved.
49    fn cargos(&self) -> impl Iterator<Item = &PartitionCargo> {
50        core::iter::once(self.emit())
51    }
52}