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 #[must_use]
23 pub fn is_negated(&self) -> bool {
24 self.not.is_some()
25 }
26}
27
28impl HasSpan for ConditionalType<'_> {
29 fn span(&self) -> Span {
30 self.subject.span().join(self.otherwise.span())
31 }
32}
33
34impl std::fmt::Display for ConditionalType<'_> {
35 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36 write!(
37 f,
38 "{} {}{} {} ? {} : {}",
39 self.subject,
40 self.is,
41 self.not.as_ref().map(|k| format!(" {k}")).unwrap_or_default(),
42 self.target,
43 self.then,
44 self.otherwise
45 )
46 }
47}