Struct spdx::expression::Expression[][src]

pub struct Expression { /* fields omitted */ }
Expand description

An SPDX license expression that is both syntactically and semantically valid, and can be evaluated

use spdx::Expression;

let this_is_fine = Expression::parse("MIT OR Apache-2.0").unwrap();
assert!(this_is_fine.evaluate(|req| {
    if let spdx::LicenseItem::Spdx { id, .. } = req.license {
        // Both MIT and Apache-2.0 are OSI approved, so this expression
        // evaluates to true
        return id.is_osi_approved();
    }

   false
}));

assert!(!this_is_fine.evaluate(|req| {
    if let spdx::LicenseItem::Spdx { id, .. } = req.license {
        // This is saying we don't accept any licenses that are OSI approved
        // so the expression will evaluate to false as both sides of the OR
        // are now rejected
        return !id.is_osi_approved();
    }

    false
}));

// `NOPE` is not a valid SPDX license identifier, so this expression
// will fail to parse
let _this_is_not = Expression::parse("MIT OR NOPE").unwrap_err();

Implementations

Given a license expression, attempts to parse and validate it as a valid SPDX expression. Uses ParseMode::Strict.

The validation can fail for many reasons:

  • The expression contains invalid characters
  • An unknown/invalid license or exception identifier was found. Only SPDX short identifiers are allowed
  • The expression contained unbalanced parentheses
  • A license or exception immediately follows another license or exception, without a valid AND, OR, or WITH operator separating them
  • An AND, OR, or WITH doesn’t have a license or ) preceding it
spdx::Expression::parse("MIT OR Apache-2.0 WITH LLVM-exception").unwrap();

Parses an expression with the specified ParseMode. With ParseMode::Lax it permits some non-SPDX syntax, such as imprecise license names and “/” used instead of “OR” in exprssions.

spdx::Expression::parse_mode(
    "mit/Apache-2.0 WITH LLVM-exception",
    spdx::ParseMode::Lax
).unwrap();

Returns each of the license requirements in the license expression, but not the operators that join them together

let expr = spdx::Expression::parse("MIT AND BSD-2-Clause").unwrap();

assert_eq!(
    &expr.requirements().map(|er| er.req.license.id()).collect::<Vec<_>>(), &[
        spdx::license_id("MIT"),
        spdx::license_id("BSD-2-Clause")
    ]
);

Returns both the license requirements and the operators that join them together. Note that the expression is returned in post fix order.

use spdx::expression::{ExprNode, Operator};
let expr = spdx::Expression::parse("Apache-2.0 OR MIT").unwrap();

let mut ei = expr.iter();

assert!(ei.next().is_some()); // Apache
assert!(ei.next().is_some()); // MIT
assert_eq!(*ei.next().unwrap(), ExprNode::Op(Operator::Or));

Evaluates the expression, using the provided function to determine if the licensee meets the requirements for each license term. If enough requirements are satisfied the evaluation will return true.

use spdx::Expression;

let this_is_fine = Expression::parse("MIT OR Apache-2.0").unwrap();
assert!(this_is_fine.evaluate(|req| {
    // If we find MIT, then we're happy!
    req.license.id() == spdx::license_id("MIT")
}));

Just as with evaluate, the license expression is evaluated to see if enough license requirements in the expresssion are met for the evaluation to succeed, except this method also keeps track of each failed requirement and returns them, allowing for more detailed error reporting about precisely what terms in the expression caused the overall failure

Trait Implementations

Performs the conversion.

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

Performs the conversion.

Performs the conversion.

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)

recently added

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.