@@ -5,17 +5,17 @@ use serde::Serialize;
55/// Each ApiPagedResponse holds the data array and the "token" for next part of the slice.
66/// The next token should be passed via query 'next' and only used when getting the next slice.
77/// Hence the first request, the next token is always empty and not provided.
8- ///
8+ ///
99/// With ascending sorted list and a limit of 3 items per slice will have the behaviour as such.
10- ///
10+ ///
1111/// SORTED : | [1] [2] [3] | [4] [5] [6] | [7] [8] [9] | [10]
1212/// Query 1 : Data: [1] [2] [3], Next: 3, Operator: GT (>)
1313/// Query 2 : Data: [4] [5] [6], Next: 6, Operator: GT (>)
1414/// Query 3 : Data: [7] [8] [9], Next: 3, Operator: GT (>)
1515/// Query 4 : Data: [10], Next: undefined
16- ///
16+ ///
1717/// This design is resilient also mutating sorted list, where pagination is not.
18- ///
18+ ///
1919/// SORTED : [2] [4] [6] [8] [10] [12] [14]
2020/// Query 1 : Data: [2] [4] [6], Next: 6, Operator: GT (>)
2121///
@@ -27,7 +27,7 @@ use serde::Serialize;
2727/// Limitations of this requires your dat astructure to always be sorted in one direction and your sort
2828/// indexes always fixed. Hence the moving down of the slice window, your operator will be greater than (GT).
2929/// While moving up your operator will be less than (GT).
30- ///
30+ ///
3131/// ASC : | [1] [2] [3] | [4] [5] [6] | [7] [8] [9] |
3232/// >3 >6 >9
3333/// DESC : | [9] [8] [7] | [6] [5] [4] | [3] [2] [1] |
@@ -37,15 +37,15 @@ use serde::Serialize;
3737/// when the usage narrative is clear and so will the use of ease. LIST query must be dead simple.
3838/// Image travelling down the path, and getting a "next token" to get the next set of itmes to
3939/// continue walking.
40- ///
40+ ///
4141/// Because the limit is not part of the slice window your query mechanism should support varying size windows.
42- ///
42+ ///
4343/// DATA: | [1] [2] [3] | [4] [5] [6] [7] | [8] [9] | ...
4444/// | limit 3, >3 | limit 4, >7 | limit 2, >9
4545/// For simplicity your API should not attempt to allow access to different sort indexes, be cognizant of
4646/// how our APIs are consumed. If we create a GET /blocks operation to list blocks what would the correct indexes
4747/// be 99% of the time?
48- ///
48+ ///
4949/// Answer: Blocks sorted by height in descending order, that's your sorted list and your slice window.
5050/// : <- Latest | [100] [99] [98] [97] [...] | Oldest ->
5151///
@@ -62,7 +62,12 @@ struct ApiPage {
6262
6363impl < T > ApiPagedResponse < T > {
6464 pub fn new ( data : Vec < T > , next : Option < & str > ) -> Self {
65- Self { data, page : ApiPage { next : next. map ( Into :: into) } } // Option<&str> -> Option<String>
65+ Self {
66+ data,
67+ page : ApiPage {
68+ next : next. map ( Into :: into) ,
69+ } ,
70+ } // Option<&str> -> Option<String>
6671 }
6772
6873 pub fn next ( data : Vec < T > , next : Option < & str > ) -> Self {
@@ -77,22 +82,22 @@ impl<T> ApiPagedResponse<T> {
7782 Self :: next ( data, None )
7883 }
7984 }
80-
85+
8186 pub fn empty ( ) -> Self {
8287 Self :: new ( Vec :: new ( ) , None )
8388 }
8489}
8590
8691#[ cfg( test) ]
8792mod tests {
88- use super :: { ApiPagedResponse , ApiPage } ;
89-
93+ use super :: { ApiPage , ApiPagedResponse } ;
94+
9095 #[ derive( Clone , Debug ) ]
9196 struct Item {
9297 id : String ,
9398 sort : String ,
9499 }
95-
100+
96101 impl Item {
97102 fn new ( id : & str , sort : & str ) -> Self {
98103 Self {
@@ -101,25 +106,19 @@ mod tests {
101106 }
102107 }
103108 }
104-
109+
105110 #[ test]
106111 fn should_next_with_none ( ) {
107- let items: Vec < Item > = vec ! [
108- Item :: new( "0" , "a" ) ,
109- Item :: new( "1" , "b" ) ,
110- ] ;
111-
112+ let items: Vec < Item > = vec ! [ Item :: new( "0" , "a" ) , Item :: new( "1" , "b" ) ] ;
113+
112114 let next = ApiPagedResponse :: next ( items, None ) . page . next ;
113115 assert_eq ! ( next, None ) ;
114116 }
115117
116118 #[ test]
117119 fn should_next_with_value ( ) {
118- let items: Vec < Item > = vec ! [
119- Item :: new( "0" , "a" ) ,
120- Item :: new( "1" , "b" ) ,
121- ] ;
122-
120+ let items: Vec < Item > = vec ! [ Item :: new( "0" , "a" ) , Item :: new( "1" , "b" ) ] ;
121+
123122 let next = ApiPagedResponse :: next ( items, Some ( "b" ) ) . page . next ;
124123 assert_eq ! ( next, Some ( "b" . into( ) ) ) ;
125124 }
@@ -131,20 +130,18 @@ mod tests {
131130 Item :: new( "1" , "b" ) ,
132131 Item :: new( "2" , "c" ) ,
133132 ] ;
134-
135- let next = ApiPagedResponse :: of ( items, 3 , |item| item. clone ( ) . sort ) . page . next ;
133+
134+ let next = ApiPagedResponse :: of ( items, 3 , |item| item. clone ( ) . sort )
135+ . page
136+ . next ;
136137 assert_eq ! ( next, Some ( "c" . into( ) ) )
137138 }
138-
139+
139140 #[ test]
140141 fn should_not_create_with_limit_3_while_size_2 ( ) {
141- let items: Vec < Item > = vec ! [
142- Item :: new( "0" , "a" ) ,
143- Item :: new( "1" , "b" ) ,
144- ] ;
145-
142+ let items: Vec < Item > = vec ! [ Item :: new( "0" , "a" ) , Item :: new( "1" , "b" ) ] ;
143+
146144 let page = ApiPagedResponse :: of ( items, 3 , |item| item. clone ( ) . sort ) . page ;
147- assert_eq ! ( page, ApiPage { next: None } )
145+ assert_eq ! ( page, ApiPage { next: None } )
148146 }
149-
150147}
0 commit comments