From dd7b8da15119419aa994f5b7a9fbe19e113b56ee Mon Sep 17 00:00:00 2001 From: nils <48135649+Nilstrieb@users.noreply.github.com> Date: Mon, 12 Sep 2022 14:36:04 +0200 Subject: [PATCH] opts --- src/lib.rs | 1 + src/opts.rs | 130 ++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 92 insertions(+), 39 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index cbbe61e..319bf73 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,7 @@ mod opts; mod write; pub use mono_fmt_macro::format_args; +use opts::{WithAlternate, WithFill, WithWidth, WithLeftAlign, WithRightAlign, WithCenterAlign}; pub use crate::args::Arguments; pub use crate::opts::FmtOpts; diff --git a/src/opts.rs b/src/opts.rs index 09b421f..36cec26 100644 --- a/src/opts.rs +++ b/src/opts.rs @@ -1,3 +1,5 @@ +use crate::Formatter; + pub enum Alignment { Left, Center, @@ -25,8 +27,6 @@ pub trait FmtOpts { } } -pub struct Default; - mod never { use crate::FmtOpts; @@ -64,59 +64,111 @@ impl FmtOpts for () { None } } +pub struct WithAlternate(pub I); -macro_rules! with_fmts { - (@ty $ty:ty, $($tt:tt)*) => { $ty }; - ($(struct $name:ident $()? { - fn $override:ident() -> $override_ret:ty { - $($override_body:tt)* - } - })*) => { - $( - pub struct $name(I); +impl FmtOpts for WithAlternate { + type Inner = I; + fn alternate() -> bool { + true + } +} +pub struct WithWidth(pub I); - impl FmtOpts for $name { - type Inner = I; +impl FmtOpts for WithWidth { + type Inner = I; + fn width() -> Option { + Some(A) + } +} +pub struct WithLeftAlign(pub I); - fn $override() -> $override_ret { - $($override_body)* - } - } - )* - }; +impl FmtOpts for WithLeftAlign { + type Inner = I; + fn align() -> Option { + Some(Alignment::Left) + } +} +pub struct WithRightAlign(pub I); - (struct $name:ident) => {}; +impl FmtOpts for WithRightAlign { + type Inner = I; + fn align() -> Option { + Some(Alignment::Right) + } +} +pub struct WithCenterAlign(pub I); + +impl FmtOpts for WithCenterAlign { + type Inner = I; + fn align() -> Option { + Some(Alignment::Center) + } +} +pub struct WithFill(pub I); + +impl FmtOpts for WithFill { + type Inner = I; + fn fill() -> Option { + Some(A) + } } -with_fmts! { - struct WithAlternate { - fn alternate() -> bool { - true +impl Formatter { + pub fn alternate(&self) -> bool { + O::alternate() + } + + pub fn width() -> Option { + O::width() + } + + pub fn align() -> Option { + O::align() + } + + pub fn fill() -> Option { + O::fill() + } + + pub fn with_alternate(self) -> Formatter> { + Formatter { + buf: self.buf, + opts: WithAlternate(self.opts), } } - struct WithWidth { - fn width() -> Option { - Some(A) + + pub fn with_width(self) -> Formatter> { + Formatter { + buf: self.buf, + opts: WithWidth(self.opts), } } - struct WithLeftAlign { - fn align() -> Option { - Some(Alignment::Left) + + pub fn with_left_align(self) -> Formatter> { + Formatter { + buf: self.buf, + opts: WithLeftAlign(self.opts), } } - struct WithRightAlign { - fn align() -> Option { - Some(Alignment::Right) + + pub fn with_right_align(self) -> Formatter> { + Formatter { + buf: self.buf, + opts: WithRightAlign(self.opts), } } - struct WithCenterAlign { - fn align() -> Option { - Some(Alignment::Center) + + pub fn with_center_align(self) -> Formatter> { + Formatter { + buf: self.buf, + opts: WithCenterAlign(self.opts), } } - struct WithFill { - fn fill() -> Option { - Some(A) + + pub fn with_fill(self) -> Formatter> { + Formatter { + buf: self.buf, + opts: WithFill(self.opts), } } }