fn syn_brackets(syn_expr: syn::Expr) -> syn::Expr {
    use syn::*;
    use syn::fold::*;

    fn paren(folder: &mut BracketsFolder, node: ExprKind) -> ExprKind {
        ExprKind::Paren(ExprParen {
            expr: Box::new(fold_expr(folder, Expr {
                node: node,
                attrs: vec![],
            })),
            paren_token: tokens::Paren::default(),
        })
    }

    struct BracketsFolder;
    impl Folder for BracketsFolder {
        fn fold_expr(&mut self, expr: Expr) -> Expr {
            let kind = match expr.node {
                ExprKind::Group(_) => unreachable!(),
                ExprKind::Paren(p) => paren(self, p.expr.node),
                ExprKind::If(..) |
                ExprKind::Block(..) |
                ExprKind::IfLet(..) => {
                    return fold_expr(self, expr);
                }
                node => paren(self, node),
            };

            Expr {
                node: kind,
                ..expr
            }
        }

        fn fold_stmt(&mut self, stmt: Stmt) -> Stmt {
            match stmt {
                // Don't wrap toplevel expressions in statements.
                Stmt::Expr(e) => {
                    Stmt::Expr(Box::new(fold_expr(self, *e)))
                }
                Stmt::Semi(e, semi) => {
                    Stmt::Semi(Box::new(fold_expr(self, *e)), semi)
                }
                s => s,
            }
        }

        // We don't want to look at expressions that might appear in patterns or
        // types yet. We'll look into comparing those in the future. For now
        // focus on expressions appearing in other places.
        fn fold_pat(&mut self, pat: Pat) -> Pat {
            pat
        }

        fn fold_ty(&mut self, ty: Ty) -> Ty {
            ty
        }
    }

    let mut folder = BracketsFolder;
    folder.fold_expr(syn_expr)
}
/// Wrap every expression which is not already wrapped in parens with parens, to
/// reveal the precidence of the parsed expressions, and produce a stringified form
/// of the resulting expression.
Wrap every expression which is not already wrapped in parens with parens, to
reveal the precidence of the parsed expressions, and produce a stringified form
of the resulting expression.
Wrap every expression which is not already wrapped in parens with parens to reveal the precidence of the parsed expressions and produce a stringified form of the resulting expression .
fn syn_brackets(syn_expr: syn::Expr) -> syn::Expr {
    use syn::*;
    use syn::fold::*;
    fn paren(folder: &mut BracketsFolder, node: ExprKind) -> ExprKind {
        ExprKind::Paren(ExprParen {
            expr: Box::new(fold_expr(folder, Expr {
                node: node,
                attrs: vec![],
            })),
            paren_token: tokens::Paren::default(),
        })
    }
    struct BracketsFolder;
    impl Folder for BracketsFolder {
        fn fold_expr(&mut self, expr: Expr) -> Expr {
            let kind = match expr.node {
                ExprKind::Group(_) => unreachable!(),
                ExprKind::Paren(p) => paren(self, p.expr.node),
                ExprKind::If(..) |
                ExprKind::Block(..) |
                ExprKind::IfLet(..) => {
                    return fold_expr(self, expr);
                }
                node => paren(self, node),
            };
            Expr {
                node: kind,
                ..expr
            }
        }
        fn fold_stmt(&mut self, stmt: Stmt) -> Stmt {
            match stmt {
                // Don't wrap toplevel expressions in statements.
                Stmt::Expr(e) => {
                    Stmt::Expr(Box::new(fold_expr(self, *e)))
                }
                Stmt::Semi(e, semi) => {
                    Stmt::Semi(Box::new(fold_expr(self, *e)), semi)
                }
                s => s,
            }
        }
        // We don't want to look at expressions that might appear in patterns or
        // types yet. We'll look into comparing those in the future. For now
        // focus on expressions appearing in other places.
        fn fold_pat(&mut self, pat: Pat) -> Pat {
            pat
        }
        fn fold_ty(&mut self, ty: Ty) -> Ty {
            ty
        }
    }
    let mut folder = BracketsFolder;
    folder.fold_expr(syn_expr)
}
fn syn_brackets ( syn_expr : syn :: Expr ) -> syn :: Expr { use syn :: * ; use syn :: fold :: * ; fn paren ( folder : & mut BracketsFolder , node : ExprKind ) -> ExprKind { ExprKind :: Paren ( ExprParen { expr : Box :: new ( fold_expr ( folder , Expr { node : node , attrs : vec ! [ ] , } ) ) , paren_token : tokens :: Paren :: default ( ) , } ) } struct BracketsFolder ; impl Folder for BracketsFolder { fn fold_expr ( & mut self , expr : Expr ) -> Expr { let kind = match expr . node { ExprKind :: Group ( _ ) => unreachable ! ( ) , ExprKind :: Paren ( p ) => paren ( self , p . expr . node ) , ExprKind :: If ( .. ) | ExprKind :: Block ( .. ) | ExprKind :: IfLet ( .. ) => { return fold_expr ( self , expr ) ; } node => paren ( self , node ) , } ; Expr { node : kind , .. expr } } fn fold_stmt ( & mut self , stmt : Stmt ) -> Stmt { match stmt { // Don't wrap toplevel expressions in statements. Stmt :: Expr ( e ) => { Stmt :: Expr ( Box :: new ( fold_expr ( self , * e ) ) ) } Stmt :: Semi ( e , semi ) => { Stmt :: Semi ( Box :: new ( fold_expr ( self , * e ) ) , semi ) } s => s , } } // We don't want to look at expressions that might appear in patterns or // types yet. We'll look into comparing those in the future. For now // focus on expressions appearing in other places. fn fold_pat ( & mut self , pat : Pat ) -> Pat { pat } fn fold_ty ( & mut self , ty : Ty ) -> Ty { ty } } let mut folder = BracketsFolder ; folder . fold_expr ( syn_expr ) }
Wrap every expression which is not already wrapped in parens with parens, to
reveal the precidence of the parsed expressions, and produce a stringified form
of the resulting expression.
Wrap every expression which is not already wrapped in parens with parens to reveal the precidence of the parsed expressions and produce a stringified form of the resulting expression .
syn_expr
syn::Expr
syn_expr
syn::Expr