Skip to content

Commit

Permalink
Make formatting only available with alloc or std feature, not wit…
Browse files Browse the repository at this point in the history
…h `test`
  • Loading branch information
pitdicker committed Jun 30, 2023
1 parent 697f5d7 commit 72e1fba
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 49 deletions.
8 changes: 4 additions & 4 deletions src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! ISO 8601 calendar date with time zone.
#![allow(deprecated)]

#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
use core::borrow::Borrow;
use core::cmp::Ordering;
use core::ops::{Add, AddAssign, Sub, SubAssign};
Expand All @@ -15,7 +15,7 @@ use rkyv::{Archive, Deserialize, Serialize};

#[cfg(feature = "unstable-locales")]
use crate::format::Locale;
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
use crate::format::{DelayedFormat, Item, StrftimeItems};
use crate::naive::{IsoWeek, NaiveDate, NaiveTime};
use crate::offset::{TimeZone, Utc};
Expand Down Expand Up @@ -333,7 +333,7 @@ where
Tz::Offset: fmt::Display,
{
/// Formats the date with the specified formatting items.
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
#[inline]
#[must_use]
Expand All @@ -348,7 +348,7 @@ where
/// Formats the date with the specified format string.
/// See the [`crate::format::strftime`] module
/// on the supported escape sequences.
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
#[inline]
#[must_use]
Expand Down
12 changes: 6 additions & 6 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::string::ToString;
#[cfg(feature = "std")]
use std::time::{SystemTime, UNIX_EPOCH};

#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
use crate::format::DelayedFormat;
#[cfg(feature = "unstable-locales")]
use crate::format::Locale;
Expand Down Expand Up @@ -707,7 +707,7 @@ where
///
/// Panics if the date can not be represented in this format: the year may not be negative and
/// can not have more than 4 digits.
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
#[must_use]
pub fn to_rfc2822(&self) -> String {
Expand All @@ -718,7 +718,7 @@ where
}

/// Returns an RFC 3339 and ISO 8601 date and time string such as `1996-12-19T16:39:57-08:00`.
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
#[must_use]
pub fn to_rfc3339(&self) -> String {
Expand Down Expand Up @@ -752,7 +752,7 @@ where
/// assert_eq!(dt.to_rfc3339_opts(SecondsFormat::Secs, true),
/// "2018-01-26T10:30:09+08:00");
/// ```
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
#[must_use]
pub fn to_rfc3339_opts(&self, secform: SecondsFormat, use_z: bool) -> String {
Expand Down Expand Up @@ -798,7 +798,7 @@ where
}

/// Formats the combined date and time with the specified formatting items.
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
#[inline]
#[must_use]
Expand All @@ -823,7 +823,7 @@ where
/// let formatted = format!("{}", date_time.format("%d/%m/%Y %H:%M"));
/// assert_eq!(formatted, "02/04/2017 12:50");
/// ```
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
#[inline]
#[must_use]
Expand Down
38 changes: 19 additions & 19 deletions src/format/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ extern crate alloc;

#[cfg(feature = "alloc")]
use alloc::string::{String, ToString};
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
use core::borrow::Borrow;
use core::fmt;
use core::fmt::Write;

#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
use crate::naive::{NaiveDate, NaiveTime};
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
use crate::offset::{FixedOffset, Offset};
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
use crate::{Datelike, Timelike, Weekday};

#[cfg(feature = "unstable-locales")]
use super::locales;
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
use super::{Fixed, InternalFixed, InternalInternal, Item, Locale, Numeric, Pad};

#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
struct Locales {
short_months: &'static [&'static str],
long_months: &'static [&'static str],
Expand All @@ -34,7 +34,7 @@ struct Locales {
am_pm: &'static [&'static str],
}

#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
impl Locales {
fn new(_locale: Option<Locale>) -> Self {
#[cfg(feature = "unstable-locales")]
Expand Down Expand Up @@ -84,7 +84,7 @@ impl Locales {

/// A *temporary* object which can be used as an argument to `format!` or others.
/// This is normally constructed via `format` methods of each date and time type.
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
#[derive(Debug)]
pub struct DelayedFormat<I> {
Expand All @@ -103,7 +103,7 @@ pub struct DelayedFormat<I> {
locale: Option<Locale>,
}

#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
impl<'a, I: Iterator<Item = B> + Clone, B: Borrow<Item<'a>>> DelayedFormat<I> {
/// Makes a new `DelayedFormat` value out of local date and time.
#[must_use]
Expand Down Expand Up @@ -172,7 +172,7 @@ impl<'a, I: Iterator<Item = B> + Clone, B: Borrow<Item<'a>>> DelayedFormat<I> {
}
}

#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
impl<'a, I: Iterator<Item = B> + Clone, B: Borrow<Item<'a>>> fmt::Display for DelayedFormat<I> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
#[cfg(feature = "unstable-locales")]
Expand All @@ -195,7 +195,7 @@ impl<'a, I: Iterator<Item = B> + Clone, B: Borrow<Item<'a>>> fmt::Display for De

/// Tries to format given arguments with given formatting items.
/// Internally used by `DelayedFormat`.
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
pub fn format<'a, I, B>(
w: &mut fmt::Formatter,
Expand All @@ -215,7 +215,7 @@ where
w.pad(&result)
}
/// Formats single formatting item
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
pub fn format_item(
w: &mut fmt::Formatter,
Expand Down Expand Up @@ -268,7 +268,7 @@ pub fn format_item_localized(
w.pad(&result)
}

#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
fn format_inner(
result: &mut String,
date: Option<&NaiveDate>,
Expand All @@ -281,7 +281,7 @@ fn format_inner(

match *item {
Item::Literal(s) | Item::Space(s) => result.push_str(s),
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
Item::OwnedLiteral(ref s) | Item::OwnedSpace(ref s) => result.push_str(s),

Item::Numeric(ref spec, ref pad) => {
Expand Down Expand Up @@ -476,7 +476,7 @@ fn format_inner(
Ok(())
}

#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum Colons {
None,
Expand All @@ -487,7 +487,7 @@ enum Colons {

/// Prints an offset from UTC in the format of `+HHMM` or `+HH:MM`.
/// `Z` instead of `+00[:]00` is allowed when `allow_zulu` is true.
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
fn write_local_minus_utc(
result: &mut String,
off: FixedOffset,
Expand Down Expand Up @@ -521,7 +521,7 @@ fn write_local_minus_utc(
}

/// Writes the date, time and offset to the string. same as `%Y-%m-%dT%H:%M:%S%.f%:z`
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
pub(crate) fn write_rfc3339(
result: &mut String,
dt: crate::NaiveDateTime,
Expand All @@ -533,7 +533,7 @@ pub(crate) fn write_rfc3339(
write_local_minus_utc(result, off, false, Colons::Single)
}

#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
/// write datetimes like `Tue, 1 Jul 2003 10:52:37 +0200`, same as `%a, %d %b %Y %H:%M:%S %z`
pub(crate) fn write_rfc2822(
result: &mut String,
Expand All @@ -543,7 +543,7 @@ pub(crate) fn write_rfc2822(
write_rfc2822_inner(result, &dt.date(), &dt.time(), off, Locales::new(None))
}

#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
/// write datetimes like `Tue, 1 Jul 2003 10:52:37 +0200`, same as `%a, %d %b %Y %H:%M:%S %z`
fn write_rfc2822_inner(
result: &mut String,
Expand Down
8 changes: 4 additions & 4 deletions src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ pub mod strftime;
pub(crate) mod locales;

pub(crate) use formatting::write_hundreds;
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
pub use formatting::{format, format_item, DelayedFormat};
#[cfg(feature = "unstable-locales")]
pub use formatting::{format_item_localized, format_localized};
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
pub(crate) use formatting::{write_rfc2822, write_rfc3339};
pub use parse::{parse, parse_and_remainder};
pub use parsed::Parsed;
Expand Down Expand Up @@ -286,13 +286,13 @@ pub enum Item<'a> {
/// A literally printed and parsed text.
Literal(&'a str),
/// Same as `Literal` but with the string owned by the item.
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
OwnedLiteral(Box<str>),
/// Whitespace. Prints literally but reads zero or more whitespace.
Space(&'a str),
/// Same as `Space` but with the string owned by the item.
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
OwnedSpace(Box<str>),
/// Numeric item. Can be optionally padded to the maximal length (if any) when formatting;
Expand Down
7 changes: 3 additions & 4 deletions src/format/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ where
s = &s[prefix.len()..];
}

#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
Item::OwnedLiteral(ref prefix) => {
if s.len() < prefix.len() {
return Err((s, TOO_SHORT));
Expand All @@ -324,7 +324,7 @@ where
s = s.trim_start();
}

#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
Item::OwnedSpace(_) => {
s = s.trim_start();
}
Expand Down Expand Up @@ -1349,12 +1349,11 @@ mod tests {
fn parse_rfc850() {
static RFC850_FMT: &str = "%A, %d-%b-%y %T GMT";

let dt_str = "Sunday, 06-Nov-94 08:49:37 GMT";
let dt = Utc.with_ymd_and_hms(1994, 11, 6, 8, 49, 37).unwrap();

// Check that the format is what we expect
#[cfg(any(feature = "alloc", feature = "std"))]
assert_eq!(dt.format(RFC850_FMT).to_string(), dt_str);
assert_eq!(dt.format(RFC850_FMT).to_string(), "Sunday, 06-Nov-94 08:49:37 GMT");

// Check that it parses correctly
assert_eq!(Ok(dt), Utc.datetime_from_str("Sunday, 06-Nov-94 08:49:37 GMT", RFC850_FMT));
Expand Down
8 changes: 4 additions & 4 deletions src/naive/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

//! ISO 8601 calendar date without timezone.

#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
use core::borrow::Borrow;
use core::iter::FusedIterator;
use core::ops::{Add, AddAssign, RangeInclusive, Sub, SubAssign};
Expand All @@ -16,7 +16,7 @@ use rkyv::{Archive, Deserialize, Serialize};
#[cfg(feature = "unstable-locales")]
use pure_rust_locales::Locale;

#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
use crate::format::DelayedFormat;
use crate::format::{
parse, parse_and_remainder, write_hundreds, Item, Numeric, Pad, ParseError, ParseResult,
Expand Down Expand Up @@ -1246,7 +1246,7 @@ impl NaiveDate {
/// # let d = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap();
/// assert_eq!(format!("{}", d.format_with_items(fmt)), "2015-09-05");
/// ```
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
#[inline]
#[must_use]
Expand Down Expand Up @@ -1290,7 +1290,7 @@ impl NaiveDate {
/// assert_eq!(format!("{}", d.format("%Y-%m-%d")), "2015-09-05");
/// assert_eq!(format!("{}", d.format("%A, %-d %B, %C%y")), "Saturday, 5 September, 2015");
/// ```
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
#[inline]
#[must_use]
Expand Down
8 changes: 4 additions & 4 deletions src/naive/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

//! ISO 8601 date and time without timezone.

#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
use core::borrow::Borrow;
use core::fmt::Write;
use core::ops::{Add, AddAssign, Sub, SubAssign};
Expand All @@ -12,7 +12,7 @@ use core::{fmt, str};
#[cfg(feature = "rkyv")]
use rkyv::{Archive, Deserialize, Serialize};

#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
use crate::format::DelayedFormat;
use crate::format::{parse, parse_and_remainder, ParseError, ParseResult, Parsed, StrftimeItems};
use crate::format::{Fixed, Item, Numeric, Pad};
Expand Down Expand Up @@ -855,7 +855,7 @@ impl NaiveDateTime {
/// # let dt = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap().and_hms_opt(23, 56, 4).unwrap();
/// assert_eq!(format!("{}", dt.format_with_items(fmt)), "2015-09-05 23:56:04");
/// ```
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
#[inline]
#[must_use]
Expand Down Expand Up @@ -899,7 +899,7 @@ impl NaiveDateTime {
/// assert_eq!(format!("{}", dt.format("%Y-%m-%d %H:%M:%S")), "2015-09-05 23:56:04");
/// assert_eq!(format!("{}", dt.format("around %l %p on %b %-d")), "around 11 PM on Sep 5");
/// ```
#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "alloc", feature = "std"))))]
#[inline]
#[must_use]
Expand Down

0 comments on commit 72e1fba

Please sign in to comment.