mago_type_syntax/ast/
generics.rs

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