Skip to content

Commit 0aeca62

Browse files
committed
Move Encodable* structs for Category and Keyword to the views module
1 parent e9f6010 commit 0aeca62

File tree

3 files changed

+37
-31
lines changed

3 files changed

+37
-31
lines changed

src/category.rs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use diesel::*;
33

44
use models::Crate;
55
use schema::*;
6+
use views::EncodableCategory;
67

78
#[derive(Clone, Identifiable, Queryable, QueryableByName, Debug)]
89
#[table_name = "categories"]
@@ -25,27 +26,6 @@ pub struct CrateCategory {
2526
category_id: i32,
2627
}
2728

28-
#[derive(Serialize, Deserialize, Debug)]
29-
pub struct EncodableCategory {
30-
pub id: String,
31-
pub category: String,
32-
pub slug: String,
33-
pub description: String,
34-
#[serde(with = "::util::rfc3339")] pub created_at: NaiveDateTime,
35-
pub crates_cnt: i32,
36-
}
37-
38-
#[derive(Serialize, Deserialize, Debug)]
39-
pub struct EncodableCategoryWithSubcategories {
40-
pub id: String,
41-
pub category: String,
42-
pub slug: String,
43-
pub description: String,
44-
#[serde(with = "::util::rfc3339")] pub created_at: NaiveDateTime,
45-
pub crates_cnt: i32,
46-
pub subcategories: Vec<EncodableCategory>,
47-
}
48-
4929
impl Category {
5030
pub fn encodable(self) -> EncodableCategory {
5131
let Category {
@@ -184,6 +164,8 @@ mod tests {
184164
use serde_json;
185165
use std::env;
186166

167+
use views::EncodableCategoryWithSubcategories;
168+
187169
fn pg_connection() -> PgConnection {
188170
let _ = dotenv();
189171
let database_url =

src/keyword.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use diesel;
44

55
use models::Crate;
66
use schema::*;
7+
use views::EncodableKeyword;
78

89
#[derive(Clone, Identifiable, Queryable, Debug)]
910
pub struct Keyword {
@@ -23,14 +24,6 @@ pub struct CrateKeyword {
2324
keyword_id: i32,
2425
}
2526

26-
#[derive(Serialize, Deserialize, Debug)]
27-
pub struct EncodableKeyword {
28-
pub id: String,
29-
pub keyword: String,
30-
#[serde(with = "::util::rfc3339")] pub created_at: NaiveDateTime,
31-
pub crates_cnt: i32,
32-
}
33-
3427
impl Keyword {
3528
pub fn find_by_keyword(conn: &PgConnection, name: &str) -> QueryResult<Keyword> {
3629
keywords::table

src/views/mod.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,43 @@
11
// TODO: Move all encodable types here
22
// For now, just reexport
33

4+
use chrono::NaiveDateTime;
5+
46
pub use badge::EncodableBadge;
5-
pub use category::{EncodableCategory, EncodableCategoryWithSubcategories};
7+
8+
#[derive(Serialize, Deserialize, Debug)]
9+
pub struct EncodableCategory {
10+
pub id: String,
11+
pub category: String,
12+
pub slug: String,
13+
pub description: String,
14+
#[serde(with = "::util::rfc3339")] pub created_at: NaiveDateTime,
15+
pub crates_cnt: i32,
16+
}
17+
18+
#[derive(Serialize, Deserialize, Debug)]
19+
pub struct EncodableCategoryWithSubcategories {
20+
pub id: String,
21+
pub category: String,
22+
pub slug: String,
23+
pub description: String,
24+
#[serde(with = "::util::rfc3339")] pub created_at: NaiveDateTime,
25+
pub crates_cnt: i32,
26+
pub subcategories: Vec<EncodableCategory>,
27+
}
28+
629
pub use crate_owner_invitation::{EncodableCrateOwnerInvitation, InvitationResponse};
730
pub use dependency::EncodableDependency;
831
pub use download::EncodableVersionDownload;
9-
pub use keyword::EncodableKeyword;
32+
33+
#[derive(Serialize, Deserialize, Debug)]
34+
pub struct EncodableKeyword {
35+
pub id: String,
36+
pub keyword: String,
37+
#[serde(with = "::util::rfc3339")] pub created_at: NaiveDateTime,
38+
pub crates_cnt: i32,
39+
}
40+
1041
pub use krate::EncodableCrate;
1142
pub use owner::{EncodableOwner, EncodableTeam};
1243
pub use token::EncodableApiTokenWithToken;

0 commit comments

Comments
 (0)