Skip to content

Commit 3989b71

Browse files
committed
refine
1 parent 30a52ac commit 3989b71

2 files changed

Lines changed: 30 additions & 22 deletions

File tree

lib/ain-ocean/src/api/block.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ struct BlockHash {
1414
}
1515

1616
#[derive(Deserialize)]
17-
struct ListBlocksRequest {
18-
size: usize,
19-
next: Option<String>
17+
pub struct ListBlocksRequest {
18+
pub size: usize,
19+
pub next: Option<String>
2020
}
2121

2222
#[debug_handler]
@@ -33,7 +33,7 @@ async fn list_blocks(Json(req): Json<ListBlocksRequest>) -> Json<ApiPagedRespons
3333
::of(
3434
blocks,
3535
req.size,
36-
|block| block.clone().id
36+
|block| block.clone().id
3737
)
3838
)
3939
}
@@ -82,5 +82,3 @@ pub struct Block {
8282
// size_stripped: u64,
8383
// weight: u64,
8484
}
85-
86-

lib/ain-ocean/src/api_paged_response.rs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,18 @@ struct ApiPage {
6161
}
6262

6363
impl<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> {
8787
mod 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

Comments
 (0)