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