Skip to content

Commit d1f21e6

Browse files
authored
Merge pull request #7199 from Turbo87/licenses
Extract `licenses` module
2 parents e5c2f17 + 167ca30 commit d1f21e6

File tree

4 files changed

+37
-32
lines changed

4 files changed

+37
-32
lines changed

src/controllers/krate/publish.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ use crate::models::{
1717
VersionAction,
1818
};
1919

20+
use crate::licenses::parse_license_expr;
2021
use crate::middleware::log_request::RequestLogExt;
2122
use crate::models::token::EndpointScope;
22-
use crate::models::version::validate_license_expr;
2323
use crate::rate_limiter::LimitedAction;
2424
use crate::schema::*;
2525
use crate::sql::canon_crate_name;
@@ -34,6 +34,10 @@ const MISSING_RIGHTS_ERROR_MESSAGE: &str = "this crate exists but you don't seem
3434
to accept an invitation to be an owner before \
3535
publishing.";
3636

37+
const LICENSE_ERROR: &str = "unknown or invalid license expression; \
38+
see http://opensource.org/licenses for options, \
39+
and http://spdx.org/licenses/ for their identifiers";
40+
3741
/// Handles the `PUT /crates/new` route.
3842
/// Used by `cargo publish` to publish a new crate or to publish a new version of an
3943
/// existing crate.
@@ -138,7 +142,7 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
138142
}
139143

140144
if let Some(ref license) = license {
141-
validate_license_expr(license)?;
145+
parse_license_expr(license).map_err(|_| cargo_err(LICENSE_ERROR))?;
142146
} else if license_file.is_some() {
143147
// If no license is given, but a license file is given, flag this
144148
// crate as having a nonstandard license. Note that we don't

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ pub mod worker;
5757

5858
pub mod auth;
5959
pub mod controllers;
60+
mod licenses;
6061
pub mod models;
6162
mod router;
6263
pub mod sentry;

src/licenses.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use spdx::{Expression, ParseError};
2+
3+
const PARSE_MODE: spdx::ParseMode = spdx::ParseMode {
4+
allow_lower_case_operators: false,
5+
allow_slash_as_or_operator: true,
6+
allow_imprecise_license_names: false,
7+
allow_postfix_plus_on_gpl: true,
8+
};
9+
10+
pub fn parse_license_expr(s: &str) -> Result<Expression, ParseError> {
11+
Expression::parse_mode(s, PARSE_MODE)
12+
}
13+
14+
#[cfg(test)]
15+
mod tests {
16+
use super::parse_license_expr;
17+
18+
#[test]
19+
fn licenses() {
20+
assert_ok!(parse_license_expr("MIT"));
21+
assert_ok!(parse_license_expr("MIT OR Apache-2.0"));
22+
assert_ok!(parse_license_expr("MIT/Apache-2.0"));
23+
assert_ok!(parse_license_expr("MIT AND Apache-2.0"));
24+
assert_ok!(parse_license_expr("MIT OR (Apache-2.0 AND MIT)"));
25+
assert_ok!(parse_license_expr("GPL-3.0+"));
26+
27+
assert_err!(parse_license_expr("apache 2.0"));
28+
}
29+
}

src/models/version.rs

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -192,21 +192,6 @@ impl NewVersion {
192192
}
193193
}
194194

195-
pub fn validate_license_expr(s: &str) -> AppResult<()> {
196-
pub const PARSE_MODE: spdx::ParseMode = spdx::ParseMode {
197-
allow_lower_case_operators: false,
198-
allow_slash_as_or_operator: true,
199-
allow_imprecise_license_names: false,
200-
allow_postfix_plus_on_gpl: true,
201-
};
202-
203-
spdx::Expression::parse_mode(s, PARSE_MODE).map_err(|_| {
204-
cargo_err("unknown or invalid license expression; see http://opensource.org/licenses for options, and http://spdx.org/licenses/ for their identifiers")
205-
})?;
206-
207-
Ok(())
208-
}
209-
210195
fn strip_build_metadata(version: &str) -> &str {
211196
version
212197
.split_once('+')
@@ -216,7 +201,7 @@ fn strip_build_metadata(version: &str) -> &str {
216201

217202
#[cfg(test)]
218203
mod tests {
219-
use super::{validate_license_expr, TopVersions};
204+
use super::TopVersions;
220205
use chrono::NaiveDateTime;
221206

222207
#[track_caller]
@@ -286,18 +271,4 @@ mod tests {
286271
}
287272
);
288273
}
289-
290-
#[test]
291-
fn licenses() {
292-
assert_ok!(validate_license_expr("MIT"));
293-
assert_ok!(validate_license_expr("MIT OR Apache-2.0"));
294-
assert_ok!(validate_license_expr("MIT/Apache-2.0"));
295-
assert_ok!(validate_license_expr("MIT AND Apache-2.0"));
296-
assert_ok!(validate_license_expr("MIT OR (Apache-2.0 AND MIT)"));
297-
assert_ok!(validate_license_expr("GPL-3.0+"));
298-
299-
let error = assert_err!(validate_license_expr("apache 2.0"));
300-
let error = format!("{error}");
301-
assert!(error.starts_with("unknown or invalid license expression; see http"));
302-
}
303274
}

0 commit comments

Comments
 (0)