Skip to content

Commit 99e7ad8

Browse files
committed
controllers::keyword: Use json! macro to simplify JSON serialization code
1 parent 43784d6 commit 99e7ad8

File tree

1 file changed

+10
-21
lines changed

1 file changed

+10
-21
lines changed

src/controllers/keyword.rs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,15 @@ pub fn index(req: &mut dyn RequestExt) -> EndpointResult {
2424
let conn = req.db_read_only()?;
2525
let data: Paginated<Keyword> = query.load(&*conn)?;
2626
let total = data.total();
27-
let kws = data.into_iter().map(Keyword::into).collect::<Vec<_>>();
28-
29-
#[derive(Serialize)]
30-
struct R {
31-
keywords: Vec<EncodableKeyword>,
32-
meta: Meta,
33-
}
34-
#[derive(Serialize)]
35-
struct Meta {
36-
total: Option<i64>,
37-
}
38-
39-
Ok(req.json(&R {
40-
keywords: kws,
41-
meta: Meta { total: Some(total) },
42-
}))
27+
let kws = data
28+
.into_iter()
29+
.map(Keyword::into)
30+
.collect::<Vec<EncodableKeyword>>();
31+
32+
Ok(req.json(&json!({
33+
"keywords": kws,
34+
"meta": { "total": total },
35+
})))
4336
}
4437

4538
/// Handles the `GET /keywords/:keyword_id` route.
@@ -49,9 +42,5 @@ pub fn show(req: &mut dyn RequestExt) -> EndpointResult {
4942

5043
let kw = Keyword::find_by_keyword(&conn, name)?;
5144

52-
#[derive(Serialize)]
53-
struct R {
54-
keyword: EncodableKeyword,
55-
}
56-
Ok(req.json(&R { keyword: kw.into() }))
45+
Ok(req.json(&json!({ "keyword": EncodableKeyword::from(kw) })))
5746
}

0 commit comments

Comments
 (0)