implement Default for FormatSpec

This commit is contained in:
mejrs
2025-05-23 22:51:40 +02:00
parent cf3af234e3
commit b68c06b1e2

View File

@@ -56,30 +56,12 @@ pub struct Argument<'a> {
impl<'a> Argument<'a> { impl<'a> Argument<'a> {
pub fn is_identifier(&self) -> bool { pub fn is_identifier(&self) -> bool {
matches!(self.position, Position::ArgumentNamed(_)) matches!(self.position, Position::ArgumentNamed(_)) && self.format == FormatSpec::default()
&& matches!(
self.format,
FormatSpec {
fill: None,
fill_span: None,
align: AlignUnknown,
sign: None,
alternate: false,
zero_pad: false,
debug_hex: None,
precision: CountImplied,
precision_span: None,
width: CountImplied,
width_span: None,
ty: "",
ty_span: None,
},
)
} }
} }
/// Specification for the formatting of an argument in the format string. /// Specification for the formatting of an argument in the format string.
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq, Default)]
pub struct FormatSpec<'a> { pub struct FormatSpec<'a> {
/// Optionally specified character to fill alignment with. /// Optionally specified character to fill alignment with.
pub fill: Option<char>, pub fill: Option<char>,
@@ -132,7 +114,7 @@ impl Position<'_> {
} }
/// Enum of alignments which are supported. /// Enum of alignments which are supported.
#[derive(Copy, Clone, Debug, PartialEq)] #[derive(Copy, Clone, Debug, PartialEq, Default)]
pub enum Alignment { pub enum Alignment {
/// The value will be aligned to the left. /// The value will be aligned to the left.
AlignLeft, AlignLeft,
@@ -141,6 +123,7 @@ pub enum Alignment {
/// The value will be aligned in the center. /// The value will be aligned in the center.
AlignCenter, AlignCenter,
/// The value will take on a default alignment. /// The value will take on a default alignment.
#[default]
AlignUnknown, AlignUnknown,
} }
@@ -164,7 +147,7 @@ pub enum DebugHex {
/// A count is used for the precision and width parameters of an integer, and /// A count is used for the precision and width parameters of an integer, and
/// can reference either an argument or a literal integer. /// can reference either an argument or a literal integer.
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq, Default)]
pub enum Count<'a> { pub enum Count<'a> {
/// The count is specified explicitly. /// The count is specified explicitly.
CountIs(u16), CountIs(u16),
@@ -175,6 +158,7 @@ pub enum Count<'a> {
/// The count is specified by a star (like in `{:.*}`) that refers to the argument at the given index. /// The count is specified by a star (like in `{:.*}`) that refers to the argument at the given index.
CountIsStar(usize), CountIsStar(usize),
/// The count is implied and cannot be explicitly specified. /// The count is implied and cannot be explicitly specified.
#[default]
CountImplied, CountImplied,
} }
@@ -596,21 +580,8 @@ impl<'a> Parser<'a> {
/// Parses a format specifier at the current position, returning all of the /// Parses a format specifier at the current position, returning all of the
/// relevant information in the `FormatSpec` struct. /// relevant information in the `FormatSpec` struct.
fn format(&mut self) -> FormatSpec<'a> { fn format(&mut self) -> FormatSpec<'a> {
let mut spec = FormatSpec { let mut spec = FormatSpec::default();
fill: None,
fill_span: None,
align: AlignUnknown,
sign: None,
alternate: false,
zero_pad: false,
debug_hex: None,
precision: CountImplied,
precision_span: None,
width: CountImplied,
width_span: None,
ty: &self.input[..0],
ty_span: None,
};
if !self.consume(':') { if !self.consume(':') {
return spec; return spec;
} }
@@ -727,21 +698,8 @@ impl<'a> Parser<'a> {
/// Parses an inline assembly template modifier at the current position, returning the modifier /// Parses an inline assembly template modifier at the current position, returning the modifier
/// in the `ty` field of the `FormatSpec` struct. /// in the `ty` field of the `FormatSpec` struct.
fn inline_asm(&mut self) -> FormatSpec<'a> { fn inline_asm(&mut self) -> FormatSpec<'a> {
let mut spec = FormatSpec { let mut spec = FormatSpec::default();
fill: None,
fill_span: None,
align: AlignUnknown,
sign: None,
alternate: false,
zero_pad: false,
debug_hex: None,
precision: CountImplied,
precision_span: None,
width: CountImplied,
width_span: None,
ty: &self.input[..0],
ty_span: None,
};
if !self.consume(':') { if !self.consume(':') {
return spec; return spec;
} }