From dba575d4a5b016e216f5b260e31d86b34d58e0e5 Mon Sep 17 00:00:00 2001 From: Sunli Date: Fri, 29 May 2020 12:19:08 +0800 Subject: [PATCH] Add Connection::map and Connection::map_node functions --- src/types/connection/connection_type.rs | 59 ++++++++++++++----------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/src/types/connection/connection_type.rs b/src/types/connection/connection_type.rs index a309b488..3dac10c1 100644 --- a/src/types/connection/connection_type.rs +++ b/src/types/connection/connection_type.rs @@ -13,12 +13,7 @@ use std::borrow::Cow; /// Connection type /// /// Connection is the result of a query for `DataSource`. -pub struct Connection< - C: CursorType, - T, - EC: ObjectType + Send = EmptyFields, - EE: ObjectType + Send = EmptyFields, -> { +pub struct Connection { /// All edges of the current page. edges: Vec>, additional_fields: EC, @@ -26,12 +21,7 @@ pub struct Connection< has_next_page: bool, } -impl Connection -where - C: CursorType + Send, - T: OutputValueType + Send, - EE: ObjectType + Send, -{ +impl Connection { /// Create a new connection. pub fn new(has_previous_page: bool, has_next_page: bool) -> Self { Connection { @@ -43,13 +33,7 @@ where } } -impl Connection -where - C: CursorType + Send, - T: OutputValueType + Send, - EC: ObjectType + Send, - EE: ObjectType + Send, -{ +impl Connection { /// Create a new connection, it can have some additional fields. pub fn with_additional_fields( has_previous_page: bool, @@ -65,13 +49,36 @@ where } } -impl Connection -where - C: CursorType, - T: OutputValueType + Send + Sync, - EC: ObjectType + Sync + Send, - EE: ObjectType + Sync + Send, -{ +impl Connection { + /// Convert the edge type and return a new `Connection`. + pub fn map(self, mut f: F) -> Connection + where + F: FnMut(Edge) -> Edge, + { + let mut new_edges = Vec::with_capacity(self.edges.len()); + for edge in self.edges { + new_edges.push(f(edge)); + } + Connection { + edges: new_edges, + additional_fields: self.additional_fields, + has_previous_page: self.has_previous_page, + has_next_page: self.has_next_page, + } + } + + /// Convert the node type and return a new `Connection`. + pub fn map_node(self, mut f: F) -> Connection + where + F: FnMut(T) -> T2, + { + self.map(|edge| Edge { + cursor: edge.cursor, + node: f(edge.node), + additional_fields: edge.additional_fields, + }) + } + /// Append edges with `IntoIterator>` pub fn append(&mut self, iter: I) where