Add write_list fn to type_system_nodes
@@ -197,15 +197,7 @@ pub struct ListValue {
impl fmt::Display for ListValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[")?;
- let mut first = true;
- for item in &self.values {
- if first {
- first = false;
- } else {
- write!(f, ", ")?;
- }
- write!(f, "{}", item)?;
- }
+ write_list(f, &self.values, ", ")?;
write!(f, "]")
}
}
@@ -217,15 +209,7 @@ pub struct ObjectValue {
impl fmt::Display for ObjectValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{{")?;
- let mut first = true;
- for field in &self.fields {
- if first {
- first = false;
- } else {
- write!(f, ", ")?;
- }
- write!(f, "{}: {}", field.name, field.value)?;
- }
+ write_list(f, &self.fields, ", ")?;
write!(f, "}}")
}
}
@@ -236,6 +220,12 @@ pub struct ObjectField {
pub value: Value,
}
+impl fmt::Display for ObjectField {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ write!(f, "{}: {}", self.name, self.value)
+ }
+}
+
#[derive(PartialEq, Debug, Clone)]
pub struct Argument {
pub name: StringKey,
@@ -247,3 +237,16 @@ pub struct Directive {
pub name: StringKey,
pub arguments: Vec<Argument>,
}
+
+fn write_list(
+ f: &mut fmt::Formatter<'_>,
+ list: &[impl fmt::Display],
+ separator: &str,
+) -> fmt::Result {
+ let v = list
+ .iter()
+ .map(|elem| elem.to_string())
+ .collect::<Vec<String>>()
+ .join(separator);
+ write!(f, "{}", v)
+}
