1use serde::Serialize;
2
3use super::*;
4
5#[derive(Debug, Clone, Serialize)]
6pub enum Expression {
7 Literal(Literal),
8 Path(ExprPath),
9 Statement(Box<Statement>),
10 Array(Vec<Expression>),
11 ArrayRepeat(Box<Expression>, Box<Expression>),
12 Closure {
13 return_type: Option<TypeExpr>,
14 params: Vec<VarDecl>,
15 body: Box<Expression>,
16 },
17 Fix {
18 initial: Box<Expression>,
19 prefixes: Vec<Prefix>,
20 postfixes: Vec<Postfix>,
21 },
22 Binary {
23 lhs: Box<Expression>,
24 op: String,
25 rhs: Box<Expression>,
26 },
27}
28
29#[derive(Debug, Clone, Serialize)]
30pub enum Literal {
31 String(String),
32 Int(i64),
33 Float(f64),
34 Bool(bool),
35 Tuple(Vec<Expression>),
36}
37
38#[derive(Debug, Clone, Serialize)]
39pub enum Pattern {
40 NamedTuple(Path, Vec<Box<Pattern>>),
41 Struct(Path, Vec<Option<(Identifier, Option<Box<Pattern>>)>>),
42 Tuple(Vec<Box<Pattern>>),
43 Literal(Literal),
44 Path(bool, Path),
45 Etc,
46}
47
48#[derive(Debug, Clone, Serialize)]
49pub enum MacroDelimiter {
50 Paren,
51 Bracket,
52 Brace,
53}
54
55#[derive(Debug, Clone, Serialize)]
56pub enum Postfix {
57 FieldAccess(Identifier, Option<Generics>),
58 TupleFieldAccess(u8, Option<Generics>),
59 Call(Vec<Expression>),
60 MacroCall {
61 inner: String,
62 delimiter: MacroDelimiter,
63 },
64 StructCall(Vec<(Identifier, Option<Expression>)>),
65 Assign(String, Box<Expression>),
66 Index(Expression),
67 As(TypeExpr),
68 Increment,
69 Decrement,
70 Try,
71}
72
73#[derive(Debug, Clone, Serialize)]
74pub enum Prefix {
75 Ref,
76 RefMut,
77 Deref,
78 Not,
79 Neg,
80}
81
82#[derive(Debug, Clone, Serialize, PartialEq, Eq, Hash)]
83pub struct ExprPathSegment {
84 pub ident: Identifier,
85 pub generics: Option<Generics>,
86}
87
88#[derive(Debug, Clone, Serialize, PartialEq, Eq, Hash)]
89pub struct ExprPath(pub Vec<ExprPathSegment>);
90
91#[derive(Debug, Clone, Serialize, PartialEq, Eq, Hash)]
92pub struct Generics(pub Vec<Generic>);
93
94#[derive(Debug, Clone, Serialize, PartialEq, Eq, Hash)]
95pub enum Generic {
96 Lifetime(Identifier),
97 Type(TypeExpr),
98}
99
100impl Expression {
101 pub fn is_block(&self) -> bool {
102 if let Expression::Statement(stmt) = self {
103 stmt.is_block()
104 } else {
105 false
106 }
107 }
108}
109
110impl From<Path> for ExprPath {
111 fn from(path: Path) -> Self {
112 Self(
113 path.0
114 .into_iter()
115 .map(|v| ExprPathSegment {
116 ident: v,
117 generics: None,
118 })
119 .collect(),
120 )
121 }
122}