use alloc::vec::Vec;
#[derive(Debug)]
pub struct FormatString<'a> {
pub segments: Vec<Segment<'a>>,
}
#[derive(Debug)]
pub enum Segment<'a> {
Literal(&'a str),
EscapedOpen,
EscapedClose,
Placeholder(Placeholder<'a>),
}
#[derive(Debug)]
pub struct Placeholder<'a> {
pub argument: Argument<'a>,
pub spec: FormatSpec<'a>,
pub offset: usize,
}
#[derive(Debug)]
pub enum Argument<'a> {
Implicit,
Positional(usize),
Named(&'a str),
}
#[derive(Debug)]
pub struct FormatSpec<'a> {
pub fill: Option<char>,
pub align: Option<Align>,
pub sign: Option<Sign>,
pub alternate: bool,
pub zero_pad: bool,
pub width: Option<Count<'a>>,
pub precision: Option<Precision<'a>>,
pub format_type: FormatType,
}
impl FormatSpec<'_> {
pub const fn default() -> FormatSpec<'static> {
FormatSpec {
fill: None,
align: None,
sign: None,
alternate: false,
zero_pad: false,
width: None,
precision: None,
format_type: FormatType::Display,
}
}
pub fn is_default(&self) -> bool {
self.fill.is_none()
&& self.align.is_none()
&& self.sign.is_none()
&& !self.alternate
&& !self.zero_pad
&& self.width.is_none()
&& self.precision.is_none()
&& self.format_type == FormatType::Display
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Align {
Left,
Center,
Right,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Sign {
Plus,
Minus,
}
#[derive(Debug)]
pub enum Count<'a> {
Literal(usize),
Param(CountParam<'a>),
}
#[derive(Debug)]
pub enum CountParam<'a> {
Positional(usize),
Named(&'a str),
}
#[derive(Debug)]
pub enum Precision<'a> {
Count(Count<'a>),
Star,
}
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum FormatType {
Display,
Debug,
DebugLowerHex,
DebugUpperHex,
Octal,
LowerHex,
UpperHex,
Binary,
LowerExp,
UpperExp,
Pointer,
}