Skip to main content

macroonz_compiler/recipe/
type_contract.rs

1//! Recipe kind, role, lowering, refusal, and host-emission contracts.
2
3#[cfg(feature = "host")]
4use super::RecipeBake;
5use super::types::{RecipeShell, RecipeShellContent};
6use super::{ProjectionError, Recipe, RecipeProjection, RecipeRole};
7use crate::bounded::Overflow;
8use crate::kind::{Destination, Kind, NoQuestions, Role, SoleRole};
9use crate::render::RenderError;
10use core::fmt;
11
12impl Role for RecipeRole {
13    const ALL: &'static [Self] = Self::ALL;
14
15    fn name(self) -> &'static str {
16        Self::name(self)
17    }
18
19    fn destination(self) -> Destination {
20        self.profile().output.destination
21    }
22}
23
24impl Kind for RecipeProjection {
25    const NAME: &'static str = "recipe-projection";
26    type Content = Recipe;
27    type Role = RecipeRole;
28    type Question = NoQuestions;
29}
30
31impl Kind for RecipeShell {
32    const NAME: &'static str = "recipe-emission";
33    type Content = RecipeShellContent;
34    type Role = SoleRole;
35    type Question = NoQuestions;
36}
37
38impl fmt::Display for ProjectionError {
39    fn fmt(&self, into: &mut fmt::Formatter<'_>) -> fmt::Result {
40        match self {
41            Self::Tokens(overflow) => write!(into, "{overflow}"),
42            Self::Render(refusal) => write!(into, "{refusal}"),
43        }
44    }
45}
46
47impl core::error::Error for ProjectionError {
48    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
49        match self {
50            Self::Tokens(overflow) => Some(overflow),
51            Self::Render(refusal) => Some(refusal),
52        }
53    }
54}
55
56impl From<Overflow> for ProjectionError {
57    fn from(overflow: Overflow) -> Self {
58        Self::Tokens(overflow)
59    }
60}
61
62impl From<ProjectionError> for RenderError {
63    fn from(refusal: ProjectionError) -> Self {
64        match refusal {
65            ProjectionError::Tokens(overflow) => Self::TokensUnbounded {
66                bound: overflow.capacity,
67                observed: overflow.offered,
68            },
69            ProjectionError::Render(render) => render,
70        }
71    }
72}
73
74#[cfg(feature = "host")]
75impl crate::host::Emittable for RecipeBake {
76    fn cargos(&self) -> impl Iterator<Item = &crate::closure::PartitionCargo> {
77        core::iter::once(self.emit())
78    }
79}