1use crate::algorithm::Printer;
2use crate::iter::IterDelimited;
3use crate::path::PathKind;
4use crate::INDENT;
5use syn::{Field, Fields, FieldsUnnamed, Variant, VisRestricted, Visibility};
6
7impl Printer {
8 pub fn variant(&mut self, variant: &Variant) {
9 self.outer_attrs(&variant.attrs);
10 self.ident(&variant.ident);
11 match &variant.fields {
12 Fields::Named(fields) => {
13 self.nbsp();
14 self.word("{");
15 self.cbox(INDENT);
16 self.space();
17 for field in fields.named.iter().delimited() {
18 self.field(&field);
19 self.trailing_comma_or_space(field.is_last);
20 }
21 self.offset(-INDENT);
22 self.end();
23 self.word("}");
24 }
25 Fields::Unnamed(fields) => {
26 self.cbox(INDENT);
27 self.fields_unnamed(fields);
28 self.end();
29 }
30 Fields::Unit => {}
31 }
32 if let Some((_eq_token, discriminant)) = &variant.discriminant {
33 self.word(" = ");
34 self.expr(discriminant);
35 }
36 }
37
38 pub fn fields_unnamed(&mut self, fields: &FieldsUnnamed) {
39 self.word("(");
40 self.zerobreak();
41 for field in fields.unnamed.iter().delimited() {
42 self.field(&field);
43 self.trailing_comma(field.is_last);
44 }
45 self.offset(-INDENT);
46 self.word(")");
47 }
48
49 pub fn field(&mut self, field: &Field) {
50 self.outer_attrs(&field.attrs);
51 self.visibility(&field.vis);
52 if let Some(ident) = &field.ident {
53 self.ident(ident);
54 self.word(": ");
55 }
56 self.ty(&field.ty);
57 }
58
59 pub fn visibility(&mut self, vis: &Visibility) {
60 match vis {
61 Visibility::Public(_) => self.word("pub "),
62 Visibility::Restricted(vis) => self.vis_restricted(vis),
63 Visibility::Inherited => {}
64 }
65 }
66
67 fn vis_restricted(&mut self, vis: &VisRestricted) {
68 self.word("pub(");
69 let omit_in = vis.path.get_ident().map_or(false, |ident| {
70 matches!(ident.to_string().as_str(), "self" | "super" | "crate")
71 });
72 if !omit_in {
73 self.word("in ");
74 }
75 self.path(&vis.path, PathKind::Simple);
76 self.word(") ");
77 }
78}
79