Skip to content

Commit 822583d

Browse files
committed
views: Implement From<Category> for EncodableCategory
1 parent ec859bb commit 822583d

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

src/controllers/category.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub fn index(req: &mut dyn RequestExt) -> EndpointResult {
1818
let conn = req.db_conn()?;
1919
let categories =
2020
Category::toplevel(&conn, sort, i64::from(options.per_page), i64::from(offset))?;
21-
let categories = categories.into_iter().map(Category::encodable).collect();
21+
let categories = categories.into_iter().map(Category::into).collect();
2222

2323
// Query for the total count of categories
2424
let total = Category::count_toplevel(&conn)?;
@@ -47,15 +47,15 @@ pub fn show(req: &mut dyn RequestExt) -> EndpointResult {
4747
let subcats = cat
4848
.subcategories(&conn)?
4949
.into_iter()
50-
.map(Category::encodable)
50+
.map(Category::into)
5151
.collect();
5252
let parents = cat
5353
.parent_categories(&conn)?
5454
.into_iter()
55-
.map(Category::encodable)
55+
.map(Category::into)
5656
.collect();
5757

58-
let cat = cat.encodable();
58+
let cat = EncodableCategory::from(cat);
5959
let cat_with_subcats = EncodableCategoryWithSubcategories {
6060
id: cat.id,
6161
category: cat.category,

src/controllers/krate/metadata.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub fn summary(req: &mut dyn RequestExt) -> EndpointResult {
8585

8686
let popular_categories = Category::toplevel(&conn, "crates", 10, 0)?
8787
.into_iter()
88-
.map(Category::encodable)
88+
.map(Category::into)
8989
.collect();
9090

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

src/views.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use chrono::NaiveDateTime;
22
use std::collections::HashMap;
33

4-
use crate::models::{Badge, DependencyKind};
4+
use crate::models::{Badge, Category, DependencyKind};
55
use crate::util::rfc3339;
66

77
#[derive(PartialEq, Debug, Serialize, Deserialize)]
@@ -28,6 +28,12 @@ pub struct EncodableCategory {
2828
pub crates_cnt: i32,
2929
}
3030

31+
impl From<Category> for EncodableCategory {
32+
fn from(category: Category) -> Self {
33+
category.encodable()
34+
}
35+
}
36+
3137
#[derive(Serialize, Deserialize, Debug)]
3238
pub struct EncodableCategoryWithSubcategories {
3339
pub id: String,

0 commit comments

Comments
 (0)