mago_type_syntax/ast/
generics.rs1use serde::Serialize;
2
3use mago_span::HasSpan;
4use mago_span::Span;
5use mago_syntax_core::ast::Sequence;
6
7use crate::ast::Type;
8
9#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, PartialOrd, Ord)]
10pub struct GenericParameterEntry<'arena> {
11 pub inner: Type<'arena>,
12 pub comma: Option<Span>,
13}
14
15#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, PartialOrd, Ord)]
16pub struct GenericParameters<'arena> {
17 pub less_than: Span,
18 pub entries: Sequence<'arena, GenericParameterEntry<'arena>>,
19 pub greater_than: Span,
20}
21
22#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, PartialOrd, Ord)]
23pub struct SingleGenericParameter<'arena> {
24 pub less_than: Span,
25 pub entry: &'arena GenericParameterEntry<'arena>,
26 pub greater_than: Span,
27}
28
29impl HasSpan for GenericParameterEntry<'_> {
30 fn span(&self) -> Span {
31 match &self.comma {
32 Some(comma) => self.inner.span().join(*comma),
33 None => self.inner.span(),
34 }
35 }
36}
37
38impl HasSpan for GenericParameters<'_> {
39 fn span(&self) -> Span {
40 self.less_than.join(self.greater_than)
41 }
42}
43
44impl HasSpan for SingleGenericParameter<'_> {
45 fn span(&self) -> Span {
46 self.less_than.join(self.greater_than)
47 }
48}
49
50impl std::fmt::Display for GenericParameterEntry<'_> {
51 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52 write!(f, "{}", self.inner)
53 }
54}
55
56impl std::fmt::Display for SingleGenericParameter<'_> {
57 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58 write!(f, "<{}>", self.entry)
59 }
60}
61
62impl std::fmt::Display for GenericParameters<'_> {
63 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
64 write!(f, "<")?;
65 for (i, entry) in self.entries.iter().enumerate() {
66 if i > 0 {
67 write!(f, ", ")?;
68 }
69
70 write!(f, "{entry}")?;
71 }
72 write!(f, ">")
73 }
74}