Skip to content

Commit ecd775b

Browse files
committed
Auto merge of #3134 - Turbo87:encodable-dep, r=pietroalbini
models::Dependency: Replace `encodable()` method This PR continues #3124 by implementing `from_dep()` and `from_reverse_dep()` methods for the `EncodableDependency` struct and removing the `encodable()` methods on `Dependency` and `ReverseDependency`. We can't use the `From` trait in these cases because the `crate_name` has to be explicitly provided. What we could do instead is implement `From<(Dependency, String)>`, but that doesn't feel right to me. r? `@pietroalbini`
2 parents 13fdc94 + f79a463 commit ecd775b

File tree

4 files changed

+32
-29
lines changed

4 files changed

+32
-29
lines changed

src/controllers/krate/metadata.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ pub fn reverse_dependencies(req: &mut dyn RequestExt) -> EndpointResult {
247247
let (rev_deps, total) = krate.reverse_dependencies(&*conn, pagination_options)?;
248248
let rev_deps: Vec<_> = rev_deps
249249
.into_iter()
250-
.map(|dep| dep.encodable(&krate.name))
250+
.map(|dep| EncodableDependency::from_reverse_dep(dep, &krate.name))
251251
.collect();
252252

253253
let version_ids: Vec<i32> = rev_deps.iter().map(|dep| dep.version_id).collect();

src/controllers/version/metadata.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub fn dependencies(req: &mut dyn RequestExt) -> EndpointResult {
2626
let deps = version.dependencies(&conn)?;
2727
let deps = deps
2828
.into_iter()
29-
.map(|(dep, crate_name)| dep.encodable(&crate_name, None))
29+
.map(|(dep, crate_name)| EncodableDependency::from_dep(dep, &crate_name))
3030
.collect();
3131

3232
#[derive(Serialize)]

src/models/dependency.rs

+1-26
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::util::errors::{cargo_err, AppResult};
55

66
use crate::models::{Crate, Version};
77
use crate::schema::*;
8-
use crate::views::{EncodableCrateDependency, EncodableDependency};
8+
use crate::views::EncodableCrateDependency;
99

1010
pub const WILDCARD_ERROR_MESSAGE: &str = "wildcard (`*`) dependency constraints are not allowed \
1111
on crates.io. See https://doc.rust-lang.org/cargo/faq.html#can-\
@@ -49,31 +49,6 @@ pub enum DependencyKind {
4949
// if you add a kind here, be sure to update `from_row` below.
5050
}
5151

52-
impl Dependency {
53-
// `downloads` need only be specified when generating a reverse dependency
54-
pub fn encodable(self, crate_name: &str, downloads: Option<i32>) -> EncodableDependency {
55-
EncodableDependency {
56-
id: self.id,
57-
version_id: self.version_id,
58-
crate_id: crate_name.into(),
59-
req: self.req,
60-
optional: self.optional,
61-
default_features: self.default_features,
62-
features: self.features,
63-
target: self.target,
64-
kind: self.kind,
65-
downloads: downloads.unwrap_or(0),
66-
}
67-
}
68-
}
69-
70-
impl ReverseDependency {
71-
pub fn encodable(self, crate_name: &str) -> EncodableDependency {
72-
self.dependency
73-
.encodable(crate_name, Some(self.crate_downloads))
74-
}
75-
}
76-
7752
pub fn add_dependencies(
7853
conn: &PgConnection,
7954
deps: &[EncodableCrateDependency],

src/views.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use std::collections::HashMap;
33

44
use crate::github;
55
use crate::models::{
6-
Badge, Category, CreatedApiToken, DependencyKind, Keyword, Owner, Team, User, VersionDownload,
6+
Badge, Category, CreatedApiToken, Dependency, DependencyKind, Keyword, Owner,
7+
ReverseDependency, Team, User, VersionDownload,
78
};
89
use crate::util::rfc3339;
910

@@ -95,6 +96,33 @@ pub struct EncodableDependency {
9596
pub downloads: i32,
9697
}
9798

99+
impl EncodableDependency {
100+
pub fn from_dep(dependency: Dependency, crate_name: &str) -> Self {
101+
Self::encode(dependency, crate_name, None)
102+
}
103+
104+
pub fn from_reverse_dep(rev_dep: ReverseDependency, crate_name: &str) -> Self {
105+
let dependency = rev_dep.dependency;
106+
Self::encode(dependency, crate_name, Some(rev_dep.crate_downloads))
107+
}
108+
109+
// `downloads` need only be specified when generating a reverse dependency
110+
fn encode(dependency: Dependency, crate_name: &str, downloads: Option<i32>) -> Self {
111+
Self {
112+
id: dependency.id,
113+
version_id: dependency.version_id,
114+
crate_id: crate_name.into(),
115+
req: dependency.req,
116+
optional: dependency.optional,
117+
default_features: dependency.default_features,
118+
features: dependency.features,
119+
target: dependency.target,
120+
kind: dependency.kind,
121+
downloads: downloads.unwrap_or(0),
122+
}
123+
}
124+
}
125+
98126
#[derive(Serialize, Deserialize, Debug)]
99127
pub struct EncodableVersionDownload {
100128
pub version: i32,

0 commit comments

Comments
 (0)