From 2f7a13cb2f8e9de1209bf2a1d5c1ad3926fad556 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Tue, 13 Sep 2022 22:23:12 +0200 Subject: [PATCH] fixes --- mono-fmt-macro/src/lib.rs | 8 +-- src/lib.rs | 6 +- src/opts.rs | 128 ++++++++++++++++---------------------- 3 files changed, 59 insertions(+), 83 deletions(-) diff --git a/mono-fmt-macro/src/lib.rs b/mono-fmt-macro/src/lib.rs index 2ba2dc1..710192d 100644 --- a/mono-fmt-macro/src/lib.rs +++ b/mono-fmt-macro/src/lib.rs @@ -130,9 +130,9 @@ where impl ToTokens for Alignment { fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) { tokens.extend(match self { - Self::Center => quote! { WithCenterAlign }, - Self::Left => quote! { WithLeftAlign }, - Self::Right => quote! { WithRightAlign }, + Self::Left => quote! { 1 }, + Self::Center => quote! { 2 }, + Self::Right => quote! { 3 }, }) } } @@ -154,7 +154,7 @@ impl ToTokens for FmtPart { opt_toks = quote! { #crate_ident::_private::WithFill<#opt_toks, #fill> }; } let alignment = align.kind; - opt_toks = quote! { #crate_ident::_private::#alignment<#opt_toks> }; + opt_toks = quote! { #crate_ident::_private::WithAlign<#opt_toks, #alignment> }; } if spec.alternate { diff --git a/src/lib.rs b/src/lib.rs index 23b485e..3749d4c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -122,7 +122,7 @@ mod _private { pub use crate::{ args::{ConstWidthArg, DebugArg, DisplayArg, Str}, opts::{ - WithAlternate, WithCenterAlign, WithFill, WithLeftAlign, WithRightAlign, WithWidth, + WithAlternate, WithFill, WithAlign, WithWidth, }, }; } @@ -168,7 +168,3 @@ mod tests { assert_eq!(result, "a: 32523532"); } } - -fn f() { - crate::format!("uwu, {f:_ $ret:ty { $($default:tt)* } + + struct $with_name:ident$(<$(const $const_gen_name:ident: $with_ty:ty),*>)? { + $($struct_body:tt)* + } )* ) => { pub trait FmtOpts { @@ -29,7 +33,7 @@ macro_rules! options { } impl FmtOpts for () { - type Inner = never::Never; + type Inner = Self; $( #[inline] @@ -47,6 +51,18 @@ macro_rules! options { } )* } + + $( + pub struct $with_name(#[doc(hidden)] pub I); + + impl FmtOpts for $with_name { + type Inner = I; + + fn $name() -> $ret { + $($struct_body)* + } + } + )* }; } @@ -54,113 +70,77 @@ options!( fn alternate() -> bool { false } + struct WithAlternate { + true + } fn width() -> Option { None } + struct WithWidth { + Some(A) + } fn align() -> Alignment { Alignment::Unknown } + struct WithAlign { + match A { + 0 => Alignment::Unknown, + 1 => Alignment::Left, + 2 => Alignment::Center, + 3 => Alignment::Right, + _ => panic!("invalid alignment number {A}") + } + } + fn fill() -> char { ' ' } + struct WithFill { + A + } fn sign_plus() -> bool { false } + struct WithSignPlus { + true + } fn sign_aware_zero_pad() -> bool { false } + struct WithSignAwareZeroPad { + true + } fn sign_minus() -> bool { false } + struct WithMinus { + true + } fn precision() -> Option { None } + struct WithPrecision { + Some(A) + } fn debug_lower_hex() -> bool { false } + struct WithDebugLowerHex { + true + } fn debug_upper_hex() -> bool { false } + struct WithDebugUpperHex { + false + } ); - -mod never { - use crate::FmtOpts; - - pub trait Func { - type Output; - } - - impl Func for fn() -> T { - type Output = T; - } - - pub type Never = ! as Func>::Output; - - impl FmtOpts for Never { - type Inner = Self; - } -} - -pub struct WithAlternate(pub I); - -impl FmtOpts for WithAlternate { - type Inner = I; - #[inline] - fn alternate() -> bool { - true - } -} -pub struct WithWidth(pub I); - -impl FmtOpts for WithWidth { - type Inner = I; - #[inline] - fn width() -> Option { - Some(A) - } -} -pub struct WithLeftAlign(pub I); - -impl FmtOpts for WithLeftAlign { - type Inner = I; - #[inline] - fn align() -> Alignment { - Alignment::Left - } -} -pub struct WithRightAlign(pub I); - -impl FmtOpts for WithRightAlign { - type Inner = I; - #[inline] - fn align() -> Alignment { - Alignment::Right - } -} -pub struct WithCenterAlign(pub I); - -impl FmtOpts for WithCenterAlign { - type Inner = I; - #[inline] - fn align() -> Alignment { - Alignment::Center - } -} -pub struct WithFill(pub I); - -impl FmtOpts for WithFill { - type Inner = I; - #[inline] - fn fill() -> char { - A - } -}