Skip to content

Commit 6f2c7cc

Browse files
committed
Use different method to run feature-dependent doctests
1 parent 61d54b5 commit 6f2c7cc

File tree

8 files changed

+41
-28
lines changed

8 files changed

+41
-28
lines changed

src/datetime/mod.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ impl<Tz: TimeZone> DateTime<Tz> {
7979
///
8080
/// # Example
8181
///
82-
#[cfg_attr(not(feature = "clock"), doc = "```ignore")]
83-
#[cfg_attr(feature = "clock", doc = "```rust")]
82+
/// ```
83+
/// # #[cfg(feature = "clock")] {
8484
/// use chrono::{DateTime, Local};
8585
///
8686
/// let dt = Local::now();
@@ -90,6 +90,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
9090
/// // Serialize, pass through FFI... and recreate the `DateTime`:
9191
/// let dt_new = DateTime::<Local>::from_naive_utc_and_offset(naive_utc, offset);
9292
/// assert_eq!(dt, dt_new);
93+
/// # }
9394
/// ```
9495
#[inline]
9596
#[must_use]
@@ -691,8 +692,8 @@ impl<Tz: TimeZone> DateTime<Tz> {
691692
///
692693
/// # Example
693694
///
694-
#[cfg_attr(not(feature = "clock"), doc = "```ignore")]
695-
#[cfg_attr(feature = "clock", doc = "```rust")]
695+
/// ```
696+
/// # #[cfg(feature = "clock")] {
696697
/// use chrono::{Local, NaiveTime};
697698
///
698699
/// let noon = NaiveTime::from_hms_opt(12, 0, 0).unwrap();
@@ -701,6 +702,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
701702
///
702703
/// assert_eq!(today_noon.single().unwrap().time(), noon);
703704
/// assert_eq!(today_midnight.single().unwrap().time(), NaiveTime::MIN);
705+
/// # }
704706
/// ```
705707
#[must_use]
706708
pub fn with_time(&self, time: NaiveTime) -> LocalResult<Self> {

src/format/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
//! C's `strftime` format. The available options can be found [here](./strftime/index.html).
1717
//!
1818
//! # Example
19-
#![cfg_attr(not(feature = "std"), doc = "```ignore")]
20-
#![cfg_attr(feature = "std", doc = "```rust")]
19+
//! ```
20+
//! # #[cfg(feature = "std")] {
2121
//! use chrono::{NaiveDateTime, TimeZone, Utc};
2222
//!
2323
//! let date_time = Utc.with_ymd_and_hms(2020, 11, 10, 0, 1, 32).unwrap();
@@ -27,6 +27,7 @@
2727
//!
2828
//! let parsed = NaiveDateTime::parse_from_str(&formatted, "%Y-%m-%d %H:%M:%S")?.and_utc();
2929
//! assert_eq!(parsed, date_time);
30+
//! # }
3031
//! # Ok::<(), chrono::ParseError>(())
3132
//! ```
3233

src/format/parsed.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ use crate::{DateTime, Datelike, TimeDelta, Timelike, Weekday};
5757
///
5858
/// Let's see how `Parsed` correctly detects the second RFC 2822 string from before is inconsistent.
5959
///
60-
#[cfg_attr(not(feature = "alloc"), doc = "```ignore")]
61-
#[cfg_attr(feature = "alloc", doc = "```rust")]
60+
/// ```
61+
/// # #[cfg(feature = "alloc")] {
6262
/// use chrono::format::{ParseErrorKind, Parsed};
6363
/// use chrono::Weekday;
6464
///
@@ -89,6 +89,7 @@ use crate::{DateTime, Datelike, TimeDelta, Timelike, Weekday};
8989
/// if let Err(error) = result {
9090
/// assert_eq!(error.kind(), ParseErrorKind::Impossible);
9191
/// }
92+
/// # }
9293
/// # Ok::<(), chrono::ParseError>(())
9394
/// ```
9495
///
@@ -98,8 +99,8 @@ use crate::{DateTime, Datelike, TimeDelta, Timelike, Weekday};
9899
/// [RFC2822 formatting item]: crate::format::Fixed::RFC2822
99100
/// [`format::parse()`]: crate::format::parse()
100101
///
101-
#[cfg_attr(not(feature = "alloc"), doc = "```ignore")]
102-
#[cfg_attr(feature = "alloc", doc = "```rust")]
102+
/// ```
103+
/// # #[cfg(feature = "alloc")] {
103104
/// use chrono::format::{parse, Fixed, Item, Parsed};
104105
/// use chrono::Weekday;
105106
///
@@ -120,6 +121,7 @@ use crate::{DateTime, Datelike, TimeDelta, Timelike, Weekday};
120121
/// // What is the weekday?
121122
/// assert_eq!(parsed.weekday(), Some(Weekday::Thu));
122123
/// }
124+
/// # }
123125
/// # Ok::<(), chrono::ParseError>(())
124126
/// ```
125127
#[allow(clippy::manual_non_exhaustive)]

src/format/strftime.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,8 @@ impl<'a> StrftimeItems<'a> {
258258
///
259259
/// # Example
260260
///
261-
#[cfg_attr(not(any(feature = "alloc", feature = "std")), doc = "```ignore")]
262-
#[cfg_attr(any(feature = "alloc", feature = "std"), doc = "```rust")]
261+
/// ```
262+
/// # #[cfg(feature = "alloc")] {
263263
/// use chrono::format::{Locale, StrftimeItems};
264264
/// use chrono::{FixedOffset, TimeZone};
265265
///

src/lib.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,22 @@
108108
//! or in the local time zone
109109
//! ([`Local::now()`](./offset/struct.Local.html#method.now)).
110110
//!
111-
#![cfg_attr(not(feature = "now"), doc = "```ignore")]
112-
#![cfg_attr(feature = "now", doc = "```rust")]
111+
//! ```
112+
//! # #[cfg(feature = "now")] {
113113
//! use chrono::prelude::*;
114114
//!
115115
//! let utc: DateTime<Utc> = Utc::now(); // e.g. `2014-11-28T12:45:59.324310806Z`
116116
//! # let _ = utc;
117+
//! # }
117118
//! ```
118119
//!
119-
#![cfg_attr(not(feature = "clock"), doc = "```ignore")]
120-
#![cfg_attr(feature = "clock", doc = "```rust")]
120+
//! ```
121+
//! # #[cfg(feature = "clock")] {
121122
//! use chrono::prelude::*;
122123
//!
123124
//! let local: DateTime<Local> = Local::now(); // e.g. `2014-11-28T21:45:59.324310806+09:00`
124125
//! # let _ = local;
126+
//! # }
125127
//! ```
126128
//!
127129
//! Alternatively, you can create your own date and time.
@@ -382,8 +384,8 @@
382384
//! [`DateTime.timestamp_subsec_nanos`](DateTime::timestamp_subsec_nanos)
383385
//! to get the number of additional number of nanoseconds.
384386
//!
385-
#![cfg_attr(not(feature = "std"), doc = "```ignore")]
386-
#![cfg_attr(feature = "std", doc = "```rust")]
387+
//! ```
388+
//! # #[cfg(feature = "std")] {
387389
//! // We need the trait in scope to use Utc::timestamp().
388390
//! use chrono::{DateTime, Utc};
389391
//!
@@ -394,6 +396,7 @@
394396
//! // Get epoch value from a datetime:
395397
//! let dt = DateTime::parse_from_rfc2822("Fri, 14 Jul 2017 02:40:00 +0000").unwrap();
396398
//! assert_eq!(dt.timestamp(), 1_500_000_000);
399+
//! # }
397400
//! ```
398401
//!
399402
//! ### Naive date and time

src/naive/time/mod.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -948,12 +948,13 @@ impl Timelike for NaiveTime {
948948
/// ([Why?](#leap-second-handling))
949949
/// Use the proper [formatting method](#method.format) to get a human-readable representation.
950950
///
951-
#[cfg_attr(not(feature = "std"), doc = "```ignore")]
952-
#[cfg_attr(feature = "std", doc = "```")]
951+
/// ```
952+
/// # #[cfg(feature = "std")] {
953953
/// # use chrono::{NaiveTime, Timelike};
954954
/// let leap = NaiveTime::from_hms_milli_opt(23, 59, 59, 1_000).unwrap();
955955
/// assert_eq!(leap.second(), 59);
956956
/// assert_eq!(leap.format("%H:%M:%S").to_string(), "23:59:60");
957+
/// # }
957958
/// ```
958959
#[inline]
959960
fn second(&self) -> u32 {
@@ -980,12 +981,13 @@ impl Timelike for NaiveTime {
980981
/// You can reduce the range with `time.nanosecond() % 1_000_000_000`, or
981982
/// use the proper [formatting method](#method.format) to get a human-readable representation.
982983
///
983-
#[cfg_attr(not(feature = "std"), doc = "```ignore")]
984-
#[cfg_attr(feature = "std", doc = "```")]
984+
/// ```
985+
/// # #[cfg(feature = "std")] {
985986
/// # use chrono::{NaiveTime, Timelike};
986987
/// let leap = NaiveTime::from_hms_milli_opt(23, 59, 59, 1_000).unwrap();
987988
/// assert_eq!(leap.nanosecond(), 1_000_000_000);
988989
/// assert_eq!(leap.format("%H:%M:%S%.9f").to_string(), "23:59:60.000000000");
990+
/// # }
989991
/// ```
990992
#[inline]
991993
fn nanosecond(&self) -> u32 {

src/offset/fixed.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,14 @@ impl FixedOffset {
4949
///
5050
/// # Example
5151
///
52-
#[cfg_attr(not(feature = "std"), doc = "```ignore")]
53-
#[cfg_attr(feature = "std", doc = "```")]
52+
/// ```
53+
/// # #[cfg(feature = "std")] {
5454
/// use chrono::{FixedOffset, TimeZone};
5555
/// let hour = 3600;
5656
/// let datetime =
5757
/// FixedOffset::east_opt(5 * hour).unwrap().with_ymd_and_hms(2016, 11, 08, 0, 0, 0).unwrap();
5858
/// assert_eq!(&datetime.to_rfc3339(), "2016-11-08T00:00:00+05:00")
59+
/// # }
5960
/// ```
6061
#[must_use]
6162
pub const fn east_opt(secs: i32) -> Option<FixedOffset> {
@@ -83,13 +84,14 @@ impl FixedOffset {
8384
///
8485
/// # Example
8586
///
86-
#[cfg_attr(not(feature = "std"), doc = "```ignore")]
87-
#[cfg_attr(feature = "std", doc = "```")]
87+
/// ```
88+
/// # #[cfg(feature = "std")] {
8889
/// use chrono::{FixedOffset, TimeZone};
8990
/// let hour = 3600;
9091
/// let datetime =
9192
/// FixedOffset::west_opt(5 * hour).unwrap().with_ymd_and_hms(2016, 11, 08, 0, 0, 0).unwrap();
9293
/// assert_eq!(&datetime.to_rfc3339(), "2016-11-08T00:00:00-05:00")
94+
/// # }
9395
/// ```
9496
#[must_use]
9597
pub const fn west_opt(secs: i32) -> Option<FixedOffset> {

src/weekday.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,16 @@ impl Weekday {
123123
///
124124
/// # Example
125125
///
126-
#[cfg_attr(not(feature = "clock"), doc = "```ignore")]
127-
#[cfg_attr(feature = "clock", doc = "```rust")]
126+
/// ```
127+
/// # #[cfg(feature = "clock")] {
128128
/// # use chrono::{Local, Datelike};
129129
/// // MTWRFSU is occasionally used as a single-letter abbreviation of the weekdays.
130130
/// // Use `num_days_from_monday` to index into the array.
131131
/// const MTWRFSU: [char; 7] = ['M', 'T', 'W', 'R', 'F', 'S', 'U'];
132132
///
133133
/// let today = Local::now().weekday();
134134
/// println!("{}", MTWRFSU[today.num_days_from_monday() as usize]);
135+
/// # }
135136
/// ```
136137
#[inline]
137138
pub const fn num_days_from_monday(&self) -> u32 {

0 commit comments

Comments
 (0)