mago_type_syntax/ast/
composite.rs1use serde::Serialize;
2
3use mago_span::HasSpan;
4use mago_span::Span;
5
6use crate::ast::Type;
7
8#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, PartialOrd, Ord)]
9#[repr(C)]
10pub struct ParenthesizedType<'input> {
11 pub left_parenthesis: Span,
12 pub inner: Box<Type<'input>>,
13 pub right_parenthesis: Span,
14}
15
16#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, PartialOrd, Ord)]
17#[repr(C)]
18pub struct UnionType<'input> {
19 pub left: Box<Type<'input>>,
20 pub pipe: Span,
21 pub right: Box<Type<'input>>,
22}
23
24#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, PartialOrd, Ord)]
25#[repr(C)]
26pub struct IntersectionType<'input> {
27 pub left: Box<Type<'input>>,
28 pub ampersand: Span,
29 pub right: Box<Type<'input>>,
30}
31
32#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, PartialOrd, Ord)]
33#[repr(C)]
34pub struct NullableType<'input> {
35 pub question_mark: Span,
36 pub inner: Box<Type<'input>>,
37}
38
39impl HasSpan for ParenthesizedType<'_> {
40 fn span(&self) -> Span {
41 self.left_parenthesis.join(self.right_parenthesis)
42 }
43}
44
45impl HasSpan for UnionType<'_> {
46 fn span(&self) -> Span {
47 self.left.span().join(self.right.span())
48 }
49}
50
51impl HasSpan for IntersectionType<'_> {
52 fn span(&self) -> Span {
53 self.left.span().join(self.right.span())
54 }
55}
56
57impl HasSpan for NullableType<'_> {
58 fn span(&self) -> Span {
59 self.question_mark.join(self.inner.span())
60 }
61}
62
63impl std::fmt::Display for ParenthesizedType<'_> {
64 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
65 write!(f, "({})", self.inner)
66 }
67}
68
69impl std::fmt::Display for UnionType<'_> {
70 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
71 write!(f, "{} | {}", self.left, self.right)
72 }
73}
74
75impl std::fmt::Display for IntersectionType<'_> {
76 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77 write!(f, "{} & {}", self.left, self.right)
78 }
79}
80
81impl std::fmt::Display for NullableType<'_> {
82 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83 write!(f, "?{}", self.inner)
84 }
85}