mago_type_syntax/ast/
conditional.rs1use serde::Serialize;
2
3use mago_span::HasSpan;
4use mago_span::Span;
5
6use crate::ast::Type;
7use crate::ast::keyword::Keyword;
8
9#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, PartialOrd, Ord)]
10pub struct ConditionalType<'input> {
11 pub subject: Box<Type<'input>>,
12 pub is: Keyword<'input>,
13 pub not: Option<Keyword<'input>>,
14 pub target: Box<Type<'input>>,
15 pub question_mark: Span,
16 pub then: Box<Type<'input>>,
17 pub colon: Span,
18 pub otherwise: Box<Type<'input>>,
19}
20
21impl ConditionalType<'_> {
22 pub fn is_negated(&self) -> bool {
23 self.not.is_some()
24 }
25}
26
27impl HasSpan for ConditionalType<'_> {
28 fn span(&self) -> Span {
29 self.subject.span().join(self.otherwise.span())
30 }
31}
32
33impl std::fmt::Display for ConditionalType<'_> {
34 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35 write!(
36 f,
37 "{} {}{} {} ? {} : {}",
38 self.subject,
39 self.is,
40 self.not.as_ref().map(|k| format!(" {k}")).unwrap_or_default(),
41 self.target,
42 self.then,
43 self.otherwise
44 )
45 }
46}