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