Destructure format_options in make_format_spec.

This commit is contained in:
Mara Bos 2023-01-27 11:43:38 +01:00
parent be69002dd7
commit 21cf9dbc85

View file

@ -137,11 +137,21 @@ fn make_format_spec<'hir>(
}
Err(_) => ctx.expr(sp, hir::ExprKind::Err),
};
let fill = ctx.expr_char(sp, placeholder.format_options.fill.unwrap_or(' '));
let &FormatOptions {
ref width,
ref precision,
alignment,
fill,
sign,
alternate,
zero_pad,
debug_hex,
} = &placeholder.format_options;
let fill = ctx.expr_char(sp, fill.unwrap_or(' '));
let align = ctx.expr_lang_item_type_relative(
sp,
hir::LangItem::FormatAlignment,
match placeholder.format_options.alignment {
match alignment {
Some(FormatAlignment::Left) => sym::Left,
Some(FormatAlignment::Right) => sym::Right,
Some(FormatAlignment::Center) => sym::Center,
@ -149,21 +159,21 @@ fn make_format_spec<'hir>(
},
);
// This needs to match `FlagV1` in library/core/src/fmt/mod.rs.
let flags: u32 = ((placeholder.format_options.sign == Some(FormatSign::Plus)) as u32)
| ((placeholder.format_options.sign == Some(FormatSign::Minus)) as u32) << 1
| (placeholder.format_options.alternate as u32) << 2
| (placeholder.format_options.zero_pad as u32) << 3
| ((placeholder.format_options.debug_hex == Some(FormatDebugHex::Lower)) as u32) << 4
| ((placeholder.format_options.debug_hex == Some(FormatDebugHex::Upper)) as u32) << 5;
let flags: u32 = ((sign == Some(FormatSign::Plus)) as u32)
| ((sign == Some(FormatSign::Minus)) as u32) << 1
| (alternate as u32) << 2
| (zero_pad as u32) << 3
| ((debug_hex == Some(FormatDebugHex::Lower)) as u32) << 4
| ((debug_hex == Some(FormatDebugHex::Upper)) as u32) << 5;
let flags = ctx.expr_u32(sp, flags);
let prec = make_count(ctx, sp, &placeholder.format_options.precision, argmap);
let width = make_count(ctx, sp, &placeholder.format_options.width, argmap);
let precision = make_count(ctx, sp, &precision, argmap);
let width = make_count(ctx, sp, &width, argmap);
let format_placeholder_new = ctx.arena.alloc(ctx.expr_lang_item_type_relative(
sp,
hir::LangItem::FormatPlaceholder,
sym::new,
));
let args = ctx.arena.alloc_from_iter([position, fill, align, flags, prec, width]);
let args = ctx.arena.alloc_from_iter([position, fill, align, flags, precision, width]);
ctx.expr_call_mut(sp, format_placeholder_new, args)
}