@@ -16,22 +16,14 @@ pub trait CacheObject {
1616
1717/// A response from the transaction cache, containing an item.
1818#[ derive( Debug , Clone , Serialize , Deserialize , PartialEq , Eq ) ]
19- #[ serde( untagged, rename_all_fields = "camelCase" ) ]
20- pub enum CacheResponse < T : CacheObject > {
21- /// A paginated response, containing the inner item and a next cursor.
22- Paginated {
23- /// The actual item.
24- #[ serde( flatten) ]
25- inner : T ,
26- /// The next cursor for pagination, if any.
27- next_cursor : T :: Key ,
28- } ,
29- /// An unpaginated response, containing the actual item.
30- Unpaginated {
31- /// The actual item.
32- #[ serde( flatten) ]
33- inner : T ,
34- } ,
19+ #[ serde( rename_all = "camelCase" ) ]
20+ pub struct CacheResponse < T : CacheObject > {
21+ /// The response.
22+ #[ serde( flatten) ]
23+ inner : T ,
24+ /// The next cursor for pagination, if any.
25+ #[ serde( default , skip_serializing_if = "Option::is_none" ) ]
26+ next_cursor : Option < T :: Key > ,
3527}
3628
3729impl < T : CacheObject > CacheObject for CacheResponse < T > {
@@ -41,35 +33,32 @@ impl<T: CacheObject> CacheObject for CacheResponse<T> {
4133impl < T : CacheObject > CacheResponse < T > {
4234 /// Create a new paginated response from a list of items and a pagination info.
4335 pub const fn paginated ( inner : T , pagination : T :: Key ) -> Self {
44- Self :: Paginated { inner, next_cursor : pagination }
36+ Self { inner, next_cursor : Some ( pagination) }
4537 }
4638
4739 /// Create a new unpaginated response from a list of items.
4840 pub const fn unpaginated ( inner : T ) -> Self {
49- Self :: Unpaginated { inner }
41+ Self { inner, next_cursor : None }
5042 }
5143
5244 /// Return a reference to the inner value.
5345 pub const fn inner ( & self ) -> & T {
5446 match self {
55- Self :: Paginated { inner, .. } => inner,
56- Self :: Unpaginated { inner } => inner,
47+ Self { inner, .. } => inner,
5748 }
5849 }
5950
6051 /// Return a mutable reference to the inner value.
6152 pub const fn inner_mut ( & mut self ) -> & mut T {
6253 match self {
63- Self :: Paginated { inner, .. } => inner,
64- Self :: Unpaginated { inner } => inner,
54+ Self { inner, .. } => inner,
6555 }
6656 }
6757
6858 /// Return the next cursor for pagination, if any.
6959 pub const fn next_cursor ( & self ) -> Option < & T :: Key > {
7060 match self {
71- Self :: Paginated { next_cursor, .. } => Some ( next_cursor) ,
72- Self :: Unpaginated { .. } => None ,
61+ Self { next_cursor, .. } => next_cursor. as_ref ( ) ,
7362 }
7463 }
7564
@@ -80,27 +69,25 @@ impl<T: CacheObject> CacheResponse<T> {
8069
8170 /// Check if the response is paginated.
8271 pub const fn is_paginated ( & self ) -> bool {
83- matches ! ( self , Self :: Paginated { .. } )
72+ self . next_cursor . is_some ( )
8473 }
8574
8675 /// Check if the response is unpaginated.
8776 pub const fn is_unpaginated ( & self ) -> bool {
88- matches ! ( self , Self :: Unpaginated { .. } )
77+ self . next_cursor . is_none ( )
8978 }
9079
9180 /// Get the inner value.
9281 pub fn into_inner ( self ) -> T {
9382 match self {
94- Self :: Paginated { inner, .. } => inner,
95- Self :: Unpaginated { inner } => inner,
83+ Self { inner, .. } => inner,
9684 }
9785 }
9886
9987 /// Consume the response and return the parts.
10088 pub fn into_parts ( self ) -> ( T , Option < T :: Key > ) {
10189 match self {
102- Self :: Paginated { inner, next_cursor } => ( inner, Some ( next_cursor) ) ,
103- Self :: Unpaginated { inner } => ( inner, None ) ,
90+ Self { inner, next_cursor } => ( inner, next_cursor) ,
10491 }
10592 }
10693
@@ -889,9 +876,8 @@ mod tests {
889876
890877 #[ test]
891878 fn test_unpaginated_cache_response_deser ( ) {
892- let cache_response = CacheResponse :: Unpaginated {
893- inner : TxCacheTransactionsResponse { transactions : vec ! [ ] } ,
894- } ;
879+ let cache_response =
880+ CacheResponse :: unpaginated ( TxCacheTransactionsResponse { transactions : vec ! [ ] } ) ;
895881 let expected_json = r#"{"transactions":[]}"# ;
896882 let serialized = serde_json:: to_string ( & cache_response) . unwrap ( ) ;
897883 assert_eq ! ( serialized, expected_json) ;
@@ -903,14 +889,14 @@ mod tests {
903889
904890 #[ test]
905891 fn test_paginated_cache_response_deser ( ) {
906- let cache_response = CacheResponse :: Paginated {
907- inner : TxCacheTransactionsResponse { transactions : vec ! [ ] } ,
908- next_cursor : TxKey {
892+ let cache_response = CacheResponse :: paginated (
893+ TxCacheTransactionsResponse { transactions : vec ! [ ] } ,
894+ TxKey {
909895 txn_hash : B256 :: repeat_byte ( 0xaa ) ,
910896 score : 100 ,
911897 global_transaction_score_key : "gtsk" . to_string ( ) ,
912898 } ,
913- } ;
899+ ) ;
914900 let expected_json = r#"{"transactions":[],"nextCursor":{"txnHash":"0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","score":100,"globalTransactionScoreKey":"gtsk"}}"# ;
915901 let serialized = serde_json:: to_string ( & cache_response) . unwrap ( ) ;
916902 assert_eq ! ( serialized, expected_json) ;
@@ -955,13 +941,11 @@ mod tests {
955941
956942 #[ test]
957943 fn test_unpaginated_cache_bundle_response_deser ( ) {
958- let cache_response = CacheResponse :: Unpaginated {
959- inner : TxCacheBundlesResponse {
960- bundles : vec ! [ dummy_bundle_with_id(
961- Uuid :: from_str( "5932d4bb-58d9-41a9-851d-8dd7f04ccc33" ) . unwrap( ) ,
962- ) ] ,
963- } ,
964- } ;
944+ let cache_response = CacheResponse :: unpaginated ( TxCacheBundlesResponse {
945+ bundles : vec ! [ dummy_bundle_with_id(
946+ Uuid :: from_str( "5932d4bb-58d9-41a9-851d-8dd7f04ccc33" ) . unwrap( ) ,
947+ ) ] ,
948+ } ) ;
965949 let expected_json = r#"{"bundles":[{"id":"5932d4bb-58d9-41a9-851d-8dd7f04ccc33","bundle":{"txs":[],"blockNumber":"0x0","replacementUuid":"5932d4bb-58d9-41a9-851d-8dd7f04ccc33"}}]}"# ;
966950 let serialized = serde_json:: to_string ( & cache_response) . unwrap ( ) ;
967951 assert_eq ! ( serialized, expected_json) ;
@@ -974,14 +958,10 @@ mod tests {
974958 fn test_paginated_cache_bundle_response_deser ( ) {
975959 let uuid = Uuid :: from_str ( "5932d4bb-58d9-41a9-851d-8dd7f04ccc33" ) . unwrap ( ) ;
976960
977- let cache_response = CacheResponse :: Paginated {
978- inner : TxCacheBundlesResponse { bundles : vec ! [ dummy_bundle_with_id( uuid) ] } ,
979- next_cursor : BundleKey {
980- id : uuid,
981- score : 100 ,
982- global_bundle_score_key : "gbsk" . to_string ( ) ,
983- } ,
984- } ;
961+ let cache_response = CacheResponse :: paginated (
962+ TxCacheBundlesResponse { bundles : vec ! [ dummy_bundle_with_id( uuid) ] } ,
963+ BundleKey { id : uuid, score : 100 , global_bundle_score_key : "gbsk" . to_string ( ) } ,
964+ ) ;
985965 let expected_json = r#"{"bundles":[{"id":"5932d4bb-58d9-41a9-851d-8dd7f04ccc33","bundle":{"txs":[],"blockNumber":"0x0","replacementUuid":"5932d4bb-58d9-41a9-851d-8dd7f04ccc33"}}],"nextCursor":{"id":"5932d4bb-58d9-41a9-851d-8dd7f04ccc33","score":100,"globalBundleScoreKey":"gbsk"}}"# ;
986966 let serialized = serde_json:: to_string ( & cache_response) . unwrap ( ) ;
987967 dbg ! ( & serialized) ;
0 commit comments