Implement field shorthands in struct literal expressions
Fixes #57.
diff --git a/src/expr.rs b/src/expr.rs
index b1ff25b..8cd586b 100644
--- a/src/expr.rs
+++ b/src/expr.rs
@@ -149,6 +149,7 @@
pub struct FieldValue {
pub ident: Ident,
pub expr: Expr,
+ pub is_shorthand: bool,
}
/// A Block (`{ .. }`).
@@ -746,13 +747,22 @@
(ExprKind::Struct(path, fields, base.map(Box::new)))
));
- named!(field_value -> FieldValue, do_parse!(
- name: wordlike >>
- punct!(":") >>
- value: expr >>
- (FieldValue {
- ident: name,
- expr: value,
+ named!(field_value -> FieldValue, alt!(
+ do_parse!(
+ name: wordlike >>
+ punct!(":") >>
+ value: expr >>
+ (FieldValue {
+ ident: name,
+ expr: value,
+ is_shorthand: false,
+ })
+ )
+ |
+ map!(ident, |name: Ident| FieldValue {
+ ident: name.clone(),
+ expr: ExprKind::Path(None, name.into()).into(),
+ is_shorthand: true,
})
));
@@ -1422,8 +1432,10 @@
impl ToTokens for FieldValue {
fn to_tokens(&self, tokens: &mut Tokens) {
self.ident.to_tokens(tokens);
- tokens.append(":");
- self.expr.to_tokens(tokens);
+ if !self.is_shorthand {
+ tokens.append(":");
+ self.expr.to_tokens(tokens);
+ }
}
}