From da37e26e5fabd7137c6499e55fb35c4cb90fa1e3 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Fri, 3 Sep 2021 21:22:09 +0200 Subject: [PATCH 01/19] errors::json: Use `json!` macro to simplify JSON serialization code --- src/util/errors/json.rs | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/util/errors/json.rs b/src/util/errors/json.rs index 3622f41706f..81a535e932c 100644 --- a/src/util/errors/json.rs +++ b/src/util/errors/json.rs @@ -8,18 +8,9 @@ use conduit::{header, StatusCode}; /// Generates a response with the provided status and description as JSON fn json_error(detail: &str, status: StatusCode) -> AppResponse { - #[derive(Serialize)] - struct StringError<'a> { - detail: &'a str, - } - #[derive(Serialize)] - struct Bad<'a> { - errors: [StringError<'a>; 1], - } + let json = json!({ "errors": [{ "detail": detail }] }); - let mut response = json_response(&Bad { - errors: [StringError { detail }], - }); + let mut response = json_response(&json); *response.status_mut() = status; response } From f285b17f710a2e2908f0806ab41a3af2c0d0c7e1 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Fri, 3 Sep 2021 21:23:12 +0200 Subject: [PATCH 02/19] controllers::helpers: Use `json!` macro to simplify JSON serialization code --- src/controllers/helpers.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/controllers/helpers.rs b/src/controllers/helpers.rs index 281e8436b62..54576f44f44 100644 --- a/src/controllers/helpers.rs +++ b/src/controllers/helpers.rs @@ -5,10 +5,6 @@ pub(crate) mod pagination; pub(crate) use self::pagination::Paginate; pub fn ok_true() -> EndpointResult { - #[derive(Serialize)] - struct R { - ok: bool, - } - - Ok(json_response(&R { ok: true })) + let json = json!({ "ok": true }); + Ok(json_response(&json)) } From 7c843f20532ef857369671d8b6af87231b38dcf2 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Fri, 3 Sep 2021 21:26:09 +0200 Subject: [PATCH 03/19] controllers::krate::downloads: Use `json!` macro to simplify JSON serialization code --- src/controllers/krate/downloads.rs | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/src/controllers/krate/downloads.rs b/src/controllers/krate/downloads.rs index 49925deff3b..9c34f659284 100644 --- a/src/controllers/krate/downloads.rs +++ b/src/controllers/krate/downloads.rs @@ -32,7 +32,7 @@ pub fn downloads(req: &mut dyn RequestExt) -> EndpointResult { .load(&*conn)? .into_iter() .map(VersionDownload::into) - .collect::>(); + .collect::>(); let sum_downloads = sql::("SUM(version_downloads.downloads)"); let extra: Vec = VersionDownload::belonging_to(rest) @@ -50,20 +50,11 @@ pub fn downloads(req: &mut dyn RequestExt) -> EndpointResult { date: String, downloads: i64, } - #[derive(Serialize)] - struct R { - version_downloads: Vec, - meta: Meta, - } - #[derive(Serialize)] - struct Meta { - extra_downloads: Vec, - } - let meta = Meta { - extra_downloads: extra, - }; - Ok(req.json(&R { - version_downloads: downloads, - meta, - })) + + Ok(req.json(&json!({ + "version_downloads": downloads, + "meta": { + "extra_downloads": extra, + }, + }))) } From 263c2d17c75f970b10fa2b4e0f92ad71781a4968 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Fri, 3 Sep 2021 21:28:00 +0200 Subject: [PATCH 04/19] controllers::krate::follow: Use `json!` macro to simplify JSON serialization code --- src/controllers/krate/follow.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/controllers/krate/follow.rs b/src/controllers/krate/follow.rs index 170143b30e9..7381208fa6e 100644 --- a/src/controllers/krate/follow.rs +++ b/src/controllers/krate/follow.rs @@ -49,11 +49,8 @@ pub fn following(req: &mut dyn RequestExt) -> EndpointResult { let user_id = req.authenticate()?.forbid_api_token_auth()?.user_id(); let conn = req.db_read_only()?; let follow = follow_target(req, &conn, user_id)?; - let following = diesel::select(exists(follows::table.find(follow.id()))).get_result(&*conn)?; + let following = + diesel::select(exists(follows::table.find(follow.id()))).get_result::(&*conn)?; - #[derive(Serialize)] - struct R { - following: bool, - } - Ok(req.json(&R { following })) + Ok(req.json(&json!({ "following": following }))) } From e36d08ea95590284e4a52f4adaa642549cbedb40 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Fri, 3 Sep 2021 21:34:55 +0200 Subject: [PATCH 05/19] controllers::krate::metadata: Use `json!` macro to simplify JSON serialization code --- src/controllers/krate/metadata.rs | 99 ++++++++++--------------------- 1 file changed, 31 insertions(+), 68 deletions(-) diff --git a/src/controllers/krate/metadata.rs b/src/controllers/krate/metadata.rs index 85c0d36f0c0..3e3c64c308e 100644 --- a/src/controllers/krate/metadata.rs +++ b/src/controllers/krate/metadata.rs @@ -25,8 +25,8 @@ pub fn summary(req: &mut dyn RequestExt) -> EndpointResult { use crate::schema::crates::dsl::*; let conn = req.db_read_only()?; - let num_crates = crates.count().get_result(&*conn)?; - let num_downloads = metadata::table + let num_crates: i64 = crates.count().get_result(&*conn)?; + let num_downloads: i64 = metadata::table .select(metadata::total_downloads) .get_result(&*conn)?; @@ -89,34 +89,23 @@ pub fn summary(req: &mut dyn RequestExt) -> EndpointResult { .load(&*conn)? .into_iter() .map(Keyword::into) - .collect(); + .collect::>(); let popular_categories = Category::toplevel(&conn, "crates", 10, 0)? .into_iter() .map(Category::into) - .collect(); - - #[derive(Serialize)] - struct R { - num_downloads: i64, - num_crates: i64, - new_crates: Vec, - most_downloaded: Vec, - most_recently_downloaded: Vec, - just_updated: Vec, - popular_keywords: Vec, - popular_categories: Vec, - } - Ok(req.json(&R { - num_downloads, - num_crates, - new_crates: encode_crates(new_crates)?, - most_downloaded: encode_crates(most_downloaded)?, - most_recently_downloaded: encode_crates(most_recently_downloaded)?, - just_updated: encode_crates(just_updated)?, - popular_keywords, - popular_categories, - })) + .collect::>(); + + Ok(req.json(&json!({ + "num_downloads": num_downloads, + "num_crates": num_crates, + "new_crates": encode_crates(new_crates)?, + "most_downloaded": encode_crates(most_downloaded)?, + "most_recently_downloaded": encode_crates(most_recently_downloaded)?, + "just_updated": encode_crates(just_updated)?, + "popular_keywords": popular_keywords, + "popular_categories": popular_categories, + }))) } /// Handles the `GET /crates/:crate_id` route. @@ -167,16 +156,8 @@ pub fn show(req: &mut dyn RequestExt) -> EndpointResult { .load(&*conn)?; let top_versions = krate.top_versions(&conn)?; - #[derive(Serialize)] - struct R { - #[serde(rename = "crate")] - krate: EncodableCrate, - versions: Vec, - keywords: Vec, - categories: Vec, - } - Ok(req.json(&R { - krate: EncodableCrate::from( + Ok(req.json(&json!({ + "crate": EncodableCrate::from( krate.clone(), &top_versions, Some(ids), @@ -186,13 +167,13 @@ pub fn show(req: &mut dyn RequestExt) -> EndpointResult { false, recent_downloads, ), - versions: versions_publishers_and_audit_actions + "versions": versions_publishers_and_audit_actions .into_iter() .map(|(v, pb, aas)| EncodableVersion::from(v, &krate.name, pb, aas)) - .collect(), - keywords: kws.into_iter().map(Keyword::into).collect(), - categories: cats.into_iter().map(Category::into).collect(), - })) + .collect::>(), + "keywords": kws.into_iter().map(Keyword::into).collect::>(), + "categories": cats.into_iter().map(Category::into).collect::>(), + }))) } /// Handles the `GET /crates/:crate_id/:version/readme` route. @@ -207,11 +188,7 @@ pub fn readme(req: &mut dyn RequestExt) -> EndpointResult { .readme_location(crate_name, version); if req.wants_json() { - #[derive(Serialize)] - struct R { - url: String, - } - Ok(req.json(&R { url: redirect_url })) + Ok(req.json(&json!({ "url": redirect_url }))) } else { Ok(req.redirect(redirect_url)) } @@ -242,13 +219,9 @@ pub fn versions(req: &mut dyn RequestExt) -> EndpointResult { .into_iter() .zip(VersionOwnerAction::for_versions(&conn, &versions)?.into_iter()) .map(|((v, pb), aas)| EncodableVersion::from(v, crate_name, pb, aas)) - .collect(); + .collect::>(); - #[derive(Serialize)] - struct R { - versions: Vec, - } - Ok(req.json(&R { versions })) + Ok(req.json(&json!({ "versions": versions }))) } /// Handles the `GET /crates/:crate_id/reverse_dependencies` route. @@ -288,21 +261,11 @@ pub fn reverse_dependencies(req: &mut dyn RequestExt) -> EndpointResult { .map(|((version, krate_name, published_by), actions)| { EncodableVersion::from(version, &krate_name, published_by, actions) }) - .collect(); + .collect::>(); - #[derive(Serialize)] - struct R { - dependencies: Vec, - versions: Vec, - meta: Meta, - } - #[derive(Serialize)] - struct Meta { - total: i64, - } - Ok(req.json(&R { - dependencies: rev_deps, - versions, - meta: Meta { total }, - })) + Ok(req.json(&json!({ + "dependencies": rev_deps, + "versions": versions, + "meta": { "total": total }, + }))) } From f2b7e53eb8d4ddcdfc05733b546a10830cb86e85 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Fri, 3 Sep 2021 21:37:42 +0200 Subject: [PATCH 06/19] controllers::krate::owners: Use `json!` macro to simplify JSON serialization code --- src/controllers/krate/owners.rs | 38 ++++++++++----------------------- 1 file changed, 11 insertions(+), 27 deletions(-) diff --git a/src/controllers/krate/owners.rs b/src/controllers/krate/owners.rs index f038b8be5ac..3e835ea7767 100644 --- a/src/controllers/krate/owners.rs +++ b/src/controllers/krate/owners.rs @@ -9,13 +9,13 @@ pub fn owners(req: &mut dyn RequestExt) -> EndpointResult { let crate_name = &req.params()["crate_id"]; let conn = req.db_read_only()?; let krate: Crate = Crate::by_name(crate_name).first(&*conn)?; - let owners = krate.owners(&conn)?.into_iter().map(Owner::into).collect(); + let owners = krate + .owners(&conn)? + .into_iter() + .map(Owner::into) + .collect::>(); - #[derive(Serialize)] - struct R { - users: Vec, - } - Ok(req.json(&R { users: owners })) + Ok(req.json(&json!({ "users": owners }))) } /// Handles the `GET /crates/:crate_id/owner_team` route. @@ -26,13 +26,9 @@ pub fn owner_team(req: &mut dyn RequestExt) -> EndpointResult { let owners = Team::owning(&krate, &conn)? .into_iter() .map(Owner::into) - .collect(); + .collect::>(); - #[derive(Serialize)] - struct R { - teams: Vec, - } - Ok(req.json(&R { teams: owners })) + Ok(req.json(&json!({ "teams": owners }))) } /// Handles the `GET /crates/:crate_id/owner_user` route. @@ -43,13 +39,9 @@ pub fn owner_user(req: &mut dyn RequestExt) -> EndpointResult { let owners = User::owning(&krate, &conn)? .into_iter() .map(Owner::into) - .collect(); + .collect::>(); - #[derive(Serialize)] - struct R { - users: Vec, - } - Ok(req.json(&R { users: owners })) + Ok(req.json(&json!({ "users": owners }))) } /// Handles the `PUT /crates/:crate_id/owners` route. @@ -135,14 +127,6 @@ fn modify_owners(req: &mut dyn RequestExt, add: bool) -> EndpointResult { "owners successfully removed".to_owned() }; - #[derive(Serialize)] - struct R { - ok: bool, - msg: String, - } - Ok(req.json(&R { - ok: true, - msg: comma_sep_msg, - })) + Ok(req.json(&json!({ "ok": true, "msg": comma_sep_msg }))) }) } From 0424d29cde49eed3f68adeb91de44b3da6988c7c Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Fri, 3 Sep 2021 21:40:54 +0200 Subject: [PATCH 07/19] controllers::krate::search: Use `json!` macro to simplify JSON serialization code --- src/controllers/krate/search.rs | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/src/controllers/krate/search.rs b/src/controllers/krate/search.rs index 6c9bc23da1d..de966f658b3 100644 --- a/src/controllers/krate/search.rs +++ b/src/controllers/krate/search.rs @@ -338,28 +338,16 @@ pub fn search(req: &mut dyn RequestExt) -> EndpointResult { ) }, ) - .collect(); - - #[derive(Serialize)] - struct R { - crates: Vec, - meta: Meta, - } - #[derive(Serialize)] - struct Meta { - total: Option, - next_page: Option, - prev_page: Option, - } + .collect::>(); - Ok(req.json(&R { - crates, - meta: Meta { - total: Some(total), - next_page, - prev_page, + Ok(req.json(&json!({ + "crates": crates, + "meta": { + "total": total, + "next_page": next_page, + "prev_page": prev_page, }, - })) + }))) } diesel_infix_operator!(Contains, "@>"); From 103f23c3d720f37827aaee8c0420c6bb6426b89d Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Fri, 3 Sep 2021 21:42:19 +0200 Subject: [PATCH 08/19] controllers::user::me: Use `json!` macro to simplify JSON serialization code --- src/controllers/user/me.rs | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/controllers/user/me.rs b/src/controllers/user/me.rs index 43c982f0a01..7a1ef8b8bb8 100644 --- a/src/controllers/user/me.rs +++ b/src/controllers/user/me.rs @@ -83,21 +83,12 @@ pub fn updates(req: &mut dyn RequestExt) -> EndpointResult { .map(|(version, crate_name, published_by, actions)| { EncodableVersion::from(version, &crate_name, published_by, actions) }) - .collect(); + .collect::>(); - #[derive(Serialize)] - struct R { - versions: Vec, - meta: Meta, - } - #[derive(Serialize)] - struct Meta { - more: bool, - } - Ok(req.json(&R { - versions, - meta: Meta { more }, - })) + Ok(req.json(&json!({ + "versions": versions, + "meta": { "more": more }, + }))) } /// Handles the `PUT /users/:user_id` route. From 611c5dbed99579e6425a8b5c681020abb907e510 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Fri, 3 Sep 2021 21:42:27 +0200 Subject: [PATCH 09/19] controllers::user::other: Use `json!` macro to simplify JSON serialization code --- src/controllers/user/other.rs | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/controllers/user/other.rs b/src/controllers/user/other.rs index 28be3361f48..e80b5724b7c 100644 --- a/src/controllers/user/other.rs +++ b/src/controllers/user/other.rs @@ -16,11 +16,7 @@ pub fn show(req: &mut dyn RequestExt) -> EndpointResult { .order(id.desc()) .first(&*conn)?; - #[derive(Serialize)] - struct R { - user: EncodablePublicUser, - } - Ok(req.json(&R { user: user.into() })) + Ok(req.json(&json!({ "user": EncodablePublicUser::from(user) }))) } /// Handles the `GET /users/:user_id/stats` route. @@ -39,11 +35,5 @@ pub fn stats(req: &mut dyn RequestExt) -> EndpointResult { .first::>(&*conn)? .unwrap_or(0); - #[derive(Serialize)] - struct R { - total_downloads: i64, - } - Ok(req.json(&R { - total_downloads: data, - })) + Ok(req.json(&json!({ "total_downloads": data }))) } From 6438e7bd56a47bd5c23546bc3a1137d2e0ad94c3 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Fri, 3 Sep 2021 21:45:27 +0200 Subject: [PATCH 10/19] controllers::user::session: Use `json!` macro to simplify JSON serialization code --- src/controllers/user/session.rs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/controllers/user/session.rs b/src/controllers/user/session.rs index a717ebf5b5d..d4f181976ce 100644 --- a/src/controllers/user/session.rs +++ b/src/controllers/user/session.rs @@ -36,15 +36,7 @@ pub fn begin(req: &mut dyn RequestExt) -> EndpointResult { req.session_mut() .insert("github_oauth_state".to_string(), state.clone()); - #[derive(Serialize)] - struct R { - url: String, - state: String, - } - Ok(req.json(&R { - url: url.to_string(), - state, - })) + Ok(req.json(&json!({ "url": url.to_string(), "state": state }))) } /// Handles the `GET /api/private/session/authorize` route. From 37f111a5cbc8a0d83aa514f9dd3bccb9c6a32164 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Fri, 3 Sep 2021 21:45:35 +0200 Subject: [PATCH 11/19] controllers::version::deprecated: Use `json!` macro to simplify JSON serialization code --- src/controllers/version/deprecated.rs | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/src/controllers/version/deprecated.rs b/src/controllers/version/deprecated.rs index 1c899b4697b..06d9d5ad581 100644 --- a/src/controllers/version/deprecated.rs +++ b/src/controllers/version/deprecated.rs @@ -43,13 +43,9 @@ pub fn index(req: &mut dyn RequestExt) -> EndpointResult { .map(|((version, crate_name, published_by), actions)| { EncodableVersion::from(version, &crate_name, published_by, actions) }) - .collect(); + .collect::>(); - #[derive(Serialize)] - struct R { - versions: Vec, - } - Ok(req.json(&R { versions })) + Ok(req.json(&json!({ "versions": versions }))) } /// Handles the `GET /versions/:version_id` route. @@ -71,11 +67,6 @@ pub fn show_by_id(req: &mut dyn RequestExt) -> EndpointResult { .first(&*conn)?; let audit_actions = VersionOwnerAction::by_version(&conn, &version)?; - #[derive(Serialize)] - struct R { - version: EncodableVersion, - } - Ok(req.json(&R { - version: EncodableVersion::from(version, &krate.name, published_by, audit_actions), - })) + let version = EncodableVersion::from(version, &krate.name, published_by, audit_actions); + Ok(req.json(&json!({ "version": version }))) } From 9fd5d56bcfe2c004d8b488695742426b38be5dfc Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Fri, 3 Sep 2021 21:45:42 +0200 Subject: [PATCH 12/19] controllers::version::downloads: Use `json!` macro to simplify JSON serialization code --- src/controllers/version/downloads.rs | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/src/controllers/version/downloads.rs b/src/controllers/version/downloads.rs index 9b85a7e987e..92fca6e47ae 100644 --- a/src/controllers/version/downloads.rs +++ b/src/controllers/version/downloads.rs @@ -99,11 +99,7 @@ pub fn download(req: &mut dyn RequestExt) -> EndpointResult { } if req.wants_json() { - #[derive(Serialize)] - struct R { - url: String, - } - Ok(req.json(&R { url: redirect_url })) + Ok(req.json(&json!({ "url": redirect_url }))) } else { Ok(req.redirect(redirect_url)) } @@ -129,13 +125,7 @@ pub fn downloads(req: &mut dyn RequestExt) -> EndpointResult { .load(&*conn)? .into_iter() .map(VersionDownload::into) - .collect(); + .collect::>(); - #[derive(Serialize)] - struct R { - version_downloads: Vec, - } - Ok(req.json(&R { - version_downloads: downloads, - })) + Ok(req.json(&json!({ "version_downloads": downloads }))) } From c39e7d63b563ceeb1569c09248b517bf05ff0a4c Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Fri, 3 Sep 2021 21:49:05 +0200 Subject: [PATCH 13/19] controllers::version::metadata: Use `json!` macro to simplify JSON serialization code --- src/controllers/version/metadata.rs | 37 ++++++++--------------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/src/controllers/version/metadata.rs b/src/controllers/version/metadata.rs index 5c531432efd..e23b269d113 100644 --- a/src/controllers/version/metadata.rs +++ b/src/controllers/version/metadata.rs @@ -7,7 +7,7 @@ use crate::controllers::frontend_prelude::*; use crate::models::VersionOwnerAction; -use crate::views::{EncodableDependency, EncodablePublicUser, EncodableVersion}; +use crate::views::{EncodableDependency, EncodableVersion}; use super::{extract_crate_name_and_semver, version_and_crate}; @@ -26,32 +26,20 @@ pub fn dependencies(req: &mut dyn RequestExt) -> EndpointResult { let deps = deps .into_iter() .map(|(dep, crate_name)| EncodableDependency::from_dep(dep, &crate_name)) - .collect(); + .collect::>(); - #[derive(Serialize)] - struct R { - dependencies: Vec, - } - Ok(req.json(&R { dependencies: deps })) + Ok(req.json(&json!({ "dependencies": deps }))) } /// Handles the `GET /crates/:crate_id/:version/authors` route. pub fn authors(req: &mut dyn RequestExt) -> EndpointResult { // Currently we return the empty list. // Because the API is not used anymore after RFC https://github.com/rust-lang/rfcs/pull/3052. - #[derive(Serialize)] - struct R { - users: Vec, - meta: Meta, - } - #[derive(Serialize)] - struct Meta { - names: Vec, - } - Ok(req.json(&R { - users: vec![], - meta: Meta { names: vec![] }, - })) + + Ok(req.json(&json!({ + "users": [], + "meta": { "names": [] }, + }))) } /// Handles the `GET /crates/:crate/:version` route. @@ -65,11 +53,6 @@ pub fn show(req: &mut dyn RequestExt) -> EndpointResult { let published_by = version.published_by(&conn); let actions = VersionOwnerAction::by_version(&conn, &version)?; - #[derive(Serialize)] - struct R { - version: EncodableVersion, - } - Ok(req.json(&R { - version: EncodableVersion::from(version, &krate.name, published_by, actions), - })) + let version = EncodableVersion::from(version, &krate.name, published_by, actions); + Ok(req.json(&json!({ "version": version }))) } From 39283e6665caf27bc1928584ba94b5af3a53e443 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Fri, 3 Sep 2021 21:49:12 +0200 Subject: [PATCH 14/19] controllers::category: Use `json!` macro to simplify JSON serialization code --- src/controllers/category.rs | 41 ++++++++++--------------------------- 1 file changed, 11 insertions(+), 30 deletions(-) diff --git a/src/controllers/category.rs b/src/controllers/category.rs index be774c9a623..7647142c86c 100644 --- a/src/controllers/category.rs +++ b/src/controllers/category.rs @@ -18,25 +18,18 @@ pub fn index(req: &mut dyn RequestExt) -> EndpointResult { let conn = req.db_read_only()?; let categories = Category::toplevel(&conn, sort, i64::from(options.per_page), i64::from(offset))?; - let categories = categories.into_iter().map(Category::into).collect(); + let categories = categories + .into_iter() + .map(Category::into) + .collect::>(); // Query for the total count of categories let total = Category::count_toplevel(&conn)?; - #[derive(Serialize)] - struct R { - categories: Vec, - meta: Meta, - } - #[derive(Serialize)] - struct Meta { - total: i64, - } - - Ok(req.json(&R { - categories, - meta: Meta { total }, - })) + Ok(req.json(&json!({ + "categories": categories, + "meta": { "total": total }, + }))) } /// Handles the `GET /categories/:category_id` route. @@ -67,19 +60,13 @@ pub fn show(req: &mut dyn RequestExt) -> EndpointResult { parent_categories: parents, }; - #[derive(Serialize)] - struct R { - category: EncodableCategoryWithSubcategories, - } - Ok(req.json(&R { - category: cat_with_subcats, - })) + Ok(req.json(&json!({ "category": cat_with_subcats }))) } /// Handles the `GET /category_slugs` route. pub fn slugs(req: &mut dyn RequestExt) -> EndpointResult { let conn = req.db_read_only()?; - let slugs = categories::table + let slugs: Vec = categories::table .select((categories::slug, categories::slug, categories::description)) .order(categories::slug) .load(&*conn)?; @@ -91,11 +78,5 @@ pub fn slugs(req: &mut dyn RequestExt) -> EndpointResult { description: String, } - #[derive(Serialize)] - struct R { - category_slugs: Vec, - } - Ok(req.json(&R { - category_slugs: slugs, - })) + Ok(req.json(&json!({ "category_slugs": slugs }))) } From 43784d6a98d4f4ccc1747b585ddcf353eae47480 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Fri, 3 Sep 2021 21:56:07 +0200 Subject: [PATCH 15/19] controllers::crate_owner_invitation: Use `json!` macro to simplify JSON serialization code --- src/controllers/crate_owner_invitation.rs | 35 +++++++---------------- 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/src/controllers/crate_owner_invitation.rs b/src/controllers/crate_owner_invitation.rs index 04db1561d13..91d7792aef5 100644 --- a/src/controllers/crate_owner_invitation.rs +++ b/src/controllers/crate_owner_invitation.rs @@ -44,15 +44,10 @@ pub fn list(req: &mut dyn RequestExt) -> EndpointResult { }) .collect::>>()?; - #[derive(Serialize)] - struct R { - crate_owner_invitations: Vec, - users: Vec, - } - Ok(req.json(&R { - crate_owner_invitations, - users, - })) + Ok(req.json(&json!({ + "crate_owner_invitations": crate_owner_invitations, + "users": users, + }))) } /// Handles the `GET /api/private/crate_owner_invitations` route. @@ -271,13 +266,7 @@ pub fn handle_invite(req: &mut dyn RequestExt) -> EndpointResult { invitation.decline(conn)?; } - #[derive(Serialize)] - struct R { - crate_owner_invitation: InvitationResponse, - } - Ok(req.json(&R { - crate_owner_invitation: crate_invite, - })) + Ok(req.json(&json!({ "crate_owner_invitation": crate_invite }))) } /// Handles the `PUT /api/v1/me/crate_owner_invitations/accept/:token` route. @@ -290,14 +279,10 @@ pub fn handle_invite_with_token(req: &mut dyn RequestExt) -> EndpointResult { let crate_id = invitation.crate_id; invitation.accept(&conn, config)?; - #[derive(Serialize)] - struct R { - crate_owner_invitation: InvitationResponse, - } - Ok(req.json(&R { - crate_owner_invitation: InvitationResponse { - crate_id, - accepted: true, + Ok(req.json(&json!({ + "crate_owner_invitation": { + "crate_id": crate_id, + "accepted": true, }, - })) + }))) } From 99e7ad869bfe7aaf3d130f031a46cdc3f5ee2944 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Fri, 3 Sep 2021 21:56:13 +0200 Subject: [PATCH 16/19] controllers::keyword: Use `json!` macro to simplify JSON serialization code --- src/controllers/keyword.rs | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/src/controllers/keyword.rs b/src/controllers/keyword.rs index 1b0e09cb975..6e2a8d9dc6b 100644 --- a/src/controllers/keyword.rs +++ b/src/controllers/keyword.rs @@ -24,22 +24,15 @@ pub fn index(req: &mut dyn RequestExt) -> EndpointResult { let conn = req.db_read_only()?; let data: Paginated = query.load(&*conn)?; let total = data.total(); - let kws = data.into_iter().map(Keyword::into).collect::>(); - - #[derive(Serialize)] - struct R { - keywords: Vec, - meta: Meta, - } - #[derive(Serialize)] - struct Meta { - total: Option, - } - - Ok(req.json(&R { - keywords: kws, - meta: Meta { total: Some(total) }, - })) + let kws = data + .into_iter() + .map(Keyword::into) + .collect::>(); + + Ok(req.json(&json!({ + "keywords": kws, + "meta": { "total": total }, + }))) } /// Handles the `GET /keywords/:keyword_id` route. @@ -49,9 +42,5 @@ pub fn show(req: &mut dyn RequestExt) -> EndpointResult { let kw = Keyword::find_by_keyword(&conn, name)?; - #[derive(Serialize)] - struct R { - keyword: EncodableKeyword, - } - Ok(req.json(&R { keyword: kw.into() })) + Ok(req.json(&json!({ "keyword": EncodableKeyword::from(kw) }))) } From 8726c2026c59ab6811f99f3373a98e0f9000f6ad Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Fri, 3 Sep 2021 21:56:20 +0200 Subject: [PATCH 17/19] controllers::site_metadata: Use `json!` macro to simplify JSON serialization code --- src/controllers/site_metadata.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/controllers/site_metadata.rs b/src/controllers/site_metadata.rs index 0feca662f5f..c393a27b761 100644 --- a/src/controllers/site_metadata.rs +++ b/src/controllers/site_metadata.rs @@ -8,13 +8,8 @@ pub fn show_deployed_sha(req: &mut dyn RequestExt) -> EndpointResult { let deployed_sha = dotenv::var("HEROKU_SLUG_COMMIT").unwrap_or_else(|_| String::from("unknown")); - #[derive(Serialize)] - struct R<'a> { - deployed_sha: &'a str, - commit: &'a str, - } - Ok(req.json(&R { - deployed_sha: &deployed_sha[..], - commit: &deployed_sha[..], - })) + Ok(req.json(&json!({ + "deployed_sha": &deployed_sha[..], + "commit": &deployed_sha[..], + }))) } From d4501768bd8c29d9b6bb70b10ada9d6dc83e6ae5 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Fri, 3 Sep 2021 21:56:27 +0200 Subject: [PATCH 18/19] controllers::team: Use `json!` macro to simplify JSON serialization code --- src/controllers/team.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/controllers/team.rs b/src/controllers/team.rs index fea5015c1c9..fadea1208ee 100644 --- a/src/controllers/team.rs +++ b/src/controllers/team.rs @@ -12,9 +12,5 @@ pub fn show_team(req: &mut dyn RequestExt) -> EndpointResult { let conn = req.db_read_only()?; let team: Team = teams.filter(login.eq(name)).first(&*conn)?; - #[derive(Serialize)] - struct R { - team: EncodableTeam, - } - Ok(req.json(&R { team: team.into() })) + Ok(req.json(&json!({ "team": EncodableTeam::from(team) }))) } From 2c2536a3fe8174c1cd6fc39bd0fd4ccf3724b3b2 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Fri, 3 Sep 2021 21:56:33 +0200 Subject: [PATCH 19/19] controllers::token: Use `json!` macro to simplify JSON serialization code --- src/controllers/token.rs | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/src/controllers/token.rs b/src/controllers/token.rs index f7734f5c028..b845f633aaa 100644 --- a/src/controllers/token.rs +++ b/src/controllers/token.rs @@ -14,15 +14,12 @@ pub fn list(req: &mut dyn RequestExt) -> EndpointResult { let conn = req.db_conn()?; let user = authenticated_user.user(); - let tokens = ApiToken::belonging_to(&user) + let tokens: Vec = ApiToken::belonging_to(&user) .filter(api_tokens::revoked.eq(false)) .order(api_tokens::created_at.desc()) .load(&*conn)?; - #[derive(Serialize)] - struct R { - api_tokens: Vec, - } - Ok(req.json(&R { api_tokens: tokens })) + + Ok(req.json(&json!({ "api_tokens": tokens }))) } /// Handles the `PUT /me/tokens` route. @@ -82,14 +79,9 @@ pub fn new(req: &mut dyn RequestExt) -> EndpointResult { } let api_token = ApiToken::insert(&*conn, user.id, name)?; + let api_token = EncodableApiTokenWithToken::from(api_token); - #[derive(Serialize)] - struct R { - api_token: EncodableApiTokenWithToken, - } - Ok(req.json(&R { - api_token: api_token.into(), - })) + Ok(req.json(&json!({ "api_token": api_token }))) } /// Handles the `DELETE /me/tokens/:id` route. @@ -105,9 +97,7 @@ pub fn revoke(req: &mut dyn RequestExt) -> EndpointResult { .set(api_tokens::revoked.eq(true)) .execute(&*conn)?; - #[derive(Serialize)] - struct R {} - Ok(req.json(&R {})) + Ok(req.json(&json!({}))) } /// Handles the `DELETE /tokens/current` route.