Skip to content

Commit 34b9937

Browse files
committed
Leverage Itertools::format()
1 parent b12d9d1 commit 34b9937

File tree

6 files changed

+44
-23
lines changed

6 files changed

+44
-23
lines changed

juniper/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ fnv = "1.0.5"
5555
futures = { version = "0.3.22", features = ["alloc"], default-features = false }
5656
graphql-parser = { version = "0.4", optional = true }
5757
indexmap = { version = "2.0", features = ["serde"] }
58+
itertools = "0.14"
5859
jiff = { version = "0.2", features = ["std"], default-features = false, optional = true }
5960
juniper_codegen = { version = "0.16.0", path = "../juniper_codegen" }
6061
rust_decimal = { version = "1.20", default-features = false, optional = true }

juniper/src/types/utilities.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::collections::HashSet;
22

3+
use itertools::Itertools as _;
4+
35
use crate::{
46
ast::InputValue,
57
schema::{
@@ -179,9 +181,7 @@ where
179181
} else {
180182
let missing_fields = remaining_required_fields
181183
.into_iter()
182-
.map(|s| format!("\"{s}\""))
183-
.collect::<Vec<_>>()
184-
.join(", ");
184+
.format_with(", ", |s, f| f(&format_args!("\"{s}\"")));
185185
Some(error::missing_fields(arg_type, missing_fields))
186186
}
187187
} else {

juniper/src/validation/context.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use std::{
33
fmt::{self, Debug},
44
};
55

6+
use itertools::Itertools as _;
7+
68
use crate::{
79
ast::{Definition, Document},
810
schema::model::DynType,
@@ -58,12 +60,7 @@ impl fmt::Display for RuleError {
5860
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5961
// This is fine since all `RuleError`s should have at least one source
6062
// position.
61-
let locations = self
62-
.locations
63-
.iter()
64-
.map(ToString::to_string)
65-
.collect::<Vec<_>>()
66-
.join(", ");
63+
let locations = self.locations.iter().format(", ");
6764
write!(f, "{}. At {locations}", self.message)
6865
}
6966
}

juniper/src/validation/rules/overlapping_fields_can_be_merged.rs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1-
use std::{borrow::Borrow, cell::RefCell, collections::HashMap, fmt::Debug, hash::Hash};
1+
use std::{
2+
borrow::Borrow,
3+
cell::RefCell,
4+
collections::HashMap,
5+
fmt::{self, Debug},
6+
hash::Hash,
7+
};
28

39
use arcstr::ArcStr;
10+
use itertools::Itertools as _;
411

512
use crate::{
613
ast::{Arguments, Definition, Document, Field, Fragment, FragmentSpread, Selection, Type},
@@ -745,19 +752,33 @@ fn error_message(reason_name: &str, reason: &ConflictReasonMessage) -> String {
745752
)
746753
}
747754

748-
fn format_reason(reason: &ConflictReasonMessage) -> String {
755+
fn format_reason(reason: &ConflictReasonMessage) -> impl fmt::Display {
756+
enum Either<A, B> {
757+
A(A),
758+
B(B),
759+
}
760+
761+
// TODO: Use `derive_more`.
762+
impl<A: fmt::Display, B: fmt::Display> fmt::Display for Either<A, B> {
763+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
764+
match self {
765+
Self::A(a) => write!(f, "{a}"),
766+
Self::B(b) => write!(f, "{b}"),
767+
}
768+
}
769+
}
770+
749771
match reason {
750-
ConflictReasonMessage::Message(name) => name.clone(),
751-
ConflictReasonMessage::Nested(nested) => nested
752-
.iter()
753-
.map(|ConflictReason(name, subreason)| {
754-
format!(
772+
ConflictReasonMessage::Message(name) => Either::A(name),
773+
ConflictReasonMessage::Nested(nested) => Either::B(nested.iter().format_with(
774+
" and ",
775+
|ConflictReason(name, subreason), f| {
776+
f(&format_args!(
755777
r#"subfields "{name}" conflict because {}"#,
756778
format_reason(subreason),
757-
)
758-
})
759-
.collect::<Vec<_>>()
760-
.join(" and "),
779+
))
780+
},
781+
)),
761782
}
762783
}
763784

juniper_warp/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ headers = "0.3.8"
4646
async-stream = "0.3"
4747
env_logger = "0.11"
4848
futures = "0.3.22"
49+
itertools = "0.14"
4950
juniper = { version = "0.16", path = "../juniper", features = ["expose-test-schema"] }
5051
log = "0.4"
5152
percent-encoding = "2.1"

juniper_warp/tests/http_test_suite.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use futures::TryStreamExt as _;
2+
use itertools::Itertools as _;
23
use juniper::{
34
EmptyMutation, EmptySubscription, RootNode,
45
http::tests::{HttpIntegration, TestResponse, run_http_test_suite},
@@ -69,9 +70,9 @@ impl HttpIntegration for TestWarpIntegration {
6970

7071
let url = Url::parse(&format!("http://localhost:3000{url}")).expect("url to parse");
7172

72-
let url: String = utf8_percent_encode(url.query().unwrap_or(""), QUERY_ENCODE_SET)
73-
.collect::<Vec<_>>()
74-
.join("");
73+
let url = utf8_percent_encode(url.query().unwrap_or(""), QUERY_ENCODE_SET)
74+
.format("")
75+
.to_string();
7576

7677
self.make_request(
7778
warp::test::request()

0 commit comments

Comments
 (0)