Skip to content

Convert encodable() methods to From implementations #3124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/controllers/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub fn index(req: &mut dyn RequestExt) -> EndpointResult {
let conn = req.db_conn()?;
let categories =
Category::toplevel(&conn, sort, i64::from(options.per_page), i64::from(offset))?;
let categories = categories.into_iter().map(Category::encodable).collect();
let categories = categories.into_iter().map(Category::into).collect();

// Query for the total count of categories
let total = Category::count_toplevel(&conn)?;
Expand Down Expand Up @@ -47,15 +47,15 @@ pub fn show(req: &mut dyn RequestExt) -> EndpointResult {
let subcats = cat
.subcategories(&conn)?
.into_iter()
.map(Category::encodable)
.map(Category::into)
.collect();
let parents = cat
.parent_categories(&conn)?
.into_iter()
.map(Category::encodable)
.map(Category::into)
.collect();

let cat = cat.encodable();
let cat = EncodableCategory::from(cat);
let cat_with_subcats = EncodableCategoryWithSubcategories {
id: cat.id,
category: cat.category,
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/krate/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ pub fn summary(req: &mut dyn RequestExt) -> EndpointResult {

let popular_categories = Category::toplevel(&conn, "crates", 10, 0)?
.into_iter()
.map(Category::encodable)
.map(Category::into)
.collect();

#[derive(Serialize)]
Expand Down Expand Up @@ -179,7 +179,7 @@ pub fn show(req: &mut dyn RequestExt) -> EndpointResult {
.map(|(v, pb, aas)| v.encodable(&krate.name, pb, aas))
.collect(),
keywords: kws.into_iter().map(Keyword::encodable).collect(),
categories: cats.into_iter().map(Category::encodable).collect(),
categories: cats.into_iter().map(Category::into).collect(),
}))
}

Expand Down
6 changes: 0 additions & 6 deletions src/models/badge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use std::collections::HashMap;

use crate::models::Crate;
use crate::schema::badges;
use crate::views::EncodableBadge;

/// A combination of a `Badge` and a crate ID.
///
Expand Down Expand Up @@ -108,11 +107,6 @@ impl Queryable<badges::SqlType, Pg> for Badge {
}

impl Badge {
pub fn encodable(self) -> EncodableBadge {
// The serde attributes on Badge ensure it can be deserialized to EncodableBadge
serde_json::from_value(serde_json::to_value(self).unwrap()).unwrap()
}

pub fn update_crate(
conn: &PgConnection,
krate: &Crate,
Expand Down
20 changes: 0 additions & 20 deletions src/models/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use diesel::{self, *};

use crate::models::Crate;
use crate::schema::*;
use crate::views::EncodableCategory;

#[derive(Clone, Identifiable, Queryable, QueryableByName, Debug)]
#[table_name = "categories"]
Expand Down Expand Up @@ -57,25 +56,6 @@ impl Category {
categories::table.filter(Self::with_slugs_case_sensitive(slugs))
}

pub fn encodable(self) -> EncodableCategory {
let Category {
crates_cnt,
category,
slug,
description,
created_at,
..
} = self;
EncodableCategory {
id: slug.clone(),
slug,
description,
created_at,
crates_cnt,
category: category.rsplit("::").collect::<Vec<_>>()[0].to_string(),
}
}

pub fn update_crate(
conn: &PgConnection,
krate: &Crate,
Expand Down
2 changes: 1 addition & 1 deletion src/models/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ impl Crate {
};
let keyword_ids = keywords.map(|kws| kws.iter().map(|kw| kw.keyword.clone()).collect());
let category_ids = categories.map(|cats| cats.iter().map(|cat| cat.slug.clone()).collect());
let badges = badges.map(|bs| bs.into_iter().map(Badge::encodable).collect());
let badges = badges.map(|bs| bs.into_iter().map(Badge::into).collect());
let documentation = Crate::remove_blocked_documentation_urls(documentation);

EncodableCrate {
Expand Down
30 changes: 29 additions & 1 deletion src/views.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use chrono::NaiveDateTime;
use std::collections::HashMap;

use crate::models::DependencyKind;
use crate::models::{Badge, Category, DependencyKind};
use crate::util::rfc3339;

#[derive(PartialEq, Debug, Serialize, Deserialize)]
Expand All @@ -10,6 +10,13 @@ pub struct EncodableBadge {
pub attributes: HashMap<String, Option<String>>,
}

impl From<Badge> for EncodableBadge {
fn from(badge: Badge) -> Self {
// The serde attributes on Badge ensure it can be deserialized to EncodableBadge
serde_json::from_value(serde_json::to_value(badge).unwrap()).unwrap()
}
}

#[derive(Serialize, Deserialize, Debug)]
pub struct EncodableCategory {
pub id: String,
Expand All @@ -21,6 +28,27 @@ pub struct EncodableCategory {
pub crates_cnt: i32,
}

impl From<Category> for EncodableCategory {
fn from(category: Category) -> Self {
let Category {
crates_cnt,
category,
slug,
description,
created_at,
..
} = category;
Self {
id: slug.clone(),
slug,
description,
created_at,
crates_cnt,
category: category.rsplit("::").collect::<Vec<_>>()[0].to_string(),
}
}
}

#[derive(Serialize, Deserialize, Debug)]
pub struct EncodableCategoryWithSubcategories {
pub id: String,
Expand Down