Skip to main content

rustidy_ast/ty/
tuple.rs

1//! Tuple type
2
3// Imports
4use {
5	crate::{token, util::Parenthesized},
6	super::Type,
7	rustidy_ast_util::delimited,
8	rustidy_format::{Format, FormatOutput, Formattable, WhitespaceConfig, WhitespaceFormat},
9	rustidy_parse::Parse,
10	rustidy_print::Print,
11	rustidy_util::Whitespace,
12};
13
14/// `TupleType`
15#[derive(PartialEq, Eq, Clone, Debug)]
16#[derive(serde::Serialize, serde::Deserialize)]
17#[derive(Parse, Formattable, Format, Print)]
18#[parse(name = "a tuple type")]
19pub struct TupleType(#[format(args = delimited::FmtRemove)] Parenthesized<Option<TupleTypeInner>>);
20
21#[derive(PartialEq, Eq, Clone, Debug)]
22#[derive(serde::Serialize, serde::Deserialize)]
23#[derive(Parse, Formattable, Print)]
24pub struct TupleTypeInner {
25	pub tys: Vec<(Type, token::Comma)>,
26	pub end: Option<Box<Type>>,
27}
28
29impl Format<WhitespaceConfig, ()> for TupleTypeInner {
30	fn format(
31		&mut self,
32		ctx: &mut rustidy_format::Context,
33		prefix_ws: WhitespaceConfig,
34		_args: ()
35	) -> FormatOutput {
36		let [(first_ty, first_comma), tys @ ..] = &mut *self.tys else {
37			return ctx.format(&mut self.end, prefix_ws);
38		};
39
40		let mut output = FormatOutput::default();
41
42		ctx
43			.format(first_ty, prefix_ws)
44			.append_to(&mut output);
45		ctx
46			.format(first_comma, Whitespace::REMOVE)
47			.append_to(&mut output);
48		for (ty, comma) in tys {
49			ctx
50				.format(ty, Whitespace::SINGLE)
51				.append_to(&mut output);
52			ctx
53				.format(comma, Whitespace::REMOVE)
54				.append_to(&mut output);
55		}
56		ctx
57			.format(&mut self.end, Whitespace::SINGLE)
58			.append_to(&mut output);
59
60		output
61	}
62}