Skip to content

Commit

Permalink
LocalDate compliance [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
tyranron committed Aug 15, 2024
1 parent 0edc4c9 commit 7fa7905
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 50 deletions.
5 changes: 2 additions & 3 deletions book/src/types/scalars.md
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ mod date_scalar {
| [`bigdecimal::BigDecimal`] | `BigDecimal` | [`bigdecimal`] |
| [`bson::oid::ObjectId`] | `ObjectId` | [`bson`] |
| [`bson::DateTime`] | `UtcDateTime` | [`bson`] |
| [`chrono::NaiveDate`] | [`Date`] | [`chrono`] |
| [`chrono::NaiveDate`] | [`LocalDate`] | [`chrono`] |
| [`chrono::NaiveTime`] | [`LocalTime`] | [`chrono`] |
| [`chrono::NaiveDateTime`] | [`LocalDateTime`] | [`chrono`] |
| [`chrono::DateTime`] | [`DateTime`] | [`chrono`] |
Expand All @@ -401,7 +401,7 @@ mod date_scalar {
| [`jiff::civil::DateTime`] | [`LocalDateTime`] | [`jiff`] |
| [`jiff::Timestamp`] | [`DateTime`] | [`jiff`] |
| [`jiff::Span`] | [`Duration`] | [`jiff`] |
| [`time::Date`] | [`Date`] | [`time`] |
| [`time::Date`] | [`LocalDate`] | [`time`] |
| [`time::Time`] | [`LocalTime`] | [`time`] |
| [`time::PrimitiveDateTime`] | [`LocalDateTime`] | [`time`] |
| [`time::OffsetDateTime`] | [`DateTime`] | [`time`] |
Expand All @@ -424,7 +424,6 @@ mod date_scalar {
[`chrono::NaiveTime`]: https://docs.rs/chrono/latest/chrono/naive/struct.NaiveTime.html
[`chrono-tz`]: https://docs.rs/chrono-tz
[`chrono_tz::Tz`]: https://docs.rs/chrono-tz/latest/chrono_tz/enum.Tz.html
[`Date`]: https://graphql-scalars.dev/docs/scalars/date
[`DateTime`]: https://graphql-scalars.dev/docs/scalars/date-time
[`Decimal`]: https://docs.rs/rust_decimal/latest/rust_decimal/struct.Decimal.html
[`Duration`]: https://graphql-scalars.dev/docs/scalars/duration
Expand Down
3 changes: 3 additions & 0 deletions juniper/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ All user visible changes to `juniper` crate will be documented in this file. Thi
- Switched `LocalDateTime` scalars to `yyyy-MM-ddTHH:mm:ss` format in types:
- `chrono::NaiveDateTime`.
- `time::PrimitiveDateTime`.
- Switched from `Date` scalar to `LocalDate` scalar in types:
- `chrono::NaiveDate`.
- `time::Date`.
- Renamed `Url` scalar to `URL` in types:
- `url::Url`.
- Renamed `Uuid` scalar to `UUID` in types:
Expand Down
58 changes: 29 additions & 29 deletions juniper/src/integrations/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//!
//! | Rust type | Format | GraphQL scalar |
//! |-------------------|-----------------------|-----------------------|
//! | [`NaiveDate`] | `yyyy-MM-dd` | [`Date`][s1] |
//! | [`NaiveDate`] | `yyyy-MM-dd` | [`LocalDate`][s1] |
//! | [`NaiveTime`] | `HH:mm[:ss[.SSS]]` | [`LocalTime`][s2] |
//! | [`NaiveDateTime`] | `yyyy-MM-ddTHH:mm:ss` | [`LocalDateTime`][s3] |
//! | [`DateTime`] | [RFC 3339] string | [`DateTime`][s4] |
Expand All @@ -14,7 +14,7 @@
//! [`NaiveDateTime`]: chrono::naive::NaiveDateTime
//! [`NaiveTime`]: chrono::naive::NaiveTime
//! [RFC 3339]: https://datatracker.ietf.org/doc/html/rfc3339#section-5.6
//! [s1]: https://graphql-scalars.dev/docs/scalars/date
//! [s1]: https://graphql-scalars.dev/docs/scalars/local-date
//! [s2]: https://graphql-scalars.dev/docs/scalars/local-time
//! [s3]: https://graphql-scalars.dev/docs/scalars/local-date-time
//! [s4]: https://graphql-scalars.dev/docs/scalars/date-time
Expand All @@ -30,42 +30,42 @@ use crate::{graphql_scalar, InputValue, ScalarValue, Value};
/// Represents a description of the date (as used for birthdays, for example).
/// It cannot represent an instant on the time-line.
///
/// [`Date` scalar][1] compliant.
/// [`LocalDate` scalar][1] compliant.
///
/// See also [`chrono::NaiveDate`][2] for details.
///
/// [1]: https://graphql-scalars.dev/docs/scalars/date
/// [1]: https://graphql-scalars.dev/docs/scalars/local-date
/// [2]: https://docs.rs/chrono/latest/chrono/naive/struct.NaiveDate.html
#[graphql_scalar(
with = date,
with = local_date,
parse_token(String),
specified_by_url = "https://graphql-scalars.dev/docs/scalars/date",
specified_by_url = "https://graphql-scalars.dev/docs/scalars/local-date",
)]
pub type Date = chrono::NaiveDate;
pub type LocalDate = chrono::NaiveDate;

mod date {
mod local_date {
use super::*;

/// Format of a [`Date` scalar][1].
/// Format of a [`LocalDate` scalar][1].
///
/// [1]: https://graphql-scalars.dev/docs/scalars/date
/// [1]: https://graphql-scalars.dev/docs/scalars/local-date
const FORMAT: &str = "%Y-%m-%d";

pub(super) fn to_output<S>(v: &Date) -> Value<S>
pub(super) fn to_output<S>(v: &LocalDate) -> Value<S>
where
S: ScalarValue,
{
Value::scalar(v.format(FORMAT).to_string())
}

pub(super) fn from_input<S>(v: &InputValue<S>) -> Result<Date, String>
pub(super) fn from_input<S>(v: &InputValue<S>) -> Result<LocalDate, String>
where
S: ScalarValue,
{
v.as_string_value()
.ok_or_else(|| format!("Expected `String`, found: {v}"))
.and_then(|s| {
Date::parse_from_str(s, FORMAT).map_err(|e| format!("Invalid `Date`: {e}"))
LocalDate::parse_from_str(s, FORMAT).map_err(|e| format!("Invalid `LocalDate`: {e}"))
})
}
}
Expand Down Expand Up @@ -339,19 +339,19 @@ impl FromFixedOffset for chrono_tz::Tz {
}

#[cfg(test)]
mod date_test {
mod local_date_test {
use crate::{graphql_input_value, FromInputValue as _, InputValue, ToInputValue as _};

use super::Date;
use super::LocalDate;

#[test]
fn parses_correct_input() {
for (raw, expected) in [
("1996-12-19", Date::from_ymd_opt(1996, 12, 19)),
("1564-01-30", Date::from_ymd_opt(1564, 01, 30)),
("1996-12-19", LocalDate::from_ymd_opt(1996, 12, 19)),
("1564-01-30", LocalDate::from_ymd_opt(1564, 01, 30)),
] {
let input: InputValue = graphql_input_value!((raw));
let parsed = Date::from_input_value(&input);
let parsed = LocalDate::from_input_value(&input);

assert!(
parsed.is_ok(),
Expand Down Expand Up @@ -379,7 +379,7 @@ mod date_test {
graphql_input_value!(false),
] {
let input: InputValue = input;
let parsed = Date::from_input_value(&input);
let parsed = LocalDate::from_input_value(&input);

assert!(parsed.is_err(), "allows input: {input:?}");
}
Expand All @@ -389,15 +389,15 @@ mod date_test {
fn formats_correctly() {
for (val, expected) in [
(
Date::from_ymd_opt(1996, 12, 19),
LocalDate::from_ymd_opt(1996, 12, 19),
graphql_input_value!("1996-12-19"),
),
(
Date::from_ymd_opt(1564, 01, 30),
LocalDate::from_ymd_opt(1564, 01, 30),
graphql_input_value!("1564-01-30"),
),
(
Date::from_ymd_opt(2020, 01, 01),
LocalDate::from_ymd_opt(2020, 01, 01),
graphql_input_value!("2020-01-01"),
),
] {
Expand Down Expand Up @@ -749,7 +749,7 @@ mod integration_test {
types::scalars::{EmptyMutation, EmptySubscription},
};

use super::{Date, DateTime, FixedOffset, FromFixedOffset, LocalDateTime, LocalTime, TimeZone};
use super::{LocalDate, DateTime, FixedOffset, FromFixedOffset, LocalDateTime, LocalTime, TimeZone};

#[tokio::test]
async fn serializes() {
Expand Down Expand Up @@ -796,8 +796,8 @@ mod integration_test {

#[graphql_object]
impl Root {
fn date() -> Date {
Date::from_ymd_opt(2015, 3, 14).unwrap()
fn local_date() -> LocalDate {
LocalDate::from_ymd_opt(2015, 3, 14).unwrap()
}

fn local_time() -> LocalTime {
Expand All @@ -806,15 +806,15 @@ mod integration_test {

fn local_date_time() -> LocalDateTime {
LocalDateTime::new(
Date::from_ymd_opt(2016, 7, 8).unwrap(),
LocalDate::from_ymd_opt(2016, 7, 8).unwrap(),
LocalTime::from_hms_opt(9, 10, 11).unwrap(),
)
}

fn date_time() -> DateTime<chrono::Utc> {
DateTime::from_naive_utc_and_offset(
LocalDateTime::new(
Date::from_ymd_opt(1996, 12, 20).unwrap(),
LocalDate::from_ymd_opt(1996, 12, 20).unwrap(),
LocalTime::from_hms_opt(0, 39, 57).unwrap(),
),
chrono::Utc,
Expand All @@ -831,7 +831,7 @@ mod integration_test {
}

const DOC: &str = r#"{
date
localDate
localTime
localDateTime
dateTime,
Expand All @@ -849,7 +849,7 @@ mod integration_test {
execute(DOC, None, &schema, &graphql_vars! {}, &()).await,
Ok((
graphql_value!({
"date": "2015-03-14",
"localDate": "2015-03-14",
"localTime": "16:07:08",
"localDateTime": "2016-07-08T09:10:11",
"dateTime": "1996-12-20T00:39:57Z",
Expand Down
36 changes: 18 additions & 18 deletions juniper/src/integrations/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//!
//! | Rust type | Format | GraphQL scalar |
//! |-----------------------|-----------------------|-----------------------|
//! | [`Date`] | `yyyy-MM-dd` | [`Date`][s1] |
//! | [`Date`] | `yyyy-MM-dd` | [`LocalDate`][s1] |
//! | [`Time`] | `HH:mm[:ss[.SSS]]` | [`LocalTime`][s2] |
//! | [`PrimitiveDateTime`] | `yyyy-MM-ddTHH:mm:ss` | [`LocalDateTime`][s3] |
//! | [`OffsetDateTime`] | [RFC 3339] string | [`DateTime`][s4] |
Expand All @@ -16,7 +16,7 @@
//! [`Time`]: time::Time
//! [`UtcOffset`]: time::UtcOffset
//! [RFC 3339]: https://datatracker.ietf.org/doc/html/rfc3339#section-5.6
//! [s1]: https://graphql-scalars.dev/docs/scalars/date
//! [s1]: https://graphql-scalars.dev/docs/scalars/local-date
//! [s2]: https://graphql-scalars.dev/docs/scalars/local-time
//! [s3]: https://graphql-scalars.dev/docs/scalars/local-date-time
//! [s4]: https://graphql-scalars.dev/docs/scalars/date-time
Expand All @@ -34,38 +34,38 @@ use crate::{graphql_scalar, InputValue, ScalarValue, Value};
/// Represents a description of the date (as used for birthdays, for example).
/// It cannot represent an instant on the time-line.
///
/// [`Date` scalar][1] compliant.
/// [`LocalDate` scalar][1] compliant.
///
/// See also [`time::Date`][2] for details.
///
/// [1]: https://graphql-scalars.dev/docs/scalars/date
/// [1]: https://graphql-scalars.dev/docs/scalars/local-date
/// [2]: https://docs.rs/time/*/time/struct.Date.html
#[graphql_scalar(
with = date,
with = local_date,
parse_token(String),
specified_by_url = "https://graphql-scalars.dev/docs/scalars/date",
specified_by_url = "https://graphql-scalars.dev/docs/scalars/local-date",
)]
pub type Date = time::Date;
pub type LocalDate = time::Date;

mod date {
mod local_date {
use super::*;

/// Format of a [`Date` scalar][1].
/// Format of a [`LocalDate` scalar][1].
///
/// [1]: https://graphql-scalars.dev/docs/scalars/date
/// [1]: https://graphql-scalars.dev/docs/scalars/local-date
const FORMAT: &[BorrowedFormatItem<'_>] = format_description!("[year]-[month]-[day]");

pub(super) fn to_output<S: ScalarValue>(v: &Date) -> Value<S> {
pub(super) fn to_output<S: ScalarValue>(v: &LocalDate) -> Value<S> {
Value::scalar(
v.format(FORMAT)
.unwrap_or_else(|e| panic!("failed to format `Date`: {e}")),
.unwrap_or_else(|e| panic!("failed to format `LocalDate`: {e}")),
)
}

pub(super) fn from_input<S: ScalarValue>(v: &InputValue<S>) -> Result<Date, String> {
pub(super) fn from_input<S: ScalarValue>(v: &InputValue<S>) -> Result<LocalDate, String> {
v.as_string_value()
.ok_or_else(|| format!("Expected `String`, found: {v}"))
.and_then(|s| Date::parse(s, FORMAT).map_err(|e| format!("Invalid `Date`: {e}")))
.and_then(|s| LocalDate::parse(s, FORMAT).map_err(|e| format!("Invalid `LocalDate`: {e}")))
}
}

Expand Down Expand Up @@ -253,12 +253,12 @@ mod utc_offset {
}

#[cfg(test)]
mod date_test {
mod local_date_test {
use time::macros::date;

use crate::{graphql_input_value, FromInputValue as _, InputValue, ToInputValue as _};

use super::Date;
use super::LocalDate;

#[test]
fn parses_correct_input() {
Expand All @@ -267,7 +267,7 @@ mod date_test {
("1564-01-30", date!(1564 - 01 - 30)),
] {
let input: InputValue = graphql_input_value!((raw));
let parsed = Date::from_input_value(&input);
let parsed = LocalDate::from_input_value(&input);

assert!(
parsed.is_ok(),
Expand Down Expand Up @@ -295,7 +295,7 @@ mod date_test {
graphql_input_value!(false),
] {
let input: InputValue = input;
let parsed = Date::from_input_value(&input);
let parsed = LocalDate::from_input_value(&input);

assert!(parsed.is_err(), "allows input: {input:?}");
}
Expand Down

0 comments on commit 7fa7905

Please sign in to comment.