@@ -61,18 +61,18 @@ struct ApiPage {
6161}
6262
6363impl < T > ApiPagedResponse < T > {
64- pub fn new ( data : Vec < T > , next : Option < String > ) -> Self {
65- Self { data, page : ApiPage { next } }
64+ pub fn new ( data : Vec < T > , next : Option < & str > ) -> Self {
65+ Self { data, page : ApiPage { next : next . map ( Into :: into ) } } // Option<&str> -> Option<String>
6666 }
6767
68- pub fn next ( data : Vec < T > , next : Option < String > ) -> Self {
68+ pub fn next ( data : Vec < T > , next : Option < & str > ) -> Self {
6969 Self :: new ( data, next)
7070 }
7171
7272 pub fn of ( data : Vec < T > , limit : usize , next_provider : impl Fn ( & T ) -> String ) -> Self {
7373 if data. len ( ) == limit && data. len ( ) > 0 && limit > 0 {
7474 let next = next_provider ( & data[ limit - 1 ] ) ;
75- Self :: next ( data, Some ( next) )
75+ Self :: next ( data, Some ( next. as_str ( ) ) )
7676 } else {
7777 Self :: next ( data, None )
7878 }
@@ -87,16 +87,26 @@ impl<T> ApiPagedResponse<T> {
8787mod tests {
8888 use super :: { ApiPagedResponse , ApiPage } ;
8989
90+ #[ derive( Clone , Debug ) ]
9091 struct Item {
9192 id : String ,
9293 sort : String ,
9394 }
9495
96+ impl Item {
97+ fn new ( id : & str , sort : & str ) -> Self {
98+ Self {
99+ id : id. into ( ) ,
100+ sort : sort. into ( ) ,
101+ }
102+ }
103+ }
104+
95105 #[ test]
96106 fn should_next_with_none ( ) {
97107 let items: Vec < Item > = vec ! [
98- Item { id : "1" . into ( ) , sort : "a" . into ( ) } ,
99- Item { id : "2" . into ( ) , sort : "b" . into ( ) } ,
108+ Item :: new ( "0" , "a" ) ,
109+ Item :: new ( "1" , "b" ) ,
100110 ] ;
101111
102112 let next = ApiPagedResponse :: next ( items, None ) . page . next ;
@@ -106,34 +116,34 @@ mod tests {
106116 #[ test]
107117 fn should_next_with_value ( ) {
108118 let items: Vec < Item > = vec ! [
109- Item { id : "1" . into ( ) , sort : "a" . into ( ) } ,
110- Item { id : "2" . into ( ) , sort : "b" . into ( ) } ,
119+ Item :: new ( "0" , "a" ) ,
120+ Item :: new ( "1" , "b" ) ,
111121 ] ;
112122
113- let next = ApiPagedResponse :: next ( items, Some ( "b" . into ( ) ) ) . page . next ;
123+ let next = ApiPagedResponse :: next ( items, Some ( "b" ) ) . page . next ;
114124 assert_eq ! ( next, Some ( "b" . into( ) ) ) ;
115125 }
116126
117127 #[ test]
118128 fn should_of_with_limit_3 ( ) {
119129 let items: Vec < Item > = vec ! [
120- Item { id : "1" . into ( ) , sort : "a" . into ( ) } ,
121- Item { id : "2" . into ( ) , sort : "b" . into ( ) } ,
122- Item { id : "3" . into ( ) , sort : "c" . into ( ) } ,
130+ Item :: new ( "0" , "a" ) ,
131+ Item :: new ( "1" , "b" ) ,
132+ Item :: new ( "2" , "c" ) ,
123133 ] ;
124134
125- let next = ApiPagedResponse :: of ( items, 3 , |item| item. sort . to_owned ( ) ) . page . next ;
135+ let next = ApiPagedResponse :: of ( items, 3 , |item| item. clone ( ) . sort ) . page . next ;
126136 assert_eq ! ( next, Some ( "c" . into( ) ) )
127137 }
128138
129139 #[ test]
130140 fn should_not_create_with_limit_3_while_size_2 ( ) {
131141 let items: Vec < Item > = vec ! [
132- Item { id : "1" . into ( ) , sort : "a" . into ( ) } ,
133- Item { id : "2" . into ( ) , sort : "b" . into ( ) } ,
142+ Item :: new ( "0" , "a" ) ,
143+ Item :: new ( "1" , "b" ) ,
134144 ] ;
135145
136- let page = ApiPagedResponse :: of ( items, 3 , |item| item. sort . to_owned ( ) ) . page ;
146+ let page = ApiPagedResponse :: of ( items, 3 , |item| item. clone ( ) . sort ) . page ;
137147 assert_eq ! ( page, ApiPage { next: None } )
138148 }
139149
0 commit comments