Struct spdx_rs::models::SpdxExpression
source · pub struct SpdxExpression { /* private fields */ }
Expand description
Main struct for SPDX License Expressions.
Implementations§
source§impl SpdxExpression
impl SpdxExpression
sourcepub fn parse(expression: &str) -> Result<SpdxExpression, SpdxExpressionError>
pub fn parse(expression: &str) -> Result<SpdxExpression, SpdxExpressionError>
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.
sourcepub fn identifiers(&self) -> HashSet<String, RandomState>
pub fn identifiers(&self) -> HashSet<String, RandomState>
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()
])
);
sourcepub fn licenses(&self) -> HashSet<&SimpleExpression, RandomState>
pub fn licenses(&self) -> HashSet<&SimpleExpression, RandomState>
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"])
);
sourcepub fn exceptions(&self) -> HashSet<&str, RandomState>
pub fn exceptions(&self) -> HashSet<&str, RandomState>
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§
source§impl Clone for SpdxExpression
impl Clone for SpdxExpression
source§fn clone(&self) -> SpdxExpression
fn clone(&self) -> SpdxExpression
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl Debug for SpdxExpression
impl Debug for SpdxExpression
source§impl Default for SpdxExpression
impl Default for SpdxExpression
source§fn default() -> SpdxExpression
fn default() -> SpdxExpression
Returns the “default value” for a type. Read more
source§impl<'de> Deserialize<'de> for SpdxExpression
impl<'de> Deserialize<'de> for SpdxExpression
source§fn deserialize<D>(
deserializer: D
) -> Result<SpdxExpression, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>( deserializer: D ) -> Result<SpdxExpression, <D as Deserializer<'de>>::Error>where D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
source§impl Display for SpdxExpression
impl Display for SpdxExpression
source§impl PartialEq<SpdxExpression> for SpdxExpression
impl PartialEq<SpdxExpression> for SpdxExpression
source§fn eq(&self, other: &SpdxExpression) -> bool
fn eq(&self, other: &SpdxExpression) -> bool
This method tests for
self
and other
values to be equal, and is used
by ==
.source§impl Serialize for SpdxExpression
impl Serialize for SpdxExpression
source§fn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>( &self, serializer: S ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where S: Serializer,
Serialize this value into the given Serde serializer. Read more
impl Eq for SpdxExpression
impl StructuralEq for SpdxExpression
impl StructuralPartialEq for SpdxExpression
Auto Trait Implementations§
impl RefUnwindSafe for SpdxExpression
impl Send for SpdxExpression
impl Sync for SpdxExpression
impl Unpin for SpdxExpression
impl UnwindSafe for SpdxExpression
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more