1pub mod array;
5pub mod async_block;
6pub mod await_;
7pub mod break_;
8pub mod call;
9pub mod closure;
10pub mod continue_;
11pub mod field;
12pub mod grouped;
13pub mod index;
14pub mod macro_invocation;
15pub mod operator;
16pub mod path;
17pub mod range;
18pub mod return_;
19pub mod struct_;
20pub mod tuple;
21pub mod tuple_indexing;
22pub mod underscore;
23
24pub use self::{
26 array::ArrayExpression,
27 async_block::AsyncBlockExpression,
28 await_::AwaitExpression,
29 break_::BreakExpression,
30 call::{CallExpression, MethodCallExpression},
31 closure::ClosureExpression,
32 continue_::ContinueExpression,
33 field::FieldExpression,
34 grouped::GroupedExpression,
35 index::IndexExpression,
36 macro_invocation::MacroInvocation,
37 operator::OperatorExpression,
38 path::PathExpression,
39 range::RangeExpression,
40 return_::ReturnExpression,
41 struct_::StructExpression,
42 tuple::TupleExpression,
43 tuple_indexing::TupleIndexingExpression,
44 underscore::UnderscoreExpression,
45};
46
47use {
49 crate::{attr::{self, OuterAttrOrDocComment, WithOuterAttributes}, token},
50 super::{Expression, ExpressionInner},
51 rustidy_ast_literal::{IntegerLiteral, LiteralExpression},
52 rustidy_format::{Format, Formattable, WhitespaceFormat},
53 rustidy_parse::{Parse, ParseRecursive, Parser, ParserError, ParserTag, RecursiveWrapper},
54 rustidy_print::Print,
55 rustidy_util::Whitespace,
56};
57
58#[derive(PartialEq, Eq, Clone, Debug)]
60#[derive(derive_more::From)]
61#[derive(serde::Serialize, serde::Deserialize)]
62#[derive(ParseRecursive, Formattable, Format, Print)]
63#[parse_recursive(root = ExpressionInner)]
64#[parse_recursive(transparent)]
65#[parse_recursive(into_root = ExpressionInner)]
66pub struct ExpressionWithoutBlock(
67 #[format(args = attr::with::fmt(Whitespace::INDENT))]
69 pub WithOuterAttributes<ExpressionWithoutBlockInner>,
70);
71
72impl From<ExpressionWithoutBlockInner> for ExpressionWithoutBlock {
73 fn from(expr: ExpressionWithoutBlockInner) -> Self {
74 Self(WithOuterAttributes::without_attributes(expr))
75 }
76}
77
78impl TryFrom<ExpressionWithoutBlock> for ExpressionWithoutBlockInner {
79 type Error = ();
80
81 fn try_from(expr: ExpressionWithoutBlock) -> Result<Self, Self::Error> {
82 match expr.0.attrs.is_empty() {
83 true => Ok(expr.0.inner),
84 false => Err(()),
85 }
86 }
87}
88
89impl Parse for ExpressionWithoutBlock {
90 type Error = ExpressionWithoutBlockError;
91
92 #[coverage(on)]
93 fn parse_from(parser: &mut Parser) -> Result<Self, Self::Error> {
94 if parser
95 .has_tag(ParserTag::SkipExpressionWithoutBlock) {
96 return Err(ExpressionWithoutBlockError::Tag);
97 }
98
99 let attrs = parser.parse::<Vec<OuterAttrOrDocComment>>()?;
100 let RecursiveWrapper(mut expr, _) = parser
101 .parse::<RecursiveWrapper<Self, ExpressionInner>>()?;
102 expr.0.attrs.extend(attrs);
103
104 Ok(expr)
105 }
106}
107#[derive(derive_more::Debug, derive_more::From, rustidy_parse::ParseError)]
108pub enum ExpressionWithoutBlockError {
109 #[parse_error(transparent)]
110 Attributes(ParserError<Vec<OuterAttrOrDocComment>>),
111
112 #[parse_error(transparent)]
113 From(ParserError<RecursiveWrapper<ExpressionWithoutBlock, ExpressionInner>>),
114
115 #[parse_error(fmt("Tag `{:?}` was present", ParserTag::SkipExpressionWithoutBlock))]
116 #[debug("Tag({:?})", ParserTag::SkipExpressionWithoutBlock)]
117 Tag,
118}
119
120#[derive(PartialEq, Eq, Clone, Debug)]
121#[derive(derive_more::From, derive_more::TryInto)]
122#[derive(strum::EnumTryAs)]
123#[derive(serde::Serialize, serde::Deserialize)]
124#[derive(ParseRecursive, Formattable, Format, Print)]
125#[parse_recursive(root = ExpressionInner)]
126#[parse_recursive(into_root = ExpressionWithoutBlock)]
127pub enum ExpressionWithoutBlockInner {
128 DoYeet(DoYeetExpression),
129
130 Literal(LiteralExpression),
131 #[parse_recursive(recursive)]
132 Operator(OperatorExpression),
133 Grouped(GroupedExpression),
134 #[parse_recursive(recursive)]
135 Index(IndexExpression),
136 #[parse_recursive(recursive)]
137 Range(RangeExpression),
138
139 MacroInvocation(MacroInvocation),
140
141 #[parse_recursive(recursive)]
142 MethodCall(MethodCallExpression),
143 #[parse_recursive(recursive)]
144 Call(CallExpression),
145 #[parse_recursive(recursive)]
146 Field(FieldExpression),
147 #[parse_recursive(recursive)]
148 TupleIndexing(TupleIndexingExpression),
149 #[parse_recursive(recursive)]
150 Await(AwaitExpression),
151 Tuple(TupleExpression),
152 Return(ReturnExpression),
153 #[parse_recursive(recursive)]
154 Closure(ClosureExpression),
155 Struct(StructExpression),
156 Array(ArrayExpression),
157 Path(PathExpression),
158 Underscore(UnderscoreExpression),
159 Continue(ContinueExpression),
160 Break(BreakExpression),
161 AsyncBlock(AsyncBlockExpression),
162}
163
164#[derive(PartialEq, Eq, Clone, Debug)]
166#[derive(serde::Serialize, serde::Deserialize)]
167#[derive(Parse, Formattable, Format, Print)]
168pub struct DoYeetExpression {
169 pub do_: token::Do,
170 #[format(prefix_ws = Whitespace::SINGLE)]
171 pub yeet_: token::Yeet,
172 #[format(prefix_ws = Whitespace::SINGLE)]
174 pub expr: Option<Expression>,
175}
176
177#[derive(PartialEq, Eq, Clone, Debug)]
179#[derive(serde::Serialize, serde::Deserialize)]
180#[derive(Parse, Formattable, Format, Print)]
181pub struct TupleIndex(pub IntegerLiteral);