pub fn validate_spdx_license(license: &str) -> Result<(), ExtensionError>Expand description
Validates that a license string is a recognized SPDX identifier, or an SPDX license expression built from them.
Accepted:
- any identifier in
COMMON_SPDX_LICENSES, spelled with its canonical case. SPDX (Annex D.2) says identifiers should be matched case-insensitively, but also that the canonical case matters, since it is used in the license’s URL; this validator requires it, and names the canonical spelling when only the case differs (mit→MIT); - a user-defined
LicenseRef-<idstring>reference, which SPDX allows in any expression; - either of those followed by
WITH <exception>, e.g.Apache-2.0 WITH LLVM-exception, where the exception is one ofSPDX_LICENSE_EXCEPTIONS(the whole SPDX exception registry) or a user-definedAdditionRef-<idstring>(SPDX 3.0). As in the SPDX grammar,WITHbinds tightest and applies to a single license, not to a parenthesised expression; - a compound expression joining those with
AND/ORand parentheses, e.g.MIT OR Apache-2.0— the Rust ecosystem’s default and what several published community extensions declare. Parentheses may nest up to 64 deep.
Not accepted: lowercase operators, and identifiers outside the shortlist — the error for those says they may still be valid rather than claiming they do not exist.
§Errors
Returns ExtensionError if the license is empty, is not a well-formed
expression, nests parentheses more than 64 deep, or names a license or
exception identifier not in the recognized lists.
§Example
use quack_rs::validate::validate_spdx_license;
assert!(validate_spdx_license("MIT").is_ok());
assert!(validate_spdx_license("Apache-2.0").is_ok());
assert!(validate_spdx_license("BSD-3-Clause").is_ok());
assert!(validate_spdx_license("MIT OR Apache-2.0").is_ok());
assert!(validate_spdx_license("Apache-2.0 WITH LLVM-exception").is_ok());
assert!(validate_spdx_license("FAKE-LICENSE").is_err());
assert!(validate_spdx_license("MIT OR").is_err());
assert!(validate_spdx_license("").is_err());