tests pass again

This commit is contained in:
nora 2022-09-24 18:23:39 +02:00
parent 53e8bec4ec
commit d7844568ca
5 changed files with 294 additions and 56 deletions

View file

@ -80,7 +80,10 @@ impl<W: Write, O: FmtOpts> Formatter<W, O> {
}
impl<W, O: FmtOpts> Formatter<W, O> {
fn wrap_with<'opt, ONew: FmtOpts>(&mut self, opts: &ONew) -> Formatter<&mut W, ONew::ReplaceInnermost<O>> {
fn wrap_with<'opt, ONew: FmtOpts>(
&mut self,
opts: &ONew,
) -> Formatter<&mut W, ONew::ReplaceInnermost<O>> {
Formatter {
buf: &mut self.buf,
opts: opts.override_other(self.opts),
@ -88,15 +91,19 @@ impl<W, O: FmtOpts> Formatter<W, O> {
}
}
pub fn write<W: Write, A: Arguments>(buffer: W, args: A) -> Result {
let mut fmt = Formatter::new(buffer);
args.fmt(&mut fmt)
}
pub mod helpers {
use crate::{Arguments, Formatter, Result, Write};
pub fn format<A: Arguments>(args: A) -> String {
let mut string = String::new();
write(&mut string, args).unwrap();
string
pub fn write<W: Write, A: Arguments>(buffer: W, args: A) -> Result {
let mut fmt = Formatter::new(buffer);
args.fmt(&mut fmt)
}
pub fn format<A: Arguments>(args: A) -> String {
let mut string = String::new();
write(&mut string, args).unwrap();
string
}
}
/// Not part of the public API.
@ -116,7 +123,7 @@ mod _private {
#[macro_export]
macro_rules! format {
($($tt:tt)*) => {
$crate::format($crate::format_args!($($tt)*))
$crate::helpers::format($crate::format_args!($($tt)*))
};
}
@ -154,17 +161,3 @@ mod tests {
assert_eq!(result, "a: 32523532");
}
}
// testing
fn fmt() {
let a = (
_private::Str("amount: "),
_private::DebugArg::<_, _private::WithAlternate<()>>(5, _private::WithAlternate(())),
);
let mut str = String::new();
let mut f = Formatter::new(&mut str);
Arguments::fmt(&a, &mut f).unwrap();
println!("{str}");
}

View file

@ -154,7 +154,11 @@ impl<W: Write, O: FmtOpts> Formatter<W, O> {
// Writes the sign if it exists, and then the prefix if it was requested
#[inline(never)]
fn write_prefix<W: Write, O>(f: &mut Formatter<W, O>, sign: Option<char>, prefix: Option<&str>) -> Result {
fn write_prefix<W: Write, O>(
f: &mut Formatter<W, O>,
sign: Option<char>,
prefix: Option<&str>,
) -> Result {
if let Some(c) = sign {
f.buf.write_char(c)?;
}
@ -183,14 +187,16 @@ impl<W: Write, O: FmtOpts> Formatter<W, O> {
// is zero
Some(min) if self.sign_aware_zero_pad() => {
write_prefix(self, sign, prefix)?;
let post_padding = self.padding(min - width, Alignment::Right, '0', Alignment::Right)?;
let post_padding =
self.padding(min - width, Alignment::Right, '0', Alignment::Right)?;
self.buf.write_str(buf)?;
post_padding.write(self)?;
Ok(())
}
// Otherwise, the sign and prefix goes after the padding
Some(min) => {
let post_padding = self.padding(min - width, Alignment::Right, self.fill(), self.align())?;
let post_padding =
self.padding(min - width, Alignment::Right, self.fill(), self.align())?;
write_prefix(self, sign, prefix)?;
self.buf.write_str(buf)?;
post_padding.write(self)
@ -221,7 +227,7 @@ impl<W: Write, O: FmtOpts> Formatter<W, O> {
// remaining parts go through the ordinary padding process.
let len = formatted.len();
if width <= len {
// no padding
self.write_formatted_parts(&formatted)