Skip to content

Commit d87e499

Browse files
committed
Leverage derive_more::Debug
1 parent 2ce55df commit d87e499

File tree

7 files changed

+33
-77
lines changed

7 files changed

+33
-77
lines changed

juniper/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ bson = { version = "2.4", optional = true }
5151
chrono = { version = "0.4.30", features = ["alloc"], default-features = false, optional = true }
5252
chrono-tz = { version = "0.10", default-features = false, optional = true }
5353
compact_str = "0.9"
54-
derive_more = { version = "2.0", features = ["display"] }
54+
derive_more = { version = "2.0", features = ["debug", "display"] }
5555
fnv = "1.0.5"
5656
futures = { version = "0.3.22", features = ["alloc"], default-features = false }
5757
graphql-parser = { version = "0.4", optional = true }

juniper/src/integrations/jiff.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@
5050
//! [s7]: https://graphql-scalars.dev/docs/scalars/duration
5151
//! [1]: https://docs.rs/jiff/latest/jiff/index.html#time-zone-features
5252
53-
use std::{error::Error, fmt, str};
53+
use std::{error::Error, str};
5454

55-
use derive_more::with_trait::Display;
55+
use derive_more::with_trait::{Debug, Display};
5656

5757
use crate::{InputValue, ScalarValue, Value, graphql_scalar};
5858

@@ -411,23 +411,14 @@ mod time_zone_or_utc_offset {
411411
}
412412

413413
/// Error parsing a [`TimeZone`] value.
414-
#[derive(Clone, Display)]
414+
#[derive(Clone, Debug, Display)]
415415
pub enum TimeZoneParsingError {
416416
/// Identifier cannot not be parsed by the [`jiff::tz::TimeZone::get()`] method.
417417
InvalidTimeZone(jiff::Error),
418418

419419
/// GraphQL scalar [`TimeZone`] requires `tz::TimeZone` with IANA name.
420420
#[display("missing IANA name")]
421-
MissingIanaName(jiff::tz::TimeZone),
422-
}
423-
424-
impl fmt::Debug for TimeZoneParsingError {
425-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
426-
match self {
427-
Self::InvalidTimeZone(e) => write!(f, "TimeZoneParsingError::InvalidTimeZone({e:?})"),
428-
Self::MissingIanaName(_) => write!(f, "TimeZoneParsingError::MissingIanaName(..)"),
429-
}
430-
}
421+
MissingIanaName(#[debug(ignore)] jiff::tz::TimeZone),
431422
}
432423

433424
impl Error for TimeZoneParsingError {

juniper/src/schema/meta.rs

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
//! Types used to describe a `GraphQL` schema
22
3-
use std::{borrow::ToOwned, fmt};
3+
use std::borrow::ToOwned;
44

55
use arcstr::ArcStr;
6+
use derive_more::with_trait::Debug;
67

78
use crate::{
89
FieldError, IntoFieldError,
@@ -42,27 +43,20 @@ impl DeprecationStatus {
4243
}
4344

4445
/// Scalar type metadata
46+
#[derive(Debug)]
4547
pub struct ScalarMeta<S> {
4648
#[doc(hidden)]
4749
pub name: ArcStr,
4850
#[doc(hidden)]
4951
pub description: Option<ArcStr>,
5052
#[doc(hidden)]
5153
pub specified_by_url: Option<ArcStr>,
54+
#[debug(ignore)]
5255
pub(crate) try_parse_fn: InputValueParseFn<S>,
56+
#[debug(ignore)]
5357
pub(crate) parse_fn: ScalarTokenParseFn<S>,
5458
}
5559

56-
impl<S: fmt::Debug> fmt::Debug for ScalarMeta<S> {
57-
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
58-
fmt.debug_struct("ScalarMeta")
59-
.field("name", &self.name)
60-
.field("description", &self.description)
61-
.field("specified_by_url", &self.specified_by_url)
62-
.finish_non_exhaustive()
63-
}
64-
}
65-
6660
impl<S> ScalarMeta<S> {
6761
/// Builds a new [`ScalarMeta`] type with the specified `name`.
6862
pub fn new<T>(name: impl Into<ArcStr>) -> Self
@@ -213,26 +207,18 @@ impl<S> ObjectMeta<S> {
213207
}
214208

215209
/// Enum type metadata
210+
#[derive(Debug)]
216211
pub struct EnumMeta<S> {
217212
#[doc(hidden)]
218213
pub name: ArcStr,
219214
#[doc(hidden)]
220215
pub description: Option<ArcStr>,
221216
#[doc(hidden)]
222217
pub values: Vec<EnumValue>,
218+
#[debug(ignore)]
223219
pub(crate) try_parse_fn: InputValueParseFn<S>,
224220
}
225221

226-
impl<S: fmt::Debug> fmt::Debug for EnumMeta<S> {
227-
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
228-
fmt.debug_struct("EnumMeta")
229-
.field("name", &self.name)
230-
.field("description", &self.description)
231-
.field("values", &self.values)
232-
.finish_non_exhaustive()
233-
}
234-
}
235-
236222
impl<S> EnumMeta<S> {
237223
/// Builds a new [`EnumMeta`] type with the specified `name` and possible `values`.
238224
pub fn new<T>(name: impl Into<ArcStr>, values: &[EnumValue]) -> Self
@@ -354,26 +340,18 @@ impl UnionMeta {
354340
}
355341

356342
/// Input object metadata
343+
#[derive(Debug)]
357344
pub struct InputObjectMeta<S> {
358345
#[doc(hidden)]
359346
pub name: ArcStr,
360347
#[doc(hidden)]
361348
pub description: Option<ArcStr>,
362349
#[doc(hidden)]
363350
pub input_fields: Vec<Argument<S>>,
351+
#[debug(ignore)]
364352
pub(crate) try_parse_fn: InputValueParseFn<S>,
365353
}
366354

367-
impl<S: fmt::Debug> fmt::Debug for InputObjectMeta<S> {
368-
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
369-
fmt.debug_struct("InputObjectMeta")
370-
.field("name", &self.name)
371-
.field("description", &self.description)
372-
.field("input_fields", &self.input_fields)
373-
.finish_non_exhaustive()
374-
}
375-
}
376-
377355
impl<S> InputObjectMeta<S> {
378356
/// Builds a new [`InputObjectMeta`] type with the specified `name` and `input_fields`.
379357
pub fn new<T>(name: impl Into<ArcStr>, input_fields: &[Argument<S>]) -> Self

juniper_graphql_ws/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ graphql-transport-ws = []
2626
graphql-ws = []
2727

2828
[dependencies]
29+
derive_more = { version = "2.0", features = ["debug"] }
2930
juniper = { version = "0.16", path = "../juniper", default-features = false }
3031
juniper_subscriptions = { version = "0.17.0", path = "../juniper_subscriptions" }
3132
serde = { version = "1.0.122", features = ["derive"], default-features = false }

juniper_graphql_ws/src/server_message.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//! Common definitions regarding server messages.
22
3-
use std::{any::Any, fmt, marker::PhantomPinned};
3+
use std::{any::Any, marker::PhantomPinned};
44

5+
use derive_more::with_trait::Debug;
56
use juniper::GraphQLError;
67
use serde::{Serialize, Serializer};
78

@@ -15,6 +16,8 @@ use serde::{Serialize, Serializer};
1516
/// [`graphql_ws::DataPayload`]: crate::graphql_ws::DataPayload
1617
// XXX: Think carefully before deriving traits. This is self-referential (error references
1718
// _execution_params).
19+
#[derive(Debug)]
20+
#[debug("{error:?}")]
1821
pub struct ErrorPayload {
1922
_execution_params: Option<Box<dyn Any + Send>>,
2023
error: GraphQLError,
@@ -37,12 +40,6 @@ impl ErrorPayload {
3740
}
3841
}
3942

40-
impl fmt::Debug for ErrorPayload {
41-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42-
self.error.fmt(f)
43-
}
44-
}
45-
4643
impl PartialEq for ErrorPayload {
4744
fn eq(&self, other: &Self) -> bool {
4845
self.error.eq(&other.error)

juniper_hyper/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ keywords = ["apollo", "graphql", "hyper", "juniper"]
1818
exclude = ["/examples/", "/release.toml"]
1919

2020
[dependencies]
21-
derive_more = { version = "2.0", features = ["display"] }
21+
derive_more = { version = "2.0", features = ["debug", "display"] }
2222
http-body-util = "0.1"
2323
hyper = { version = "1.0", features = ["server"] }
2424
juniper = { version = "0.16", path = "../juniper", default-features = false }

juniper_hyper/src/lib.rs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
#![cfg_attr(not(any(doc, test)), doc = env!("CARGO_PKG_NAME"))]
33
#![cfg_attr(test, expect(unused_crate_dependencies, reason = "examples"))]
44

5-
use std::{error::Error, fmt, string::FromUtf8Error, sync::Arc};
5+
use std::{error::Error, string::FromUtf8Error, sync::Arc};
66

7-
use derive_more::with_trait::Display;
7+
use derive_more::with_trait::{Debug, Display};
88
use http_body_util::BodyExt as _;
99
use hyper::{
1010
Method, Request, Response, StatusCode,
@@ -34,7 +34,7 @@ where
3434
SubscriptionT::TypeInfo: Sync,
3535
CtxT: Sync,
3636
S: ScalarValue + Send + Sync,
37-
B: Body<Error: fmt::Display>,
37+
B: Body<Error: Display>,
3838
{
3939
match parse_req(req).await {
4040
Ok(req) => execute_request_sync(schema, context, req).await,
@@ -58,7 +58,7 @@ where
5858
SubscriptionT::TypeInfo: Sync,
5959
CtxT: Sync,
6060
S: ScalarValue + Send + Sync,
61-
B: Body<Error: fmt::Display>,
61+
B: Body<Error: Display>,
6262
{
6363
match parse_req(req).await {
6464
Ok(req) => execute_request(schema, context, req).await,
@@ -69,7 +69,7 @@ where
6969
async fn parse_req<S, B>(req: Request<B>) -> Result<GraphQLBatchRequest<S>, Response<String>>
7070
where
7171
S: ScalarValue,
72-
B: Body<Error: fmt::Display>,
72+
B: Body<Error: Display>,
7373
{
7474
match *req.method() {
7575
Method::GET => parse_get_req(req),
@@ -176,7 +176,7 @@ pub async fn playground(
176176

177177
fn render_error<B>(err: GraphQLRequestError<B>) -> Response<String>
178178
where
179-
B: Body<Error: fmt::Display>,
179+
B: Body<Error: Display>,
180180
{
181181
let mut resp = new_response(StatusCode::BAD_REQUEST);
182182
*resp.body_mut() = err.to_string();
@@ -312,32 +312,21 @@ fn new_html_response(code: StatusCode) -> Response<String> {
312312
resp
313313
}
314314

315-
#[derive(Display)]
315+
// TODO: Use `#[debug(forward)]` once `derive_more::Debug` is capable of it.
316+
#[derive(Debug, Display)]
316317
enum GraphQLRequestError<B: Body> {
318+
#[debug("{_0:?}")]
317319
BodyHyper(B::Error),
320+
#[debug("{_0:?}")]
318321
BodyUtf8(FromUtf8Error),
322+
#[debug("{_0:?}")]
319323
BodyJSONError(SerdeError),
324+
#[debug("{_0:?}")]
320325
Variables(SerdeError),
326+
#[debug("{_0:?}")]
321327
Invalid(String),
322328
}
323329

324-
// NOTE: Manual implementation instead of `#[derive(Debug)]` is used to omit imposing unnecessary
325-
// `B: Debug` bound on the implementation.
326-
impl<B> fmt::Debug for GraphQLRequestError<B>
327-
where
328-
B: Body<Error: fmt::Debug>,
329-
{
330-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
331-
match self {
332-
Self::BodyHyper(e) => fmt::Debug::fmt(e, f),
333-
Self::BodyUtf8(e) => fmt::Debug::fmt(e, f),
334-
Self::BodyJSONError(e) => fmt::Debug::fmt(e, f),
335-
Self::Variables(e) => fmt::Debug::fmt(e, f),
336-
Self::Invalid(e) => fmt::Debug::fmt(e, f),
337-
}
338-
}
339-
}
340-
341330
impl<B> Error for GraphQLRequestError<B>
342331
where
343332
B: Body<Error: Error + 'static>,

0 commit comments

Comments
 (0)