Skip to content

Commit 2161400

Browse files
committed
Add a try_opt! macro for ease of work with Rewrite
1 parent c012d31 commit 2161400

File tree

2 files changed

+28
-30
lines changed

2 files changed

+28
-30
lines changed

src/expr.rs

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -125,19 +125,16 @@ fn rewrite_string_lit(context: &RewriteContext, s: &str, span: Span, width: usiz
125125
}
126126

127127
fn rewrite_call(context: &RewriteContext,
128-
callee: &ast::Expr,
129-
args: &[ptr::P<ast::Expr>],
130-
width: usize,
131-
offset: usize)
128+
callee: &ast::Expr,
129+
args: &[ptr::P<ast::Expr>],
130+
width: usize,
131+
offset: usize)
132132
-> Option<String>
133133
{
134134
debug!("rewrite_call, width: {}, offset: {}", width, offset);
135135

136136
// TODO using byte lens instead of char lens (and probably all over the place too)
137-
let callee_str = match callee.rewrite(context, width, offset) {
138-
Some(s) => s,
139-
None => { return None; }
140-
};
137+
let callee_str = try_opt!(callee.rewrite(context, width, offset));
141138
debug!("rewrite_call, callee_str: `{:?}`", callee_str);
142139
// 2 is for parens.
143140
let remaining_width = width - callee_str.len() - 2;
@@ -147,10 +144,8 @@ fn rewrite_call(context: &RewriteContext,
147144
let args_str = if arg_count > 0 {
148145
let mut args_rewritten = Vec::with_capacity(args.len());
149146
for arg in args.iter() {
150-
match arg.rewrite(context, remaining_width, offset) {
151-
Some(s) => { args_rewritten.push((s, String::new())); }
152-
None => { return None; }
153-
}
147+
args_rewritten.push((try_opt!(arg.rewrite(context, remaining_width, offset)),
148+
String::new()));
154149
}
155150
let fmt = ListFormatting {
156151
tactic: ListTactic::HorizontalVertical,
@@ -178,11 +173,11 @@ fn rewrite_paren(context: &RewriteContext, subexpr: &ast::Expr, width: usize, of
178173
}
179174

180175
fn rewrite_struct_lit(context: &RewriteContext,
181-
path: &ast::Path,
182-
fields: &[ast::Field],
183-
base: Option<&ast::Expr>,
184-
width: usize,
185-
offset: usize)
176+
path: &ast::Path,
177+
fields: &[ast::Field],
178+
base: Option<&ast::Expr>,
179+
width: usize,
180+
offset: usize)
186181
-> Option<String>
187182
{
188183
debug!("rewrite_struct_lit: width {}, offset {}", width, offset);
@@ -195,17 +190,11 @@ fn rewrite_struct_lit(context: &RewriteContext,
195190

196191
let mut field_strs = Vec::with_capacity(fields.len());
197192
for field in fields.iter() {
198-
match rewrite_field(context, field, budget, indent) {
199-
Some(s) => { field_strs.push(s); }
200-
None => { return None; }
201-
}
193+
field_strs.push(try_opt!(rewrite_field(context, field, budget, indent)));
202194
}
203195
if let Some(expr) = base {
204196
// Another 2 on the width/indent for the ..
205-
field_strs.push(match expr.rewrite(context, budget - 2, indent + 2) {
206-
Some(s) => format!("..{}", s),
207-
None => { return None; }
208-
});
197+
field_strs.push(format!("..{}", try_opt!(expr.rewrite(context, budget - 2, indent + 2))));
209198
}
210199

211200
// FIXME comments
@@ -238,7 +227,10 @@ fn rewrite_field(context: &RewriteContext, field: &ast::Field, width: usize, off
238227
expr.map(|s| format!("{}: {}", name, s))
239228
}
240229

241-
fn rewrite_tuple_lit(context: &RewriteContext, items: &[ptr::P<ast::Expr>], width: usize, offset: usize)
230+
fn rewrite_tuple_lit(context: &RewriteContext,
231+
items: &[ptr::P<ast::Expr>],
232+
width: usize,
233+
offset: usize)
242234
-> Option<String> {
243235
// opening paren
244236
let indent = offset + 1;
@@ -254,10 +246,7 @@ fn rewrite_tuple_lit(context: &RewriteContext, items: &[ptr::P<ast::Expr>], widt
254246
} else {
255247
config!(max_width) - indent - 2
256248
};
257-
match item.rewrite(context, rem_width, indent) {
258-
Some(s) => { item_strs.push(s); }
259-
None => {return None; }
260-
}
249+
item_strs.push(try_opt!(item.rewrite(context, rem_width, indent)));
261250
}
262251
let tactics = if item_strs.iter().any(|s| s.contains('\n')) {
263252
ListTactic::Vertical

src/utils.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,15 @@ macro_rules! impl_enum_decodable {
159159
};
160160
}
161161

162+
// Same as try!, but for Option
163+
#[macro_export]
164+
macro_rules! try_opt {
165+
($expr:expr) => (match $expr {
166+
Some(val) => val,
167+
None => { return None; }
168+
})
169+
}
170+
162171
#[test]
163172
fn power_rounding() {
164173
assert_eq!(0, round_up_to_power_of_two(0));

0 commit comments

Comments
 (0)