Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate standalone format functions #1306

Merged
merged 3 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 58 additions & 32 deletions src/format/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
use alloc::string::{String, ToString};
#[cfg(any(feature = "alloc", feature = "std"))]
use core::borrow::Borrow;
use core::fmt;
use core::fmt::Write;
#[cfg(any(feature = "alloc", feature = "std"))]
use core::fmt::Display;
use core::fmt::{self, Write};

#[cfg(any(
feature = "alloc",
Expand Down Expand Up @@ -87,7 +88,7 @@
items: I,
) -> DelayedFormat<I>
where
Off: Offset + fmt::Display,
Off: Offset + Display,
{
let name_and_diff = (offset.to_string(), offset.fix());
DelayedFormat {
Expand Down Expand Up @@ -123,37 +124,40 @@
locale: Locale,
) -> DelayedFormat<I>
where
Off: Offset + fmt::Display,
Off: Offset + Display,
{
let name_and_diff = (offset.to_string(), offset.fix());
DelayedFormat { date, time, off: Some(name_and_diff), items, locale: Some(locale) }
}
}

#[cfg(any(feature = "alloc", feature = "std"))]
impl<'a, I: Iterator<Item = B> + Clone, B: Borrow<Item<'a>>> fmt::Display for DelayedFormat<I> {
impl<'a, I: Iterator<Item = B> + Clone, B: Borrow<Item<'a>>> Display for DelayedFormat<I> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
#[cfg(feature = "unstable-locales")]
{
if let Some(locale) = self.locale {
return format_localized(
f,
self.date.as_ref(),
self.time.as_ref(),
self.off.as_ref(),
self.items.clone(),
locale,
);
}
let locale = self.locale;
#[cfg(not(feature = "unstable-locales"))]
let locale = None;

let mut result = String::new();
for item in self.items.clone() {
format_inner(
&mut result,
self.date.as_ref(),
self.time.as_ref(),
self.off.as_ref(),
item.borrow(),
locale,
)?;
}

format(f, self.date.as_ref(), self.time.as_ref(), self.off.as_ref(), self.items.clone())
f.pad(&result)
}
}

/// Tries to format given arguments with given formatting items.
/// Internally used by `DelayedFormat`.
#[cfg(any(feature = "alloc", feature = "std"))]
#[deprecated(since = "0.4.32", note = "Use DelayedFormat::fmt instead")]
pub fn format<'a, I, B>(
w: &mut fmt::Formatter,
date: Option<&NaiveDate>,
Expand All @@ -165,29 +169,42 @@
I: Iterator<Item = B> + Clone,
B: Borrow<Item<'a>>,
{
let mut result = String::new();
for item in items {
format_inner(&mut result, date, time, off, item.borrow(), None)?;
DelayedFormat {
date: date.copied(),
time: time.copied(),
off: off.cloned(),
items,
#[cfg(feature = "unstable-locales")]
locale: None,

Check warning on line 178 in src/format/formatting.rs

View check run for this annotation

Codecov / codecov/patch

src/format/formatting.rs#L172-L178

Added lines #L172 - L178 were not covered by tests
}
w.pad(&result)
.fmt(w)

Check warning on line 180 in src/format/formatting.rs

View check run for this annotation

Codecov / codecov/patch

src/format/formatting.rs#L180

Added line #L180 was not covered by tests
}

/// Formats single formatting item
#[cfg(any(feature = "alloc", feature = "std"))]
#[deprecated(since = "0.4.32", note = "Use DelayedFormat::fmt instead")]
pub fn format_item(
w: &mut fmt::Formatter,
date: Option<&NaiveDate>,
time: Option<&NaiveTime>,
off: Option<&(String, FixedOffset)>,
item: &Item<'_>,
) -> fmt::Result {
let mut result = String::new();
format_inner(&mut result, date, time, off, item, None)?;
w.pad(&result)
DelayedFormat {
date: date.copied(),
time: time.copied(),
off: off.cloned(),
items: [item].into_iter(),
#[cfg(feature = "unstable-locales")]
locale: None,
}
.fmt(w)

Check warning on line 201 in src/format/formatting.rs

View check run for this annotation

Codecov / codecov/patch

src/format/formatting.rs#L193-L201

Added lines #L193 - L201 were not covered by tests
}

/// Tries to format given arguments with given formatting items.
/// Internally used by `DelayedFormat`.
#[cfg(feature = "unstable-locales")]
#[deprecated(since = "0.4.32", note = "Use DelayedFormat::fmt instead")]
pub fn format_localized<'a, I, B>(
w: &mut fmt::Formatter,
date: Option<&NaiveDate>,
Expand All @@ -200,15 +217,19 @@
I: Iterator<Item = B> + Clone,
B: Borrow<Item<'a>>,
{
let mut result = String::new();
for item in items {
format_inner(&mut result, date, time, off, item.borrow(), Some(locale))?;
DelayedFormat {
date: date.copied(),
time: time.copied(),
off: off.cloned(),
items,
locale: Some(locale),

Check warning on line 225 in src/format/formatting.rs

View check run for this annotation

Codecov / codecov/patch

src/format/formatting.rs#L220-L225

Added lines #L220 - L225 were not covered by tests
}
w.pad(&result)
.fmt(w)

Check warning on line 227 in src/format/formatting.rs

View check run for this annotation

Codecov / codecov/patch

src/format/formatting.rs#L227

Added line #L227 was not covered by tests
}

/// Formats single formatting item
#[cfg(feature = "unstable-locales")]
#[deprecated(since = "0.4.32", note = "Use DelayedFormat::fmt instead")]
pub fn format_item_localized(
w: &mut fmt::Formatter,
date: Option<&NaiveDate>,
Expand All @@ -217,9 +238,14 @@
item: &Item<'_>,
locale: Locale,
) -> fmt::Result {
let mut result = String::new();
format_inner(&mut result, date, time, off, item, Some(locale))?;
w.pad(&result)
DelayedFormat {
date: date.copied(),
time: time.copied(),
off: off.cloned(),
items: [item].into_iter(),
locale: Some(locale),
}
.fmt(w)

Check warning on line 248 in src/format/formatting.rs

View check run for this annotation

Codecov / codecov/patch

src/format/formatting.rs#L241-L248

Added lines #L241 - L248 were not covered by tests
}

#[cfg(any(feature = "alloc", feature = "std"))]
Expand Down
2 changes: 2 additions & 0 deletions src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ pub(crate) use formatting::write_rfc2822;
))]
pub(crate) use formatting::write_rfc3339;
#[cfg(any(feature = "alloc", feature = "std"))]
#[allow(deprecated)]
pub use formatting::{format, format_item, DelayedFormat};
#[cfg(feature = "unstable-locales")]
#[allow(deprecated)]
pub use formatting::{format_item_localized, format_localized};
#[cfg(all(feature = "unstable-locales", any(feature = "alloc", feature = "std")))]
pub use locales::Locale;
Expand Down
Loading