Expression
An SPDX license expression:
one license, or several combined with AND and OR.
The syntax is MIT, GPL-2.0-or-later, Apache-2.0 WITH LLVM-exception, MIT OR Apache-2.0
and (MIT OR Apache-2.0) AND BSD-3-Clause, with WITH binding tighter than AND, and AND
tighter than OR. Operators are upper case, and the legacy MIT/Apache-2.0 of older Cargo
manifests is read as OR. A trailing + on an identifier is kept (GPL-2.0+). Identifiers
are only checked for their shape: use unknown_ids to find the ones this
crate does not list. Display writes the expression back, with parentheses only where they
change the meaning.
Import
Section titled “Import”Cargo feature license (enabled by default). To compile only this module:
or in Cargo.toml:
Definition
Section titled “Definition”Examples
Section titled “Examples”Methods
Section titled “Methods”Parses an SPDX license expression.
Parameters
| Parameter | Type | Description |
|---|---|---|
text | &str | The expression, such as "MIT OR Apache-2.0". |
Returns
Result<Self, ParseExpressionError>
Errors
A ParseExpressionError for an empty expression, one that ends too early, a token in
the wrong place, a malformed identifier, or an unclosed parenthesis.
licenses
Section titled “licenses”The license identifiers used, in order of first appearance, without repeats.
Returns
Vec<&str> — The identifiers as written, without the exceptions after WITH.
unknown_ids
Section titled “unknown_ids”The identifiers that are neither in this crate’s list of licenses (see
lookup) nor a LicenseRef- / DocumentRef- reference.
Returns
Vec<&str> — The identifiers to double-check: a typo, or a license this crate does not list.
is_satisfied_by
Section titled “is_satisfied_by”Whether a policy that allows exactly the licenses in allowed accepts this expression:
OR needs one side, AND needs both, and a WITH exception never makes a license less
acceptable than the license alone.
Identifiers are compared ignoring case and exactly otherwise (GPL-2.0+ is not
GPL-2.0-or-later; call normalize first if that matters).
Parameters
| Parameter | Type | Description |
|---|---|---|
allowed | &[&str] | The identifiers the policy accepts. |
Returns
bool — true when the expression can be satisfied with those licenses.
Error type: ParseExpressionError
Section titled “Error type: ParseExpressionError”Why a string is not a valid SPDX license expression.
