Skip to main content

mago_type_syntax/cst/
composite.rs

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