mago_type_syntax/ast/
object.rs1use serde::Serialize;
2
3use mago_span::HasSpan;
4use mago_span::Span;
5
6use crate::ast::Keyword;
7use crate::ast::ShapeField;
8
9#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, PartialOrd, Ord)]
10pub struct ObjectType<'input> {
11 pub keyword: Keyword<'input>,
12 pub properties: Option<ObjectProperties<'input>>,
13}
14
15#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, PartialOrd, Ord)]
16pub struct ObjectProperties<'input> {
17 pub left_brace: Span,
18 pub fields: Vec<ShapeField<'input>>,
19 pub ellipsis: Option<Span>,
20 pub right_brace: Span,
21}
22
23impl HasSpan for ObjectType<'_> {
24 fn span(&self) -> Span {
25 match &self.properties {
26 Some(parameters) => self.keyword.span.join(parameters.span()),
27 None => self.keyword.span,
28 }
29 }
30}
31
32impl std::fmt::Display for ObjectType<'_> {
33 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34 if let Some(parameters) = &self.properties {
35 write!(f, "{}{}", self.keyword, parameters)
36 } else {
37 write!(f, "{}", self.keyword)
38 }
39 }
40}
41
42impl HasSpan for ObjectProperties<'_> {
43 fn span(&self) -> Span {
44 self.left_brace.join(self.right_brace)
45 }
46}
47
48impl std::fmt::Display for ObjectProperties<'_> {
49 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50 write!(f, "{{")?;
51 for (i, field) in self.fields.iter().enumerate() {
52 if i > 0 {
53 write!(f, ", {field}")?;
54 } else {
55 write!(f, "{field}")?;
56 }
57 }
58
59 if self.ellipsis.is_some() {
60 if !self.fields.is_empty() {
61 write!(f, ", ")?;
62 }
63
64 write!(f, "...")?;
65 }
66
67 write!(f, "}}")
68 }
69}