Skip to main content

rustidy_ast/
ty.rs

1//! Type
2
3// Modules
4pub mod array;
5pub mod bare_function;
6pub mod path;
7pub mod pointer;
8pub mod qualified;
9pub mod slice;
10pub mod tuple;
11
12// Exports
13pub use self::{
14	array::ArrayType,
15	bare_function::BareFunctionType,
16	path::TypePath,
17	pointer::RawPointerType,
18	qualified::QualifiedPathInType,
19	slice::SliceType,
20	tuple::TupleType,
21};
22
23// Imports
24use {
25	super::{
26		expr::without_block::MacroInvocation,
27		item::function::{TraitBound, TypeParamBounds},
28		lifetime::Lifetime,
29		token,
30		util::Parenthesized,
31	},
32	rustidy_ast_util::delimited,
33	rustidy_format::{Format, Formattable, WhitespaceFormat},
34	rustidy_parse::Parse,
35	rustidy_print::Print,
36	rustidy_util::{ArenaData, ArenaIdx, Whitespace},
37};
38
39/// `Type`
40#[derive(PartialEq, Eq, Clone, Debug)]
41#[derive(serde::Serialize, serde::Deserialize)]
42#[derive(Parse, Formattable, Format, Print)]
43pub struct Type(pub ArenaIdx<TypeInner>);
44
45#[derive(PartialEq, Eq, Clone, Debug)]
46#[derive(ArenaData)]
47#[derive(serde::Serialize, serde::Deserialize)]
48#[derive(Parse, Formattable, Format, Print)]
49#[parse(name = "a type")]
50pub enum TypeInner {
51	ImplTrait(ImplTraitType),
52	TraitObject(TraitObjectType),
53	#[parse(peek = MacroInvocation)]
54	NoBounds(TypeNoBounds),
55}
56
57/// `TypeNoBounds`
58#[derive(PartialEq, Eq, Clone, Debug)]
59#[derive(derive_more::From)]
60#[derive(serde::Serialize, serde::Deserialize)]
61#[derive(Parse, Formattable, Format, Print)]
62pub enum TypeNoBounds {
63	MacroInvocation(MacroInvocation),
64	Path(TypePath),
65
66	Parenthesized(ParenthesizedPath),
67	ImplTraitOneBound(ImplTraitTypeOneBound),
68	TraitObjectOneBound(TraitObjectTypeOneBound),
69	Tuple(TupleType),
70	Never(NeverType),
71	RawPointer(RawPointerType),
72	Reference(ReferenceType),
73	Array(ArrayType),
74	Slice(SliceType),
75	Inferred(InferredType),
76	QualifiedPathIn(QualifiedPathInType),
77	BareFunction(BareFunctionType),
78}
79
80/// `ParenthesizedPath`
81#[derive(PartialEq, Eq, Clone, Debug)]
82#[derive(serde::Serialize, serde::Deserialize)]
83#[derive(Parse, Formattable, Format, Print)]
84pub struct ParenthesizedPath(
85	#[format(args = delimited::fmt_single_if_non_blank())]
86	Parenthesized<Box<Type>>,
87);
88
89/// `NeverType`
90#[derive(PartialEq, Eq, Clone, Debug)]
91#[derive(serde::Serialize, serde::Deserialize)]
92#[derive(Parse, Formattable, Format, Print)]
93pub struct NeverType(token::Not);
94
95/// `ReferenceType`
96#[derive(PartialEq, Eq, Clone, Debug)]
97#[derive(serde::Serialize, serde::Deserialize)]
98#[derive(Parse, Formattable, Format, Print)]
99#[parse(name = "a reference type")]
100pub struct ReferenceType {
101	pub ref_:     token::AndTy,
102	#[format(prefix_ws = Whitespace::REMOVE)]
103	pub lifetime: Option<Lifetime>,
104	#[format(prefix_ws = match self.lifetime.is_some() {
105		true => Whitespace::SINGLE,
106		false => Whitespace::REMOVE,
107	})]
108	pub mut_:     Option<token::Mut>,
109	#[format(prefix_ws = match self.lifetime.is_some() || self.mut_.is_some() {
110		true => Whitespace::SINGLE,
111		false => Whitespace::REMOVE,
112	})]
113	pub ty:       Box<TypeNoBounds>,
114}
115
116/// `InferredType`
117#[derive(PartialEq, Eq, Clone, Debug)]
118#[derive(serde::Serialize, serde::Deserialize)]
119#[derive(Parse, Formattable, Format, Print)]
120pub struct InferredType(token::Underscore);
121
122/// `ImplTraitTypeOneBound`
123#[derive(PartialEq, Eq, Clone, Debug)]
124#[derive(serde::Serialize, serde::Deserialize)]
125#[derive(Parse, Formattable, Format, Print)]
126pub struct ImplTraitTypeOneBound {
127	pub impl_: token::Impl,
128	#[format(prefix_ws = Whitespace::SINGLE)]
129	pub bound: TraitBound,
130}
131
132/// `TraitObjectTypeOneBound`
133#[derive(PartialEq, Eq, Clone, Debug)]
134#[derive(serde::Serialize, serde::Deserialize)]
135#[derive(Parse, Formattable, Format, Print)]
136pub struct TraitObjectTypeOneBound {
137	pub dyn_:  Option<token::Dyn>,
138	#[format(prefix_ws(expr = Whitespace::SINGLE, if_ = self.dyn_.is_some()))]
139	pub bound: TraitBound,
140}
141
142/// `ImplTraitType`
143#[derive(PartialEq, Eq, Clone, Debug)]
144#[derive(serde::Serialize, serde::Deserialize)]
145#[derive(Parse, Formattable, Format, Print)]
146pub struct ImplTraitType {
147	pub impl_: token::Impl,
148	#[format(prefix_ws = Whitespace::SINGLE)]
149	pub bound: TypeParamBounds,
150}
151
152/// `TraitObjectType`
153#[derive(PartialEq, Eq, Clone, Debug)]
154#[derive(serde::Serialize, serde::Deserialize)]
155#[derive(Parse, Formattable, Format, Print)]
156pub struct TraitObjectType {
157	pub dyn_:  Option<token::Dyn>,
158	#[format(prefix_ws(expr = Whitespace::SINGLE, if_ = self.dyn_.is_some()))]
159	pub bound: TypeParamBounds,
160}