Skip to main content

rustidy_ast/item/
implementation.rs

1//! Implementation statement
2
3// Imports
4use {
5	crate::{attr::BracedWithInnerAttributes, token, ty::{Type, TypePath}},
6	super::{function::{GenericParams, WhereClause}, trait_::AssociatedItem},
7	rustidy_format::{Format, Formattable, WhitespaceFormat},
8	rustidy_parse::Parse,
9	rustidy_print::Print,
10	rustidy_util::Whitespace,
11};
12
13/// `Implementation`
14#[derive(PartialEq, Eq, Clone, Debug)]
15#[derive(serde::Serialize, serde::Deserialize)]
16#[derive(Parse, Formattable, Format, Print)]
17#[parse(name = "an implementation")]
18pub enum Implementation {
19	Inherent(InherentImpl),
20	Trait(TraitImpl),
21}
22
23/// `InherentImpl`
24#[derive(PartialEq, Eq, Clone, Debug)]
25#[derive(serde::Serialize, serde::Deserialize)]
26#[derive(Parse, Formattable, Format, Print)]
27pub struct InherentImpl {
28	pub impl_:    token::Impl,
29	#[format(prefix_ws = Whitespace::REMOVE)]
30	pub generics: Option<GenericParams>,
31	#[format(prefix_ws = Whitespace::SINGLE)]
32	pub ty:       Type,
33	#[format(prefix_ws = Whitespace::INDENT)]
34	pub where_:   Option<WhereClause>,
35	#[format(prefix_ws = Whitespace::SINGLE)]
36	pub body:     BracedWithInnerAttributes<ImplBody>,
37}
38
39/// `TraitImpl`
40#[derive(PartialEq, Eq, Clone, Debug)]
41#[derive(serde::Serialize, serde::Deserialize)]
42#[derive(Parse, Formattable, Format, Print)]
43pub struct TraitImpl {
44	pub unsafe_:  Option<token::Unsafe>,
45	#[format(prefix_ws(expr = Whitespace::SINGLE, if_ = self.unsafe_.is_some()))]
46	pub impl_:    token::Impl,
47	#[format(prefix_ws = Whitespace::REMOVE)]
48	pub generics: Option<GenericParams>,
49	#[format(prefix_ws = Whitespace::SINGLE)]
50	pub const_:   Option<token::Const>,
51	#[format(prefix_ws = Whitespace::SINGLE)]
52	pub not:      Option<token::Not>,
53	#[format(prefix_ws = match self.not.is_some() {
54		true => Whitespace::REMOVE,
55		false => Whitespace::SINGLE,
56	})]
57	pub trait_:   TypePath,
58	#[parse(fatal)]
59	#[format(prefix_ws = Whitespace::SINGLE)]
60	pub for_:     token::For,
61	#[format(prefix_ws = Whitespace::SINGLE)]
62	pub ty:       Type,
63	#[format(prefix_ws = Whitespace::INDENT)]
64	pub where_:   Option<WhereClause>,
65	#[format(prefix_ws = Whitespace::SINGLE)]
66	pub body:     BracedWithInnerAttributes<ImplBody>,
67}
68
69#[derive(PartialEq, Eq, Clone, Debug)]
70#[derive(serde::Serialize, serde::Deserialize)]
71#[derive(Parse, Formattable, Format, Print)]
72pub struct ImplBody(
73	#[format(args = rustidy_format::vec::args_prefix_ws(Whitespace::INDENT))]
74	pub Vec<AssociatedItem>,
75);