Skip to content

models::Dependency: Replace encodable() method #3134

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 2 commits into from
Jan 7, 2021
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
2 changes: 1 addition & 1 deletion src/controllers/krate/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ pub fn reverse_dependencies(req: &mut dyn RequestExt) -> EndpointResult {
let (rev_deps, total) = krate.reverse_dependencies(&*conn, pagination_options)?;
let rev_deps: Vec<_> = rev_deps
.into_iter()
.map(|dep| dep.encodable(&krate.name))
.map(|dep| EncodableDependency::from_reverse_dep(dep, &krate.name))
.collect();

let version_ids: Vec<i32> = rev_deps.iter().map(|dep| dep.version_id).collect();
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/version/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn dependencies(req: &mut dyn RequestExt) -> EndpointResult {
let deps = version.dependencies(&conn)?;
let deps = deps
.into_iter()
.map(|(dep, crate_name)| dep.encodable(&crate_name, None))
.map(|(dep, crate_name)| EncodableDependency::from_dep(dep, &crate_name))
.collect();

#[derive(Serialize)]
Expand Down
27 changes: 1 addition & 26 deletions src/models/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::util::errors::{cargo_err, AppResult};

use crate::models::{Crate, Version};
use crate::schema::*;
use crate::views::{EncodableCrateDependency, EncodableDependency};
use crate::views::EncodableCrateDependency;

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

impl Dependency {
// `downloads` need only be specified when generating a reverse dependency
pub fn encodable(self, crate_name: &str, downloads: Option<i32>) -> EncodableDependency {
EncodableDependency {
id: self.id,
version_id: self.version_id,
crate_id: crate_name.into(),
req: self.req,
optional: self.optional,
default_features: self.default_features,
features: self.features,
target: self.target,
kind: self.kind,
downloads: downloads.unwrap_or(0),
}
}
}

impl ReverseDependency {
pub fn encodable(self, crate_name: &str) -> EncodableDependency {
self.dependency
.encodable(crate_name, Some(self.crate_downloads))
}
}

pub fn add_dependencies(
conn: &PgConnection,
deps: &[EncodableCrateDependency],
Expand Down
30 changes: 29 additions & 1 deletion src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::collections::HashMap;

use crate::github;
use crate::models::{
Badge, Category, CreatedApiToken, DependencyKind, Keyword, Owner, Team, User, VersionDownload,
Badge, Category, CreatedApiToken, Dependency, DependencyKind, Keyword, Owner,
ReverseDependency, Team, User, VersionDownload,
};
use crate::util::rfc3339;

Expand Down Expand Up @@ -95,6 +96,33 @@ pub struct EncodableDependency {
pub downloads: i32,
}

impl EncodableDependency {
pub fn from_dep(dependency: Dependency, crate_name: &str) -> Self {
Self::encode(dependency, crate_name, None)
}

pub fn from_reverse_dep(rev_dep: ReverseDependency, crate_name: &str) -> Self {
let dependency = rev_dep.dependency;
Self::encode(dependency, crate_name, Some(rev_dep.crate_downloads))
}

// `downloads` need only be specified when generating a reverse dependency
fn encode(dependency: Dependency, crate_name: &str, downloads: Option<i32>) -> Self {
Self {
id: dependency.id,
version_id: dependency.version_id,
crate_id: crate_name.into(),
req: dependency.req,
optional: dependency.optional,
default_features: dependency.default_features,
features: dependency.features,
target: dependency.target,
kind: dependency.kind,
downloads: downloads.unwrap_or(0),
}
}
}

#[derive(Serialize, Deserialize, Debug)]
pub struct EncodableVersionDownload {
pub version: i32,
Expand Down