This commit is contained in:
nora 2022-09-12 14:36:04 +02:00
parent c458902a6b
commit dd7b8da151
2 changed files with 92 additions and 39 deletions

View file

@ -7,6 +7,7 @@ mod opts;
mod write; mod write;
pub use mono_fmt_macro::format_args; pub use mono_fmt_macro::format_args;
use opts::{WithAlternate, WithFill, WithWidth, WithLeftAlign, WithRightAlign, WithCenterAlign};
pub use crate::args::Arguments; pub use crate::args::Arguments;
pub use crate::opts::FmtOpts; pub use crate::opts::FmtOpts;

View file

@ -1,3 +1,5 @@
use crate::Formatter;
pub enum Alignment { pub enum Alignment {
Left, Left,
Center, Center,
@ -25,8 +27,6 @@ pub trait FmtOpts {
} }
} }
pub struct Default;
mod never { mod never {
use crate::FmtOpts; use crate::FmtOpts;
@ -64,59 +64,111 @@ impl FmtOpts for () {
None None
} }
} }
pub struct WithAlternate<I>(pub I);
macro_rules! with_fmts { impl<I: FmtOpts> FmtOpts for WithAlternate<I> {
(@ty $ty:ty, $($tt:tt)*) => { $ty }; type Inner = I;
($(struct $name:ident $(<const A: $param:ty>)? { fn alternate() -> bool {
fn $override:ident() -> $override_ret:ty { true
$($override_body:tt)* }
} }
})*) => { pub struct WithWidth<I, const A: usize>(pub I);
$(
pub struct $name<I, $(const A: $param)?>(I);
impl<I: FmtOpts, $(const A: $param)?> FmtOpts for $name<I, $(with_fmts!(@ty A, $param))?> { impl<I: FmtOpts, const A: usize> FmtOpts for WithWidth<I, A> {
type Inner = I; type Inner = I;
fn width() -> Option<usize> {
Some(A)
}
}
pub struct WithLeftAlign<I>(pub I);
fn $override() -> $override_ret { impl<I: FmtOpts> FmtOpts for WithLeftAlign<I> {
$($override_body)* type Inner = I;
} fn align() -> Option<Alignment> {
} Some(Alignment::Left)
)* }
}; }
pub struct WithRightAlign<I>(pub I);
(struct $name:ident) => {}; impl<I: FmtOpts> FmtOpts for WithRightAlign<I> {
type Inner = I;
fn align() -> Option<Alignment> {
Some(Alignment::Right)
}
}
pub struct WithCenterAlign<I>(pub I);
impl<I: FmtOpts> FmtOpts for WithCenterAlign<I> {
type Inner = I;
fn align() -> Option<Alignment> {
Some(Alignment::Center)
}
}
pub struct WithFill<I, const A: char>(pub I);
impl<I: FmtOpts, const A: char> FmtOpts for WithFill<I, A> {
type Inner = I;
fn fill() -> Option<char> {
Some(A)
}
} }
with_fmts! { impl<W, O: FmtOpts> Formatter<W, O> {
struct WithAlternate { pub fn alternate(&self) -> bool {
fn alternate() -> bool { O::alternate()
true }
pub fn width() -> Option<usize> {
O::width()
}
pub fn align() -> Option<Alignment> {
O::align()
}
pub fn fill() -> Option<char> {
O::fill()
}
pub fn with_alternate(self) -> Formatter<W, WithAlternate<O>> {
Formatter {
buf: self.buf,
opts: WithAlternate(self.opts),
} }
} }
struct WithWidth<const A: usize> {
fn width() -> Option<usize> { pub fn with_width<const WIDTH: usize>(self) -> Formatter<W, WithWidth<O, WIDTH>> {
Some(A) Formatter {
buf: self.buf,
opts: WithWidth(self.opts),
} }
} }
struct WithLeftAlign {
fn align() -> Option<Alignment> { pub fn with_left_align(self) -> Formatter<W, WithLeftAlign<O>> {
Some(Alignment::Left) Formatter {
buf: self.buf,
opts: WithLeftAlign(self.opts),
} }
} }
struct WithRightAlign {
fn align() -> Option<Alignment> { pub fn with_right_align(self) -> Formatter<W, WithRightAlign<O>> {
Some(Alignment::Right) Formatter {
buf: self.buf,
opts: WithRightAlign(self.opts),
} }
} }
struct WithCenterAlign {
fn align() -> Option<Alignment> { pub fn with_center_align(self) -> Formatter<W, WithCenterAlign<O>> {
Some(Alignment::Center) Formatter {
buf: self.buf,
opts: WithCenterAlign(self.opts),
} }
} }
struct WithFill<const A: char> {
fn fill() -> Option<char> { pub fn with_fill<const FILL: char>(self) -> Formatter<W, WithFill<O, FILL>> {
Some(A) Formatter {
buf: self.buf,
opts: WithFill(self.opts),
} }
} }
} }