/
version.rs
194 lines (174 loc) · 5.76 KB
/
version.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use std::collections::HashMap;
use chrono::NaiveDateTime;
use diesel;
use diesel::prelude::*;
use semver;
use serde_json;
use license_exprs;
use util::{human, CargoResult};
use models::{Crate, Dependency};
use schema::*;
use views::{EncodableVersion, EncodableVersionLinks};
// Queryable has a custom implementation below
#[derive(Clone, Identifiable, Associations, Debug, Queryable)]
#[belongs_to(Crate)]
pub struct Version {
pub id: i32,
pub crate_id: i32,
pub num: semver::Version,
pub updated_at: NaiveDateTime,
pub created_at: NaiveDateTime,
pub downloads: i32,
pub features: serde_json::Value,
pub yanked: bool,
pub license: Option<String>,
pub crate_size: Option<i32>,
}
#[derive(Insertable, Debug)]
#[table_name = "versions"]
pub struct NewVersion {
crate_id: i32,
num: String,
features: serde_json::Value,
license: Option<String>,
crate_size: Option<i32>,
}
impl Version {
pub fn encodable(self, crate_name: &str) -> EncodableVersion {
let Version {
id,
num,
updated_at,
created_at,
downloads,
features,
yanked,
license,
crate_size,
..
} = self;
let num = num.to_string();
EncodableVersion {
dl_path: format!("/api/v1/crates/{}/{}/download", crate_name, num),
readme_path: format!("/api/v1/crates/{}/{}/readme", crate_name, num),
num: num.clone(),
id,
krate: crate_name.to_string(),
updated_at,
created_at,
downloads,
features,
yanked,
license,
links: EncodableVersionLinks {
dependencies: format!("/api/v1/crates/{}/{}/dependencies", crate_name, num),
version_downloads: format!("/api/v1/crates/{}/{}/downloads", crate_name, num),
authors: format!("/api/v1/crates/{}/{}/authors", crate_name, num),
},
crate_size,
}
}
/// Returns (dependency, crate dependency name)
pub fn dependencies(&self, conn: &PgConnection) -> QueryResult<Vec<(Dependency, String)>> {
Dependency::belonging_to(self)
.inner_join(crates::table)
.select((dependencies::all_columns, crates::name))
.order((dependencies::optional, crates::name))
.load(conn)
}
pub fn max<T>(versions: T) -> semver::Version
where
T: IntoIterator<Item = semver::Version>,
{
versions
.into_iter()
.max()
.unwrap_or_else(|| semver::Version {
major: 0,
minor: 0,
patch: 0,
pre: vec![],
build: vec![],
})
}
pub fn record_readme_rendering(&self, conn: &PgConnection) -> QueryResult<usize> {
use diesel::dsl::now;
use schema::readme_renderings::dsl::*;
diesel::insert_into(readme_renderings)
.values(version_id.eq(self.id))
.on_conflict(version_id)
.do_update()
.set(rendered_at.eq(now))
.execute(conn)
}
}
impl NewVersion {
pub fn new(
crate_id: i32,
num: &semver::Version,
features: &HashMap<String, Vec<String>>,
license: Option<String>,
license_file: Option<&str>,
crate_size: Option<i32>,
) -> CargoResult<Self> {
let features = serde_json::to_value(features)?;
let mut new_version = NewVersion {
crate_id,
num: num.to_string(),
features,
license,
crate_size,
};
new_version.validate_license(license_file)?;
Ok(new_version)
}
pub fn save(&self, conn: &PgConnection, authors: &[String]) -> CargoResult<Version> {
use diesel::dsl::exists;
use diesel::{insert_into, select};
use schema::version_authors::{name, version_id};
use schema::versions::dsl::*;
conn.transaction(|| {
let already_uploaded = versions
.filter(crate_id.eq(self.crate_id))
.filter(num.eq(&self.num));
if select(exists(already_uploaded)).get_result(conn)? {
return Err(human(&format_args!(
"crate version `{}` is already \
uploaded",
self.num
)));
}
let version = insert_into(versions)
.values(self)
.get_result::<Version>(conn)?;
let new_authors = authors
.iter()
.map(|s| (version_id.eq(version.id), name.eq(s)))
.collect::<Vec<_>>();
insert_into(version_authors::table)
.values(&new_authors)
.execute(conn)?;
Ok(version)
})
}
fn validate_license(&mut self, license_file: Option<&str>) -> CargoResult<()> {
if let Some(ref license) = self.license {
for part in license.split('/') {
license_exprs::validate_license_expr(part).map_err(|e| {
human(&format_args!(
"{}; see http://opensource.org/licenses \
for options, and http://spdx.org/licenses/ \
for their identifiers",
e
))
})?;
}
} else if license_file.is_some() {
// If no license is given, but a license file is given, flag this
// crate as having a nonstandard license. Note that we don't
// actually do anything else with license_file currently.
self.license = Some(String::from("non-standard"));
}
Ok(())
}
}