diff --git a/book/src/types/scalars.md b/book/src/types/scalars.md index 41dd10130..5fac586da 100644 --- a/book/src/types/scalars.md +++ b/book/src/types/scalars.md @@ -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`] | @@ -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`] | @@ -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 diff --git a/juniper/CHANGELOG.md b/juniper/CHANGELOG.md index 38e01a797..705effe33 100644 --- a/juniper/CHANGELOG.md +++ b/juniper/CHANGELOG.md @@ -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: diff --git a/juniper/src/integrations/chrono.rs b/juniper/src/integrations/chrono.rs index 869311e31..415b06436 100644 --- a/juniper/src/integrations/chrono.rs +++ b/juniper/src/integrations/chrono.rs @@ -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] | @@ -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 @@ -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(v: &Date) -> Value + pub(super) fn to_output(v: &LocalDate) -> Value where S: ScalarValue, { Value::scalar(v.format(FORMAT).to_string()) } - pub(super) fn from_input(v: &InputValue) -> Result + pub(super) fn from_input(v: &InputValue) -> Result 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}")) }) } } @@ -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(), @@ -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:?}"); } @@ -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"), ), ] { @@ -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() { @@ -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 { @@ -806,7 +806,7 @@ 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(), ) } @@ -814,7 +814,7 @@ mod integration_test { fn date_time() -> DateTime { 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, @@ -831,7 +831,7 @@ mod integration_test { } const DOC: &str = r#"{ - date + localDate localTime localDateTime dateTime, @@ -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", diff --git a/juniper/src/integrations/time.rs b/juniper/src/integrations/time.rs index c971c71a5..5c8cc90d7 100644 --- a/juniper/src/integrations/time.rs +++ b/juniper/src/integrations/time.rs @@ -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] | @@ -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 @@ -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(v: &Date) -> Value { + pub(super) fn to_output(v: &LocalDate) -> Value { 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(v: &InputValue) -> Result { + pub(super) fn from_input(v: &InputValue) -> Result { 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}"))) } } @@ -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() { @@ -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(), @@ -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:?}"); }