Skip to content

Commit a1911e7

Browse files
committed
Added back the functionality for the Crate's holding their licensing information.
1 parent 1961032 commit a1911e7

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/krate.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ pub struct EncodableCrate {
103103
pub description: Option<String>,
104104
pub homepage: Option<String>,
105105
pub documentation: Option<String>,
106+
pub license: Option<String>,
106107
pub repository: Option<String>,
107108
pub links: CrateLinks,
108109
pub exact_match: bool,
@@ -132,13 +133,14 @@ pub struct NewCrate<'a> {
132133

133134
impl<'a> NewCrate<'a> {
134135
pub fn create_or_update(
135-
self,
136+
mut self,
136137
conn: &PgConnection,
138+
license_file: Option<&'a str>,
137139
uploader: i32,
138140
) -> CargoResult<Crate> {
139141
use diesel::update;
140142

141-
self.validate()?;
143+
self.validate(license_file)?;
142144
self.ensure_name_not_reserved(conn)?;
143145

144146
conn.transaction(|| {
@@ -159,7 +161,7 @@ impl<'a> NewCrate<'a> {
159161
})
160162
}
161163

162-
fn validate(&self) -> CargoResult<()> {
164+
fn validate(&mut self, license_file: Option<&'a str>) -> CargoResult<()> {
163165
fn validate_url(url: Option<&str>, field: &str) -> CargoResult<()> {
164166
let url = match url {
165167
Some(s) => s,
@@ -193,6 +195,24 @@ impl<'a> NewCrate<'a> {
193195
validate_url(self.homepage, "homepage")?;
194196
validate_url(self.documentation, "documentation")?;
195197
validate_url(self.repository, "repository")?;
198+
self.validate_license(license_file)?;
199+
Ok(())
200+
}
201+
202+
fn validate_license(&mut self, license_file: Option<&str>) -> CargoResult<()> {
203+
if let Some(ref license) = self.license {
204+
for part in license.split('/') {
205+
license_exprs::validate_license_expr(part)
206+
.map_err(|e| human(&format_args!("{}; see http://opensource.org/licenses \
207+
for options, and http://spdx.org/licenses/ \
208+
for their identifiers", e)))?;
209+
}
210+
} else if license_file.is_some() {
211+
// If no license is given, but a license file is given, flag this
212+
// crate as having a nonstandard license. Note that we don't
213+
// actually do anything else with license_file currently.
214+
self.license = Some("non-standard");
215+
}
196216
Ok(())
197217
}
198218

@@ -480,6 +500,7 @@ impl Crate {
480500
homepage,
481501
documentation,
482502
repository,
503+
license,
483504
..
484505
} = self;
485506
let versions_link = match versions {
@@ -505,6 +526,7 @@ impl Crate {
505526
exact_match: exact_match,
506527
description: description,
507528
repository: repository,
529+
license: license,
508530
links: CrateLinks {
509531
version_downloads: format!("/api/v1/crates/{}/downloads", name),
510532
versions: versions_link,
@@ -1059,7 +1081,7 @@ pub fn new(req: &mut Request) -> CargoResult<Response> {
10591081
};
10601082

10611083
let license_file = new_crate.license_file.as_ref().map(|s| &**s);
1062-
let krate = persist.create_or_update(&conn, user.id)?;
1084+
let krate = persist.create_or_update(&conn, license_file, user.id)?;
10631085

10641086
let owners = krate.owners(&conn)?;
10651087
if rights(req.app(), &owners, &user)? < Rights::Publish {

src/tests/all.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,7 @@ impl<'a> CrateBuilder<'a> {
325325

326326
let mut krate = self.krate.create_or_update(
327327
connection,
328+
None,
328329
self.owner_id,
329330
)?;
330331

0 commit comments

Comments
 (0)