// Format the body of an enum definition
Format the body of an enum definition
Format the body of an enum definition
fn format_variant_list(
        &mut self,
        enum_def: &ast::EnumDef,
        body_lo: BytePos,
        body_hi: BytePos,
    ) -> Option<String> {
        if enum_def.variants.is_empty() {
            let mut buffer = String::with_capacity(128);
            // 1 = "}"
            let span = mk_sp(body_lo, body_hi - BytePos(1));
            format_empty_struct_or_tuple(
                &self.get_context(),
                span,
                self.block_indent,
                &mut buffer,
                "",
                "}",
            );
            return Some(buffer);
        }
        let mut result = String::with_capacity(1024);
        let original_offset = self.block_indent;
        self.block_indent = self.block_indent.block_indent(self.config);
        // If enum variants have discriminants, try to vertically align those,
        // provided the discrims are not shifted too much  to the right
        let align_threshold: usize = self.config.enum_discrim_align_threshold();
        let discr_ident_lens: Vec<usize> = enum_def
            .variants
            .iter()
            .filter(|var| var.node.disr_expr.is_some())
            .map(|var| rewrite_ident(&self.get_context(), var.node.ident).len())
            .collect();
        // cut the list at the point of longest discrim shorter than the threshold
        // All of the discrims under the threshold will get padded, and all above - left as is.
        let pad_discrim_ident_to = *discr_ident_lens
            .iter()
            .filter(|&l| *l <= align_threshold)
            .max()
            .unwrap_or(&0);
        let itemize_list_with = |one_line_width: usize| {
            itemize_list(
                self.snippet_provider,
                enum_def.variants.iter(),
                "}",
                ",",
                |f| {
                    if !f.node.attrs.is_empty() {
                        f.node.attrs[0].span.lo()
                    } else {
                        f.span.lo()
                    }
                },
                |f| f.span.hi(),
                |f| self.format_variant(f, one_line_width, pad_discrim_ident_to),
                body_lo,
                body_hi,
                false,
            )
            .collect()
        };
        let mut items: Vec<_> =
            itemize_list_with(self.config.width_heuristics().struct_variant_width);
        // If one of the variants use multiple lines, use multi-lined formatting for all variants.
        let has_multiline_variant = items.iter().any(|item| item.inner_as_ref().contains('\n'));
        let has_single_line_variant = items.iter().any(|item| !item.inner_as_ref().contains('\n'));
        if has_multiline_variant && has_single_line_variant {
            items = itemize_list_with(0);
        }
        let shape = self.shape().sub_width(2)?;
        let fmt = ListFormatting::new(shape, self.config)
            .trailing_separator(self.config.trailing_comma())
            .preserve_newline(true);
        let list = write_list(&items, &fmt)?;
        result.push_str(&list);
        result.push_str(&original_offset.to_string_with_newline(self.config));
        result.push('}');
        Some(result)
    }
fn format_variant_list ( & mut self , enum_def : & ast :: EnumDef , body_lo : BytePos , body_hi : BytePos , ) -> Option < String > { if enum_def . variants . is_empty ( ) { let mut buffer = String :: with_capacity ( 128 ) ; // 1 = "}" let span = mk_sp ( body_lo , body_hi - BytePos ( 1 ) ) ; format_empty_struct_or_tuple ( & self . get_context ( ) , span , self . block_indent , & mut buffer , " " , " " , ) ; return Some ( buffer ) ; } let mut result = String :: with_capacity ( 1024 ) ; let original_offset = self . block_indent ; self . block_indent = self . block_indent . block_indent ( self . config ) ; // If enum variants have discriminants, try to vertically align those, // provided the discrims are not shifted too much  to the right let align_threshold : usize = self . config . enum_discrim_align_threshold ( ) ; let discr_ident_lens : Vec < usize > = enum_def . variants . iter ( ) . filter ( | var | var . node . disr_expr . is_some ( ) ) . map ( | var | rewrite_ident ( & self . get_context ( ) , var . node . ident ) . len ( ) ) . collect ( ) ; // cut the list at the point of longest discrim shorter than the threshold // All of the discrims under the threshold will get padded, and all above - left as is. let pad_discrim_ident_to = * discr_ident_lens . iter ( ) . filter ( | & l | * l <= align_threshold ) . max ( ) . unwrap_or ( & 0 ) ; let itemize_list_with = | one_line_width : usize | { itemize_list ( self . snippet_provider , enum_def . variants . iter ( ) , " " , " " , | f | { if ! f . node . attrs . is_empty ( ) { f . node . attrs [ 0 ] . span . lo ( ) } else { f . span . lo ( ) } } , | f | f . span . hi ( ) , | f | self . format_variant ( f , one_line_width , pad_discrim_ident_to ) , body_lo , body_hi , false , ) . collect ( ) } ; let mut items : Vec < _ > = itemize_list_with ( self . config . width_heuristics ( ) . struct_variant_width ) ; // If one of the variants use multiple lines, use multi-lined formatting for all variants. let has_multiline_variant = items . iter ( ) . any ( | item | item . inner_as_ref ( ) . contains ( '\n' ) ) ; let has_single_line_variant = items . iter ( ) . any ( | item | ! item . inner_as_ref ( ) . contains ( '\n' ) ) ; if has_multiline_variant && has_single_line_variant { items = itemize_list_with ( 0 ) ; } let shape = self . shape ( ) . sub_width ( 2 ) ? ; let fmt = ListFormatting :: new ( shape , self . config ) . trailing_separator ( self . config . trailing_comma ( ) ) . preserve_newline ( true ) ; let list = write_list ( & items , & fmt ) ? ; result . push_str ( & list ) ; result . push_str ( & original_offset . to_string_with_newline ( self . config ) ) ; result . push ( '}' ) ; Some ( result ) }
Format the body of an enum definition
Format the body of an enum definition
&mut self
enum_def
&ast::EnumDef
body_lo
BytePos
body_hi
BytePos