Skip to content

Commit 7ce3dfe

Browse files
committed
models/version: Move license code to dedicated module
1 parent 482ed93 commit 7ce3dfe

File tree

4 files changed

+37
-31
lines changed

4 files changed

+37
-31
lines changed

src/controllers/krate/publish.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ use crate::models::{
1717
VersionAction,
1818
};
1919

20+
use crate::licenses::validate_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;

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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use crate::util::errors::{cargo_err, AppResult};
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 validate_license_expr(s: &str) -> AppResult<()> {
11+
spdx::Expression::parse_mode(s, PARSE_MODE).map_err(|_| {
12+
cargo_err("unknown or invalid license expression; see http://opensource.org/licenses for options, and http://spdx.org/licenses/ for their identifiers")
13+
})?;
14+
15+
Ok(())
16+
}
17+
18+
#[cfg(test)]
19+
mod tests {
20+
use super::validate_license_expr;
21+
22+
#[test]
23+
fn licenses() {
24+
assert_ok!(validate_license_expr("MIT"));
25+
assert_ok!(validate_license_expr("MIT OR Apache-2.0"));
26+
assert_ok!(validate_license_expr("MIT/Apache-2.0"));
27+
assert_ok!(validate_license_expr("MIT AND Apache-2.0"));
28+
assert_ok!(validate_license_expr("MIT OR (Apache-2.0 AND MIT)"));
29+
assert_ok!(validate_license_expr("GPL-3.0+"));
30+
31+
let error = assert_err!(validate_license_expr("apache 2.0")).to_string();
32+
assert!(error.starts_with("unknown or invalid license expression; see http"));
33+
}
34+
}

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)