|
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 | +}; |
2 | 8 |
|
3 | 9 | use arcstr::ArcStr;
|
| 10 | +use itertools::Itertools as _; |
4 | 11 |
|
5 | 12 | use crate::{
|
6 | 13 | ast::{Arguments, Definition, Document, Field, Fragment, FragmentSpread, Selection, Type},
|
@@ -745,19 +752,33 @@ fn error_message(reason_name: &str, reason: &ConflictReasonMessage) -> String {
|
745 | 752 | )
|
746 | 753 | }
|
747 | 754 |
|
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 | + |
749 | 771 | 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!( |
755 | 777 | r#"subfields "{name}" conflict because {}"#,
|
756 | 778 | format_reason(subreason),
|
757 |
| - ) |
758 |
| - }) |
759 |
| - .collect::<Vec<_>>() |
760 |
| - .join(" and "), |
| 779 | + )) |
| 780 | + }, |
| 781 | + )), |
761 | 782 | }
|
762 | 783 | }
|
763 | 784 |
|
|
0 commit comments