Skip to main content

morphir_projection/
model.rs

1use serde::Serialize;
2
3/// Morphir distribution category retained by the projection model.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
5#[serde(rename_all = "kebab-case")]
6pub enum DistributionKind {
7    /// A Morphir library distribution.
8    Library,
9    /// A specification-only distribution.
10    Specs,
11    /// An application distribution with declared entry points.
12    Application,
13}
14
15/// A body-free package view used by Avro projection.
16#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
17pub struct ProjectionPackage {
18    /// Distribution category.
19    pub kind: DistributionKind,
20    /// Canonical Morphir package name.
21    pub package_name: String,
22    /// Dependency specifications available to projection.
23    pub dependencies: Vec<ProjectionDependency>,
24    /// Public modules owned by this package.
25    pub modules: Vec<ProjectionModule>,
26}
27
28/// A one-level package specification supplied as a distribution dependency.
29#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
30pub struct ProjectionDependency {
31    /// Canonical dependency package name.
32    pub package_name: String,
33    /// Public dependency modules.
34    pub modules: Vec<ProjectionModule>,
35}
36
37/// A public Morphir module after access filtering.
38#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
39pub struct ProjectionModule {
40    /// Canonical module path components.
41    pub path: Vec<String>,
42    /// Public type declarations in canonical order.
43    pub types: Vec<TypeDeclaration>,
44    /// Public value specifications in canonical order.
45    pub values: Vec<ValueSpecification>,
46    /// Optional source documentation.
47    pub doc: Option<String>,
48}
49
50/// A public type declaration or specification.
51#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
52#[serde(tag = "declaration", rename_all = "kebab-case")]
53pub enum TypeDeclaration {
54    /// A transparent type alias.
55    Alias {
56        /// Exact canonical Morphir FQName.
57        source_name: String,
58        /// Canonical local Morphir name.
59        name: String,
60        /// Declared type parameters.
61        type_params: Vec<String>,
62        /// Alias target type.
63        value: TypeExpr,
64        /// Optional source documentation.
65        doc: Option<String>,
66    },
67    /// An opaque type specification without a visible representation.
68    Opaque {
69        /// Exact canonical Morphir FQName.
70        source_name: String,
71        /// Canonical local Morphir name.
72        name: String,
73        /// Declared type parameters.
74        type_params: Vec<String>,
75        /// Optional source documentation.
76        doc: Option<String>,
77    },
78    /// An algebraic custom type.
79    Custom {
80        /// Exact canonical Morphir FQName.
81        source_name: String,
82        /// Canonical local Morphir name.
83        name: String,
84        /// Declared type parameters.
85        type_params: Vec<String>,
86        /// Public constructors.
87        constructors: Vec<Constructor>,
88        /// Optional source documentation.
89        doc: Option<String>,
90    },
91    /// A v4 declaration whose definition is incomplete.
92    Incomplete {
93        /// Exact canonical Morphir FQName.
94        source_name: String,
95        /// Canonical local Morphir name.
96        name: String,
97        /// Declared type parameters.
98        type_params: Vec<String>,
99        /// Kind of incompleteness reported by the IR.
100        incompleteness: IncompletenessKind,
101        /// Partial type information, when available.
102        partial_type: Option<TypeExpr>,
103        /// Optional source documentation.
104        doc: Option<String>,
105    },
106}
107
108impl TypeDeclaration {
109    /// Canonical local Morphir name of this declaration.
110    pub fn name(&self) -> &str {
111        match self {
112            Self::Alias { name, .. }
113            | Self::Opaque { name, .. }
114            | Self::Custom { name, .. }
115            | Self::Incomplete { name, .. } => name,
116        }
117    }
118
119    /// Canonical fully qualified Morphir source name.
120    pub fn source_name(&self) -> &str {
121        match self {
122            Self::Alias { source_name, .. }
123            | Self::Opaque { source_name, .. }
124            | Self::Custom { source_name, .. }
125            | Self::Incomplete { source_name, .. } => source_name,
126        }
127    }
128}
129
130/// The amount of information available for an incomplete v4 type.
131#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
132#[serde(rename_all = "kebab-case")]
133pub enum IncompletenessKind {
134    /// A deliberately unfinished draft.
135    Draft,
136    /// A missing type hole.
137    Hole,
138}
139
140/// A custom type constructor.
141#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
142pub struct Constructor {
143    /// Exact canonical Morphir constructor FQName.
144    pub source_name: String,
145    /// Canonical local constructor name.
146    pub name: String,
147    /// Constructor payload arguments.
148    pub arguments: Vec<NamedType>,
149}
150
151/// A named field, constructor argument, or value input.
152#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
153pub struct NamedType {
154    /// Canonical field or argument name.
155    pub name: String,
156    /// Associated type expression.
157    pub tpe: TypeExpr,
158}
159
160/// Morphir type forms that can contribute to Avro schemas.
161#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
162#[serde(tag = "type", content = "value", rename_all = "kebab-case")]
163pub enum TypeExpr {
164    /// A type parameter reference.
165    Variable(String),
166    /// A fully qualified type reference with concrete arguments.
167    Reference {
168        /// Exact canonical referenced Morphir FQName.
169        source_name: String,
170        /// Applied type arguments.
171        arguments: Vec<TypeExpr>,
172    },
173    /// A positional product type.
174    Tuple(Vec<TypeExpr>),
175    /// A closed structural record.
176    Record(Vec<NamedType>),
177    /// An open structural record.
178    ExtensibleRecord {
179        /// Row variable name.
180        variable: String,
181        /// Known record fields.
182        fields: Vec<NamedType>,
183    },
184    /// A function type stored within another type expression.
185    Function {
186        /// Function input type.
187        input: Box<TypeExpr>,
188        /// Function output type.
189        output: Box<TypeExpr>,
190    },
191    /// The unit type.
192    Unit,
193}
194
195/// A public value signature. It intentionally has no value body field.
196#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
197pub struct ValueSpecification {
198    /// Exact canonical Morphir value FQName.
199    pub source_name: String,
200    /// Canonical local value name.
201    pub name: String,
202    /// Flattened function inputs.
203    pub inputs: Vec<NamedType>,
204    /// Output type, absent for incomplete values.
205    pub output: Option<TypeExpr>,
206    /// Function or constant classification.
207    pub value_kind: ValueKind,
208    /// Declared application entry-point metadata.
209    pub entry_point: Option<EntryPointMetadata>,
210    /// Optional source documentation.
211    pub doc: Option<String>,
212}
213
214/// Whether a value is invoked with arguments or denotes a constant.
215#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
216#[serde(rename_all = "kebab-case")]
217pub enum ValueKind {
218    /// A value accepting one or more inputs.
219    Function,
220    /// A zero-argument value specification.
221    Constant,
222}
223
224/// Morphir v4 application entry-point category.
225#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
226#[serde(rename_all = "kebab-case")]
227pub enum EntryPointKind {
228    /// The primary application entry point.
229    Main,
230    /// A command entry point.
231    Command,
232    /// An event or request handler.
233    Handler,
234    /// A scheduled or batch job.
235    Job,
236    /// A policy evaluation entry point.
237    Policy,
238}
239
240/// Metadata attached to a declared v4 application entry point.
241#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
242pub struct EntryPointMetadata {
243    /// Stable application-level entry-point identifier.
244    pub identifier: String,
245    /// Declared entry-point category.
246    pub kind: EntryPointKind,
247    /// Optional entry-point documentation.
248    pub doc: Option<String>,
249}