From 8a912b56087b5f08a62cfb862d3600908f14428e Mon Sep 17 00:00:00 2001 From: Mathspy Date: Mon, 6 Dec 2021 18:28:57 +0200 Subject: [PATCH 01/12] Add io::Write -> fmt::Write compatibility type --- src/formatting/formattable.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/formatting/formattable.rs b/src/formatting/formattable.rs index da2db0e14..042f46b25 100644 --- a/src/formatting/formattable.rs +++ b/src/formatting/formattable.rs @@ -1,7 +1,7 @@ //! A trait that can be used to format an item from its components. use core::ops::Deref; -use std::io; +use std::{fmt, io}; use crate::format_description::well_known::{Rfc2822, Rfc3339}; use crate::format_description::FormatItem; @@ -19,6 +19,27 @@ impl Formattable for Rfc3339 {} impl Formattable for Rfc2822 {} impl Formattable for T where T::Target: Formattable {} +struct Compat<'a, W: io::Write> { + writer: &'a mut W, + bytes_written: usize, + error: Option, +} + +impl<'a, W> fmt::Write for Compat<'a, W> +where + W: io::Write, +{ + fn write_str(&mut self, s: &str) -> fmt::Result { + self.writer + .write_all(s.as_bytes()) + .map(|_| self.bytes_written += s.len()) + .map_err(|error| { + self.error = Some(error); + fmt::Error + }) + } +} + /// Seal the trait to prevent downstream users from implementing it. mod sealed { #[allow(clippy::wildcard_imports)] From eeaf28a17ff4bd515dbc880cad22b25140ae921b Mon Sep 17 00:00:00 2001 From: Mathspy Date: Mon, 6 Dec 2021 18:31:44 +0200 Subject: [PATCH 02/12] Replace old format_into and give it a default impl --- src/error/format.rs | 10 ++++++++++ src/formatting/formattable.rs | 31 +++++++++++++++++++++++++++---- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/error/format.rs b/src/error/format.rs index 723a4eb82..ff5cd09a6 100644 --- a/src/error/format.rs +++ b/src/error/format.rs @@ -21,6 +21,8 @@ pub enum Format { InvalidComponent(&'static str), /// A value of `std::io::Error` was returned internally. StdIo(io::Error), + /// A value of `std::fmt::Error` was returned internally. + StdFmt(fmt::Error), } impl fmt::Display for Format { @@ -36,6 +38,7 @@ impl fmt::Display for Format { component ), Self::StdIo(err) => err.fmt(f), + Self::StdFmt(err) => err.fmt(f), } } } @@ -46,6 +49,12 @@ impl From for Format { } } +impl From for Format { + fn from(err: fmt::Error) -> Self { + Self::StdFmt(err) + } +} + impl TryFrom for io::Error { type Error = error::DifferentVariant; @@ -63,6 +72,7 @@ impl std::error::Error for Format { match *self { Self::InsufficientTypeInformation | Self::InvalidComponent(_) => None, Self::StdIo(ref err) => Some(err), + Self::StdFmt(ref err) => Some(err), } } } diff --git a/src/formatting/formattable.rs b/src/formatting/formattable.rs index 042f46b25..9e875c684 100644 --- a/src/formatting/formattable.rs +++ b/src/formatting/formattable.rs @@ -48,14 +48,37 @@ mod sealed { /// Format the item using a format description, the intended output, and the various components. #[cfg_attr(__time_03_docs, doc(cfg(feature = "formatting")))] pub trait Sealed { - /// Format the item into the provided output, returning the number of bytes written. + /// Format the item into the provided output. fn format_into( + &self, + output: &mut impl fmt::Write, + date: Option, + time: Option