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