pub struct SpdxExpression { /* private fields */ }
Expand description

Main struct for SPDX License Expressions.

Implementations

Parse Self from a string. The input expression needs to be a syntactically valid SPDX expression, NONE or NOASSERTION. The parser accepts license identifiers that are not valid SPDX.

Examples
let expression = SpdxExpression::parse("MIT")?;

License expressions need to be syntactically valid, but they can include license identifiers not on the SPDX license list or not specified with LicenseRef.

let expression = SpdxExpression::parse("MIT OR InvalidLicenseId")?;
Errors

Returns SpdxExpressionError if the license expression is not syntactically valid.

Get all license and exception identifiers from the SpdxExpression.

Examples
let expression = SpdxExpression::parse("MIT OR Apache-2.0")?;
let licenses = expression.identifiers();
assert_eq!(licenses, HashSet::from_iter(["Apache-2.0".to_string(), "MIT".to_string()]));
let expression = SpdxExpression::parse("MIT OR GPL-2.0-only WITH Classpath-exception-2.0")?;
let licenses = expression.identifiers();
assert_eq!(
    licenses,
    HashSet::from_iter([
        "MIT".to_string(),
        "GPL-2.0-only".to_string(),
        "Classpath-exception-2.0".to_string()
    ])
);

Get all simple license expressions in Self. For licenses with exceptions, returns the license without the exception

Examples
let expression = SpdxExpression::parse(
    "(MIT OR Apache-2.0 AND (GPL-2.0-only WITH Classpath-exception-2.0 OR ISC))",
)
.unwrap();

let licenses = expression.licenses();

assert_eq!(
    licenses
        .iter()
        .map(|&license| license.identifier.as_str())
        .collect::<HashSet<_>>(),
    HashSet::from_iter(["Apache-2.0", "GPL-2.0-only", "ISC", "MIT"])
);

Get all exception identifiers for Self.

Examples
let expression = SpdxExpression::parse(
    "(MIT OR Apache-2.0 AND (GPL-2.0-only WITH Classpath-exception-2.0 OR ISC))",
)
.unwrap();

let exceptions = expression.exceptions();

assert_eq!(exceptions, HashSet::from_iter(["Classpath-exception-2.0"]));

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Formats the value using the given formatter. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.