]> git.lizzy.rs Git - rust.git/blob - src/patterns.rs
update dependencies and fix compile errors
[rust.git] / src / patterns.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use Indent;
12 use rewrite::{Rewrite, RewriteContext};
13 use utils::{wrap_str, format_mutability, span_after};
14 use lists::{format_item_list, itemize_list};
15 use expr::{rewrite_unary_prefix, rewrite_pair, rewrite_tuple};
16 use types::rewrite_path;
17
18 use syntax::ast::{BindingMode, Pat, Pat_};
19
20 // FIXME(#18): implement pattern formatting.
21 impl Rewrite for Pat {
22     fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
23         match self.node {
24             Pat_::PatBox(ref pat) => rewrite_unary_prefix(context, "box ", &**pat, width, offset),
25             Pat_::PatIdent(binding_mode, ident, None) => {
26                 let (prefix, mutability) = match binding_mode {
27                     BindingMode::BindByRef(mutability) => ("ref ", mutability),
28                     BindingMode::BindByValue(mutability) => ("", mutability),
29                 };
30                 let mut_infix = format_mutability(mutability);
31                 let result = format!("{}{}{}", prefix, mut_infix, ident.node);
32                 wrap_str(result, context.config.max_width, width, offset)
33             }
34             Pat_::PatWild => {
35                 if 1 <= width {
36                     Some("_".to_owned())
37                 } else {
38                     None
39                 }
40             }
41             Pat_::PatQPath(ref q_self, ref path) => {
42                 rewrite_path(context, true, Some(q_self), path, width, offset)
43             }
44             Pat_::PatRange(ref lhs, ref rhs) => {
45                 rewrite_pair(&**lhs, &**rhs, "", "...", "", context, width, offset)
46             }
47             Pat_::PatRegion(ref pat, mutability) => {
48                 let prefix = format!("&{}", format_mutability(mutability));
49                 rewrite_unary_prefix(context, &prefix, &**pat, width, offset)
50             }
51             Pat_::PatTup(ref items) => rewrite_tuple(context, items, self.span, width, offset),
52             Pat_::PatEnum(ref path, Some(ref pat_vec)) => {
53                 let path_str = try_opt!(::types::rewrite_path(context,
54                                                               true,
55                                                               None,
56                                                               path,
57                                                               width,
58                                                               offset));
59
60                 if pat_vec.is_empty() {
61                     Some(path_str)
62                 } else {
63                     // 1 = (
64                     let width = try_opt!(width.checked_sub(path_str.len() + 1));
65                     let offset = offset + path_str.len() + 1;
66                     let items = itemize_list(context.codemap,
67                                              pat_vec.iter(),
68                                              ")",
69                                              |item| item.span.lo,
70                                              |item| item.span.hi,
71                                              |item| item.rewrite(context, width, offset),
72                                              span_after(self.span, "(", context.codemap),
73                                              self.span.hi);
74                     Some(format!("{}({})",
75                                  path_str,
76                                  try_opt!(format_item_list(items, width, offset, context.config))))
77                 }
78             }
79             Pat_::PatLit(ref expr) => expr.rewrite(context, width, offset),
80             // FIXME(#8): format remaining pattern variants.
81             Pat_::PatIdent(_, _, Some(..)) |
82             Pat_::PatEnum(_, None) |
83             Pat_::PatStruct(..) |
84             Pat_::PatVec(..) |
85             Pat_::PatMac(..) => {
86                 wrap_str(context.snippet(self.span),
87                          context.config.max_width,
88                          width,
89                          offset)
90             }
91         }
92     }
93 }