Skip to content

Commit

Permalink
Make formatting only available with alloc or std feature
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Jun 30, 2023
1 parent 7a26827 commit 9472c8b
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 48 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
44 changes: 22 additions & 22 deletions src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ extern crate alloc;
use alloc::boxed::Box;
#[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;
use core::str::FromStr;
#[cfg(any(feature = "std", test))]
#[cfg(feature = "std")]
use std::error::Error;

#[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};
use crate::{Month, ParseMonthError, ParseWeekdayError, Weekday};

Expand Down Expand Up @@ -275,7 +275,7 @@ enum InternalInternal {
Nanosecond9NoDot,
}

#[cfg(any(feature = "alloc", feature = "std", test))]
#[cfg(any(feature = "alloc", feature = "std"))]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum Colons {
None,
Expand All @@ -290,13 +290,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 Expand Up @@ -410,7 +410,7 @@ impl fmt::Display for ParseError {
}
}

#[cfg(any(feature = "std", test))]
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl Error for ParseError {
#[allow(deprecated)]
Expand All @@ -428,7 +428,7 @@ const TOO_SHORT: ParseError = ParseError(ParseErrorKind::TooShort);
const TOO_LONG: ParseError = ParseError(ParseErrorKind::TooLong);
const BAD_FORMAT: ParseError = ParseError(ParseErrorKind::BadFormat);

#[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 @@ -437,7 +437,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 @@ -486,7 +486,7 @@ impl Locales {
}

/// 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 All @@ -500,7 +500,7 @@ pub fn format_item(
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 @@ -513,7 +513,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 @@ -710,7 +710,7 @@ fn format_inner(

/// 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 @@ -744,7 +744,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 @@ -756,7 +756,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 @@ -766,7 +766,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 Expand Up @@ -814,7 +814,7 @@ pub(crate) fn write_hundreds(w: &mut impl Write, n: u8) -> fmt::Result {

/// 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 Down Expand Up @@ -844,7 +844,7 @@ pub mod strftime;

/// 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 @@ -863,7 +863,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 @@ -932,7 +932,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 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 @@ -1345,12 +1345,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
Loading

0 comments on commit 9472c8b

Please sign in to comment.