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

Add std feature/no_std support #112

Merged
merged 4 commits into from
Dec 19, 2024
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ resolver = "2"

[workspace.package]
edition = "2021"
rust-version = "1.67"
rust-version = "1.81"
authors = ["John Thorvald Wodder II <julian-rs@varonathe.org>"]
repository = "https://github.com/jwodder/julian-rs"
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
[![CI Status](https://github.com/jwodder/julian-rs/actions/workflows/test.yml/badge.svg)](https://github.com/jwodder/julian-rs/actions/workflows/test.yml)
[![codecov.io](https://codecov.io/gh/jwodder/julian-rs/branch/master/graph/badge.svg)](https://codecov.io/gh/jwodder/julian-rs)
[![Minimum Supported Rust Version](https://img.shields.io/badge/MSRV-1.67-orange)](https://www.rust-lang.org)
[![Minimum Supported Rust Version](https://img.shields.io/badge/MSRV-1.81-orange)](https://www.rust-lang.org)
[![MIT License](https://img.shields.io/github/license/jwodder/julian-rs.svg)](https://opensource.org/licenses/MIT)

This is a Rust [workspace][] containing various packages for converting between
Expand Down
4 changes: 4 additions & 0 deletions crates/julian-cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
v0.7.0 (in development)
-----------------------
- Increased MSRV to 1.81

v0.6.0 (2024-12-18)
-------------------
- Split off CLI from `julian` crate
2 changes: 1 addition & 1 deletion crates/julian-cli/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
[![CI Status](https://github.com/jwodder/julian-rs/actions/workflows/test.yml/badge.svg)](https://github.com/jwodder/julian-rs/actions/workflows/test.yml)
[![codecov.io](https://codecov.io/gh/jwodder/julian-rs/branch/master/graph/badge.svg)](https://codecov.io/gh/jwodder/julian-rs)
[![Minimum Supported Rust Version](https://img.shields.io/badge/MSRV-1.67-orange)](https://www.rust-lang.org)
[![Minimum Supported Rust Version](https://img.shields.io/badge/MSRV-1.81-orange)](https://www.rust-lang.org)
[![MIT License](https://img.shields.io/github/license/jwodder/julian-rs.svg)](https://opensource.org/licenses/MIT)

[GitHub](https://github.com/jwodder/julian-rs) | [crates.io](https://crates.io/crates/julian-cli) | [Issues](https://github.com/jwodder/julian-rs/issues) | [Changelog](https://github.com/jwodder/julian-rs/blob/master/crates/julian-cli/CHANGELOG.md)
Expand Down
5 changes: 5 additions & 0 deletions crates/julian/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
v0.7.0 (in development)
-----------------------
- Increased MSRV to 1.81
- Added `std` feature for toggling `no_std` support

v0.6.0 (2024-12-18)
-------------------
- Increased MSRV to 1.67
Expand Down
4 changes: 3 additions & 1 deletion crates/julian/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ categories = ["date-and-time"]

[dependencies]
chrono = { version = "0.4.38", optional = true, default-features = false }
thiserror = "2.0.0"
thiserror = { version = "2.0.0", default-features = false }

[dev-dependencies]
assert_matches = "1.5.0"
rstest = { version = "0.23.0", default-features = false }
rstest_reuse = "0.7.0"

[features]
default = ["std"]
chrono = ["dep:chrono"]
std = []

[lints]
workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/julian/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
[![CI Status](https://github.com/jwodder/julian-rs/actions/workflows/test.yml/badge.svg)](https://github.com/jwodder/julian-rs/actions/workflows/test.yml)
[![codecov.io](https://codecov.io/gh/jwodder/julian-rs/branch/master/graph/badge.svg)](https://codecov.io/gh/jwodder/julian-rs)
[![Minimum Supported Rust Version](https://img.shields.io/badge/MSRV-1.67-orange)](https://www.rust-lang.org)
[![Minimum Supported Rust Version](https://img.shields.io/badge/MSRV-1.81-orange)](https://www.rust-lang.org)
[![MIT License](https://img.shields.io/github/license/jwodder/julian-rs.svg)](https://opensource.org/licenses/MIT)

[GitHub](https://github.com/jwodder/julian-rs) | [crates.io](https://crates.io/crates/julian) | [Documentation](https://docs.rs/julian) | [Issues](https://github.com/jwodder/julian-rs/issues) | [Changelog](https://github.com/jwodder/julian-rs/blob/master/crates/julian/CHANGELOG.md)
Expand Down
2 changes: 1 addition & 1 deletion crates/julian/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Error types
use crate::Month;
use std::num::ParseIntError;
use core::num::ParseIntError;
use thiserror::Error;

#[cfg(feature = "chrono")]
Expand Down
6 changes: 3 additions & 3 deletions crates/julian/src/inner.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{errors::ParseDateError, Jdnum, Month, COMMON_YEAR_LENGTH, LEAP_YEAR_LENGTH};
use std::cmp::Ordering;
use std::hash::{Hash, Hasher};
use core::cmp::Ordering;
use core::hash::{Hash, Hasher};

// Julian-calendar year in which Julian day number 0 occurs
const JDN0_YEAR: i32 = -4712;
Expand Down Expand Up @@ -192,7 +192,7 @@ impl<'a> DateParser<'a> {
pub(crate) fn parse_int(&mut self) -> Result<i32, ParseDateError> {
let mut first = true;
match scan(self.data, |c| {
(std::mem::replace(&mut first, false) && (c == '-' || c == '+')) || c.is_ascii_digit()
(core::mem::replace(&mut first, false) && (c == '-' || c == '+')) || c.is_ascii_digit()
}) {
("", _) => match self.data.chars().next() {
Some(got) => Err(ParseDateError::InvalidIntStart { got }),
Expand Down
4 changes: 2 additions & 2 deletions crates/julian/src/iter.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Iterator types
use crate::{Date, Month, MonthShape};
use std::iter::FusedIterator;
use std::ops::RangeInclusive;
use core::iter::FusedIterator;
use core::ops::RangeInclusive;

/// An iterator over the days of a month.
///
Expand Down
93 changes: 63 additions & 30 deletions crates/julian/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(not(test), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg))]
//! `julian` is a Rust library for converting between [Julian day numbers][jdn]
//! and dates in the [Gregorian calendar][] (either proleptic or with the
Expand All @@ -12,7 +13,11 @@
//! Features
//! ========
//!
//! The `julian` crate has the following optional feature:
//! The `julian` crate has the following optional features:
//!
//! - `std` — This feature is enabled by default; disable it to build in no-std
//! mode. When this feature is disabled, functions that use
//! [`std::time::SystemTime`] are not available.
//!
//! - `chrono` — Enables converting values of certain `julian` types to the
//! corresponding [`chrono`] types and *vice versa*.
Expand Down Expand Up @@ -172,10 +177,15 @@ pub mod iter;
pub mod ncal;
use crate::errors::*;
use crate::iter::*;
use std::cmp::Ordering;
use std::fmt;
use std::ops::RangeInclusive;
use std::str::FromStr;
use core::cmp::Ordering;
use core::fmt;
use core::ops::RangeInclusive;
use core::str::FromStr;

#[cfg(feature = "std")]
extern crate std;

#[cfg(feature = "std")]
use std::time::{SystemTime, UNIX_EPOCH};

/// Type used for Julian day numbers in this crate
Expand Down Expand Up @@ -396,6 +406,8 @@ impl Calendar {
/// converting the time. This can only happen if the system time in UTC is
/// before -5884323-05-15 (-5884202-03-16 O.S.) or after 5874898-06-03
/// (5874777-10-17 O.S.).
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub fn now(&self) -> Result<(Date, u32), ArithmeticError> {
self.at_system_time(SystemTime::now())
}
Expand All @@ -409,6 +421,8 @@ impl Calendar {
/// converting the time. This can only happen if the system time in UTC is
/// before -5884323-05-15 (-5884202-03-16 O.S.) or after 5874898-06-03
/// (5874777-10-17 O.S.).
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub fn at_system_time(&self, t: SystemTime) -> Result<(Date, u32), ArithmeticError> {
let (jdn, secs) = system2jdn(t)?;
Ok((self.at_jdn(jdn), secs))
Expand Down Expand Up @@ -2134,21 +2148,32 @@ impl FromStr for Month {
/// Parses a month from either its English name or just the first three
/// letters of the name. Input is treated case-insensitively.
fn from_str(s: &str) -> Result<Month, ParseMonthError> {
use Month::*;
match s.to_ascii_lowercase().as_str() {
"january" | "jan" => Ok(January),
"february" | "feb" => Ok(February),
"march" | "mar" => Ok(March),
"april" | "apr" => Ok(April),
"may" => Ok(May),
"june" | "jun" => Ok(June),
"july" | "jul" => Ok(July),
"august" | "aug" => Ok(August),
"september" | "sep" => Ok(September),
"october" | "oct" => Ok(October),
"november" | "nov" => Ok(November),
"december" | "dec" => Ok(December),
_ => Err(ParseMonthError),
if s.eq_ignore_ascii_case("january") || s.eq_ignore_ascii_case("jan") {
Ok(Month::January)
} else if s.eq_ignore_ascii_case("february") || s.eq_ignore_ascii_case("feb") {
Ok(Month::February)
} else if s.eq_ignore_ascii_case("march") || s.eq_ignore_ascii_case("mar") {
Ok(Month::March)
} else if s.eq_ignore_ascii_case("april") || s.eq_ignore_ascii_case("apr") {
Ok(Month::April)
} else if s.eq_ignore_ascii_case("may") {
Ok(Month::May)
} else if s.eq_ignore_ascii_case("june") || s.eq_ignore_ascii_case("jun") {
Ok(Month::June)
} else if s.eq_ignore_ascii_case("july") || s.eq_ignore_ascii_case("jul") {
Ok(Month::July)
} else if s.eq_ignore_ascii_case("august") || s.eq_ignore_ascii_case("aug") {
Ok(Month::August)
} else if s.eq_ignore_ascii_case("september") || s.eq_ignore_ascii_case("sep") {
Ok(Month::September)
} else if s.eq_ignore_ascii_case("october") || s.eq_ignore_ascii_case("oct") {
Ok(Month::October)
} else if s.eq_ignore_ascii_case("november") || s.eq_ignore_ascii_case("nov") {
Ok(Month::November)
} else if s.eq_ignore_ascii_case("december") || s.eq_ignore_ascii_case("dec") {
Ok(Month::December)
} else {
Err(ParseMonthError)
}
}
}
Expand Down Expand Up @@ -2366,16 +2391,22 @@ impl FromStr for Weekday {
/// Parses a weekday from either its English name or just the first three
/// letters of the name. Input is treated case-insensitively.
fn from_str(s: &str) -> Result<Weekday, ParseWeekdayError> {
use Weekday::*;
match s.to_ascii_lowercase().as_str() {
"sunday" | "sun" => Ok(Sunday),
"monday" | "mon" => Ok(Monday),
"tuesday" | "tue" => Ok(Tuesday),
"wednesday" | "wed" => Ok(Wednesday),
"thursday" | "thu" => Ok(Thursday),
"friday" | "fri" => Ok(Friday),
"saturday" | "sat" => Ok(Saturday),
_ => Err(ParseWeekdayError),
if s.eq_ignore_ascii_case("sunday") || s.eq_ignore_ascii_case("sun") {
Ok(Weekday::Sunday)
} else if s.eq_ignore_ascii_case("monday") || s.eq_ignore_ascii_case("mon") {
Ok(Weekday::Monday)
} else if s.eq_ignore_ascii_case("tuesday") || s.eq_ignore_ascii_case("tue") {
Ok(Weekday::Tuesday)
} else if s.eq_ignore_ascii_case("wednesday") || s.eq_ignore_ascii_case("wed") {
Ok(Weekday::Wednesday)
} else if s.eq_ignore_ascii_case("thursday") || s.eq_ignore_ascii_case("thu") {
Ok(Weekday::Thursday)
} else if s.eq_ignore_ascii_case("friday") || s.eq_ignore_ascii_case("fri") {
Ok(Weekday::Friday)
} else if s.eq_ignore_ascii_case("saturday") || s.eq_ignore_ascii_case("sat") {
Ok(Weekday::Saturday)
} else {
Err(ParseWeekdayError)
}
}
}
Expand Down Expand Up @@ -2446,6 +2477,8 @@ impl From<Weekday> for chrono::Weekday {
/// conversion. This can only happen if the system time in UTC is before
/// -5884323-05-15 (-5884202-03-16 O.S.) or after 5874898-06-03 (5874777-10-17
/// O.S.).
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub fn system2jdn(t: SystemTime) -> Result<(Jdnum, u32), ArithmeticError> {
let ts = match t.duration_since(UNIX_EPOCH) {
Ok(d) => i64::try_from(d.as_secs()),
Expand Down
Loading