Skip to content

Commit 167ca30

Browse files
committed
licenses: Simplify validate_license_expr() API
The main purpose of this wrapper fn is to apply our custom `ParseMode`. The conversion to an `AppError` should be done in the route handler code though.
1 parent 7ce3dfe commit 167ca30

File tree

2 files changed

+17
-18
lines changed

2 files changed

+17
-18
lines changed

src/controllers/krate/publish.rs

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

20-
use crate::licenses::validate_license_expr;
20+
use crate::licenses::parse_license_expr;
2121
use crate::middleware::log_request::RequestLogExt;
2222
use crate::models::token::EndpointScope;
2323
use crate::rate_limiter::LimitedAction;
@@ -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/licenses.rs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::util::errors::{cargo_err, AppResult};
1+
use spdx::{Expression, ParseError};
22

33
const PARSE_MODE: spdx::ParseMode = spdx::ParseMode {
44
allow_lower_case_operators: false,
@@ -7,28 +7,23 @@ const PARSE_MODE: spdx::ParseMode = spdx::ParseMode {
77
allow_postfix_plus_on_gpl: true,
88
};
99

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(())
10+
pub fn parse_license_expr(s: &str) -> Result<Expression, ParseError> {
11+
Expression::parse_mode(s, PARSE_MODE)
1612
}
1713

1814
#[cfg(test)]
1915
mod tests {
20-
use super::validate_license_expr;
16+
use super::parse_license_expr;
2117

2218
#[test]
2319
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+"));
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+"));
3026

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"));
27+
assert_err!(parse_license_expr("apache 2.0"));
3328
}
3429
}

0 commit comments

Comments
 (0)