mirror of
https://github.com/Noratrieb/mono-fmt.git
synced 2026-01-17 00:35:05 +01:00
opts
This commit is contained in:
parent
c458902a6b
commit
dd7b8da151
2 changed files with 92 additions and 39 deletions
|
|
@ -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;
|
||||||
|
|
|
||||||
114
src/opts.rs
114
src/opts.rs
|
|
@ -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 };
|
|
||||||
($(struct $name:ident $(<const A: $param:ty>)? {
|
|
||||||
fn $override:ident() -> $override_ret:ty {
|
|
||||||
$($override_body:tt)*
|
|
||||||
}
|
|
||||||
})*) => {
|
|
||||||
$(
|
|
||||||
pub struct $name<I, $(const A: $param)?>(I);
|
|
||||||
|
|
||||||
impl<I: FmtOpts, $(const A: $param)?> FmtOpts for $name<I, $(with_fmts!(@ty A, $param))?> {
|
|
||||||
type Inner = I;
|
type Inner = I;
|
||||||
|
|
||||||
fn $override() -> $override_ret {
|
|
||||||
$($override_body)*
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)*
|
|
||||||
};
|
|
||||||
|
|
||||||
(struct $name:ident) => {};
|
|
||||||
}
|
|
||||||
|
|
||||||
with_fmts! {
|
|
||||||
struct WithAlternate {
|
|
||||||
fn alternate() -> bool {
|
fn alternate() -> bool {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
struct WithWidth<const A: usize> {
|
pub struct WithWidth<I, const A: usize>(pub I);
|
||||||
|
|
||||||
|
impl<I: FmtOpts, const A: usize> FmtOpts for WithWidth<I, A> {
|
||||||
|
type Inner = I;
|
||||||
fn width() -> Option<usize> {
|
fn width() -> Option<usize> {
|
||||||
Some(A)
|
Some(A)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
struct WithLeftAlign {
|
pub struct WithLeftAlign<I>(pub I);
|
||||||
|
|
||||||
|
impl<I: FmtOpts> FmtOpts for WithLeftAlign<I> {
|
||||||
|
type Inner = I;
|
||||||
fn align() -> Option<Alignment> {
|
fn align() -> Option<Alignment> {
|
||||||
Some(Alignment::Left)
|
Some(Alignment::Left)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
struct WithRightAlign {
|
pub struct WithRightAlign<I>(pub I);
|
||||||
|
|
||||||
|
impl<I: FmtOpts> FmtOpts for WithRightAlign<I> {
|
||||||
|
type Inner = I;
|
||||||
fn align() -> Option<Alignment> {
|
fn align() -> Option<Alignment> {
|
||||||
Some(Alignment::Right)
|
Some(Alignment::Right)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
struct WithCenterAlign {
|
pub struct WithCenterAlign<I>(pub I);
|
||||||
|
|
||||||
|
impl<I: FmtOpts> FmtOpts for WithCenterAlign<I> {
|
||||||
|
type Inner = I;
|
||||||
fn align() -> Option<Alignment> {
|
fn align() -> Option<Alignment> {
|
||||||
Some(Alignment::Center)
|
Some(Alignment::Center)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
struct WithFill<const A: char> {
|
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> {
|
fn fill() -> Option<char> {
|
||||||
Some(A)
|
Some(A)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<W, O: FmtOpts> Formatter<W, O> {
|
||||||
|
pub fn alternate(&self) -> bool {
|
||||||
|
O::alternate()
|
||||||
|
}
|
||||||
|
|
||||||
|
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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_width<const WIDTH: usize>(self) -> Formatter<W, WithWidth<O, WIDTH>> {
|
||||||
|
Formatter {
|
||||||
|
buf: self.buf,
|
||||||
|
opts: WithWidth(self.opts),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_left_align(self) -> Formatter<W, WithLeftAlign<O>> {
|
||||||
|
Formatter {
|
||||||
|
buf: self.buf,
|
||||||
|
opts: WithLeftAlign(self.opts),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_right_align(self) -> Formatter<W, WithRightAlign<O>> {
|
||||||
|
Formatter {
|
||||||
|
buf: self.buf,
|
||||||
|
opts: WithRightAlign(self.opts),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_center_align(self) -> Formatter<W, WithCenterAlign<O>> {
|
||||||
|
Formatter {
|
||||||
|
buf: self.buf,
|
||||||
|
opts: WithCenterAlign(self.opts),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_fill<const FILL: char>(self) -> Formatter<W, WithFill<O, FILL>> {
|
||||||
|
Formatter {
|
||||||
|
buf: self.buf,
|
||||||
|
opts: WithFill(self.opts),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue