prettyplease/
ty.rs

1use crate::algorithm::Printer;
2use crate::fixup::FixupContext;
3use crate::iter::IterDelimited;
4use crate::path::PathKind;
5use crate::INDENT;
6use proc_macro2::TokenStream;
7use syn::{
8    Abi, BareFnArg, BareVariadic, ReturnType, Type, TypeArray, TypeBareFn, TypeGroup,
9    TypeImplTrait, TypeInfer, TypeMacro, TypeNever, TypeParen, TypePath, TypePtr, TypeReference,
10    TypeSlice, TypeTraitObject, TypeTuple,
11};
12
13impl Printer {
14    pub fn ty(&mut self, ty: &Type) {
15        match ty {
16            #![cfg_attr(all(test, exhaustive), deny(non_exhaustive_omitted_patterns))]
17            Type::Array(ty) => self.type_array(ty),
18            Type::BareFn(ty) => self.type_bare_fn(ty),
19            Type::Group(ty) => self.type_group(ty),
20            Type::ImplTrait(ty) => self.type_impl_trait(ty),
21            Type::Infer(ty) => self.type_infer(ty),
22            Type::Macro(ty) => self.type_macro(ty),
23            Type::Never(ty) => self.type_never(ty),
24            Type::Paren(ty) => self.type_paren(ty),
25            Type::Path(ty) => self.type_path(ty),
26            Type::Ptr(ty) => self.type_ptr(ty),
27            Type::Reference(ty) => self.type_reference(ty),
28            Type::Slice(ty) => self.type_slice(ty),
29            Type::TraitObject(ty) => self.type_trait_object(ty),
30            Type::Tuple(ty) => self.type_tuple(ty),
31            Type::Verbatim(ty) => self.type_verbatim(ty),
32            _ => unimplemented!("unknown Type"),
33        }
34    }
35
36    fn type_array(&mut self, ty: &TypeArray) {
37        self.word("[");
38        self.ty(&ty.elem);
39        self.word("; ");
40        self.expr(&ty.len, FixupContext::NONE);
41        self.word("]");
42    }
43
44    fn type_bare_fn(&mut self, ty: &TypeBareFn) {
45        if let Some(bound_lifetimes) = &ty.lifetimes {
46            self.bound_lifetimes(bound_lifetimes);
47        }
48        if ty.unsafety.is_some() {
49            self.word("unsafe ");
50        }
51        if let Some(abi) = &ty.abi {
52            self.abi(abi);
53        }
54        self.word("fn(");
55        self.cbox(INDENT);
56        self.zerobreak();
57        for bare_fn_arg in ty.inputs.iter().delimited() {
58            self.bare_fn_arg(&bare_fn_arg);
59            self.trailing_comma(bare_fn_arg.is_last && ty.variadic.is_none());
60        }
61        if let Some(variadic) = &ty.variadic {
62            self.bare_variadic(variadic);
63            self.zerobreak();
64        }
65        self.offset(-INDENT);
66        self.end();
67        self.word(")");
68        self.return_type(&ty.output);
69    }
70
71    fn type_group(&mut self, ty: &TypeGroup) {
72        self.ty(&ty.elem);
73    }
74
75    fn type_impl_trait(&mut self, ty: &TypeImplTrait) {
76        self.word("impl ");
77        for type_param_bound in ty.bounds.iter().delimited() {
78            if !type_param_bound.is_first {
79                self.word(" + ");
80            }
81            self.type_param_bound(&type_param_bound);
82        }
83    }
84
85    fn type_infer(&mut self, ty: &TypeInfer) {
86        let _ = ty;
87        self.word("_");
88    }
89
90    fn type_macro(&mut self, ty: &TypeMacro) {
91        let semicolon = false;
92        self.mac(&ty.mac, None, semicolon);
93    }
94
95    fn type_never(&mut self, ty: &TypeNever) {
96        let _ = ty;
97        self.word("!");
98    }
99
100    fn type_paren(&mut self, ty: &TypeParen) {
101        self.word("(");
102        self.ty(&ty.elem);
103        self.word(")");
104    }
105
106    fn type_path(&mut self, ty: &TypePath) {
107        self.qpath(&ty.qself, &ty.path, PathKind::Type);
108    }
109
110    fn type_ptr(&mut self, ty: &TypePtr) {
111        self.word("*");
112        if ty.mutability.is_some() {
113            self.word("mut ");
114        } else {
115            self.word("const ");
116        }
117        self.ty(&ty.elem);
118    }
119
120    fn type_reference(&mut self, ty: &TypeReference) {
121        self.word("&");
122        if let Some(lifetime) = &ty.lifetime {
123            self.lifetime(lifetime);
124            self.nbsp();
125        }
126        if ty.mutability.is_some() {
127            self.word("mut ");
128        }
129        self.ty(&ty.elem);
130    }
131
132    fn type_slice(&mut self, ty: &TypeSlice) {
133        self.word("[");
134        self.ty(&ty.elem);
135        self.word("]");
136    }
137
138    fn type_trait_object(&mut self, ty: &TypeTraitObject) {
139        self.word("dyn ");
140        for type_param_bound in ty.bounds.iter().delimited() {
141            if !type_param_bound.is_first {
142                self.word(" + ");
143            }
144            self.type_param_bound(&type_param_bound);
145        }
146    }
147
148    fn type_tuple(&mut self, ty: &TypeTuple) {
149        self.word("(");
150        self.cbox(INDENT);
151        self.zerobreak();
152        for elem in ty.elems.iter().delimited() {
153            self.ty(&elem);
154            if ty.elems.len() == 1 {
155                self.word(",");
156                self.zerobreak();
157            } else {
158                self.trailing_comma(elem.is_last);
159            }
160        }
161        self.offset(-INDENT);
162        self.end();
163        self.word(")");
164    }
165
166    #[cfg(not(feature = "verbatim"))]
167    fn type_verbatim(&mut self, ty: &TokenStream) {
168        unimplemented!("Type::Verbatim `{}`", ty);
169    }
170
171    #[cfg(feature = "verbatim")]
172    fn type_verbatim(&mut self, tokens: &TokenStream) {
173        use syn::parse::{Parse, ParseStream, Result};
174        use syn::punctuated::Punctuated;
175        use syn::{token, FieldsNamed, Token, TypeParamBound};
176
177        enum TypeVerbatim {
178            Ellipsis,
179            AnonStruct(AnonStruct),
180            AnonUnion(AnonUnion),
181            DynStar(DynStar),
182            MutSelf(MutSelf),
183        }
184
185        struct AnonStruct {
186            fields: FieldsNamed,
187        }
188
189        struct AnonUnion {
190            fields: FieldsNamed,
191        }
192
193        struct DynStar {
194            bounds: Punctuated<TypeParamBound, Token![+]>,
195        }
196
197        struct MutSelf {
198            ty: Option<Type>,
199        }
200
201        impl Parse for TypeVerbatim {
202            fn parse(input: ParseStream) -> Result<Self> {
203                let lookahead = input.lookahead1();
204                if lookahead.peek(Token![struct]) {
205                    input.parse::<Token![struct]>()?;
206                    let fields: FieldsNamed = input.parse()?;
207                    Ok(TypeVerbatim::AnonStruct(AnonStruct { fields }))
208                } else if lookahead.peek(Token![union]) && input.peek2(token::Brace) {
209                    input.parse::<Token![union]>()?;
210                    let fields: FieldsNamed = input.parse()?;
211                    Ok(TypeVerbatim::AnonUnion(AnonUnion { fields }))
212                } else if lookahead.peek(Token![dyn]) {
213                    input.parse::<Token![dyn]>()?;
214                    input.parse::<Token![*]>()?;
215                    let bounds = input.parse_terminated(TypeParamBound::parse, Token![+])?;
216                    Ok(TypeVerbatim::DynStar(DynStar { bounds }))
217                } else if lookahead.peek(Token![mut]) {
218                    input.parse::<Token![mut]>()?;
219                    input.parse::<Token![self]>()?;
220                    let ty = if input.is_empty() {
221                        None
222                    } else {
223                        input.parse::<Token![:]>()?;
224                        let ty: Type = input.parse()?;
225                        Some(ty)
226                    };
227                    Ok(TypeVerbatim::MutSelf(MutSelf { ty }))
228                } else if lookahead.peek(Token![...]) {
229                    input.parse::<Token![...]>()?;
230                    Ok(TypeVerbatim::Ellipsis)
231                } else {
232                    Err(lookahead.error())
233                }
234            }
235        }
236
237        let ty: TypeVerbatim = match syn::parse2(tokens.clone()) {
238            Ok(ty) => ty,
239            Err(_) => unimplemented!("Type::Verbatim `{}`", tokens),
240        };
241
242        match ty {
243            TypeVerbatim::Ellipsis => {
244                self.word("...");
245            }
246            TypeVerbatim::AnonStruct(ty) => {
247                self.cbox(INDENT);
248                self.word("struct {");
249                self.hardbreak_if_nonempty();
250                for field in &ty.fields.named {
251                    self.field(field);
252                    self.word(",");
253                    self.hardbreak();
254                }
255                self.offset(-INDENT);
256                self.end();
257                self.word("}");
258            }
259            TypeVerbatim::AnonUnion(ty) => {
260                self.cbox(INDENT);
261                self.word("union {");
262                self.hardbreak_if_nonempty();
263                for field in &ty.fields.named {
264                    self.field(field);
265                    self.word(",");
266                    self.hardbreak();
267                }
268                self.offset(-INDENT);
269                self.end();
270                self.word("}");
271            }
272            TypeVerbatim::DynStar(ty) => {
273                self.word("dyn* ");
274                for type_param_bound in ty.bounds.iter().delimited() {
275                    if !type_param_bound.is_first {
276                        self.word(" + ");
277                    }
278                    self.type_param_bound(&type_param_bound);
279                }
280            }
281            TypeVerbatim::MutSelf(bare_fn_arg) => {
282                self.word("mut self");
283                if let Some(ty) = &bare_fn_arg.ty {
284                    self.word(": ");
285                    self.ty(ty);
286                }
287            }
288        }
289    }
290
291    pub fn return_type(&mut self, ty: &ReturnType) {
292        match ty {
293            ReturnType::Default => {}
294            ReturnType::Type(_arrow, ty) => {
295                self.word(" -> ");
296                self.ty(ty);
297            }
298        }
299    }
300
301    fn bare_fn_arg(&mut self, bare_fn_arg: &BareFnArg) {
302        self.outer_attrs(&bare_fn_arg.attrs);
303        if let Some((name, _colon)) = &bare_fn_arg.name {
304            self.ident(name);
305            self.word(": ");
306        }
307        self.ty(&bare_fn_arg.ty);
308    }
309
310    fn bare_variadic(&mut self, variadic: &BareVariadic) {
311        self.outer_attrs(&variadic.attrs);
312        if let Some((name, _colon)) = &variadic.name {
313            self.ident(name);
314            self.word(": ");
315        }
316        self.word("...");
317    }
318
319    pub fn abi(&mut self, abi: &Abi) {
320        self.word("extern ");
321        if let Some(name) = &abi.name {
322            self.lit_str(name);
323            self.nbsp();
324        }
325    }
326}