Skip to main content

macroonz_compiler/stamp/
type_contract.rs

1//! The stamp home's declarative surface: the tables this home states rather than computes, and the contracts a refusal stands under.
2//!
3//! The visibility transport is stated once as a total answer over the closed reach roster.
4//! A stamped item sits one module deeper than the coordinate the site wrote its reach at, so a reach copied straight through would publish it to the site's own parent, and a reach widened by guesswork would publish it further than that.
5
6use super::{StampError, TransportedReach, Visibility};
7use crate::bounded::Overflow;
8
9impl Visibility {
10    /// The reach a stamped item carries inside the module a pattern seats it in.
11    ///
12    /// This is the one place the transport is decided, so the front arm a definition renders and the reach the stamped item wears are one answer rather than two that agree until one is edited.
13    /// A constant answer over a closed roster, so a sixth reach admitted later stops the compiler here until somebody says what it becomes one level in.
14    ///
15    /// # Bounds
16    ///
17    /// The two private reaches name the same scope and transport to the same one; the parent-facing reach gains a segment; the crate-facing and public reaches are absolute and the extra module does not move them.
18    #[must_use]
19    pub const fn transported(self) -> TransportedReach {
20        match self {
21            Self::Private | Self::Module => TransportedReach::Enclosing,
22            Self::Parent => TransportedReach::Ancestor,
23            Self::Crate => TransportedReach::Crate,
24            Self::Public => TransportedReach::Public,
25        }
26    }
27}
28
29/// Every overflow a rendering road meets is a tree that outgrew the declared token magnitude.
30///
31/// The nonclaim is stated here: this is the only overflow the roads in this home can raise, because the only bounded collection they build is a token group.
32impl From<Overflow> for StampError {
33    fn from(overflow: Overflow) -> Self {
34        Self::TokensUnbounded { overflow }
35    }
36}
37
38impl core::fmt::Display for StampError {
39    fn fmt(&self, into: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
40        match self {
41            Self::NotAnIdentifier => write!(
42                into,
43                "a spelling this stamp writes as a token is not one Rust identifier"
44            ),
45            Self::PathEmpty => write!(into, "a spelled path names no segment, so it names nothing"),
46            Self::PathUnbounded { overflow } => write!(
47                into,
48                "a spelled path carries {} segments where at most {} fit",
49                overflow.offered, overflow.capacity
50            ),
51            Self::PatternEmpty => {
52                write!(into, "a pattern declares no part, so it declares no shape")
53            }
54            Self::PatternUnbounded { overflow } => write!(
55                into,
56                "a pattern declares {} parts where at most {} fit",
57                overflow.offered, overflow.capacity
58            ),
59            Self::SeatNameDoubled { at } => write!(
60                into,
61                "the seat at part {at} carries a name an earlier seat already binds"
62            ),
63            Self::SitesAbsent => write!(
64                into,
65                "a stamp covers no site, and a definition nobody invokes has no reader"
66            ),
67            Self::SitesUnbounded { overflow } => write!(
68                into,
69                "a stamp covers {} sites where at most {} fit",
70                overflow.offered, overflow.capacity
71            ),
72            Self::SiteNameDoubled { at } => write!(
73                into,
74                "the site at position {at} carries a name an earlier site already has"
75            ),
76            Self::ArgumentsUnbounded { overflow } => write!(
77                into,
78                "a site carries {} arguments where at most {} fit",
79                overflow.offered, overflow.capacity
80            ),
81            Self::ArgumentsUnmatched {
82                at,
83                seats,
84                supplied,
85            } => write!(
86                into,
87                "the site at position {at} supplies {supplied} arguments for {seats} declared seats"
88            ),
89            Self::ReachUnseated { at } => write!(
90                into,
91                "the site at position {at} declares a reach its pattern gives no coordinate to"
92            ),
93            Self::SeatNotPlanned { role_slot } => write!(
94                into,
95                "the plan declares no member under the seat at roster position {role_slot}"
96            ),
97            Self::DestinationNotArtifact { role_slot } => write!(
98                into,
99                "the member at roster position {role_slot} lands somewhere other than an artifact"
100            ),
101            Self::TokensUnbounded { overflow } => write!(
102                into,
103                "a rendered tree carries {} tokens where at most {} fit",
104                overflow.offered, overflow.capacity
105            ),
106        }
107    }
108}
109
110impl core::error::Error for StampError {
111    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
112        match self {
113            Self::PathUnbounded { overflow }
114            | Self::PatternUnbounded { overflow }
115            | Self::SitesUnbounded { overflow }
116            | Self::ArgumentsUnbounded { overflow }
117            | Self::TokensUnbounded { overflow } => Some(overflow),
118            Self::NotAnIdentifier
119            | Self::PathEmpty
120            | Self::PatternEmpty
121            | Self::SeatNameDoubled { .. }
122            | Self::SitesAbsent
123            | Self::SiteNameDoubled { .. }
124            | Self::ArgumentsUnmatched { .. }
125            | Self::ReachUnseated { .. }
126            | Self::SeatNotPlanned { .. }
127            | Self::DestinationNotArtifact { .. } => None,
128        }
129    }
130}