Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,23 @@ include = [
]

[features]
default = ["std"]
std = ["regex?/std", "unicode-normalization?/std"]
# Includes the full canonical text of each license
text = []
# Allows analysis of text to determine if it might be an SPDX license text
detection = ["regex", "unicode-normalization"]
detection = ["std", "regex", "unicode-normalization"]
# Allows de/serialization of a spdx::detection::Store for quicker loading
detection-cache = ["detection", "zstd"]
detection-cache = ["std", "detection", "zstd"]
# Inlines a cache into this crate, which contains all of the licenses from the
# SPDX crate that the crate version was packaged with
detection-inline-cache = ["detection-cache"]
detection-inline-cache = ["std", "detection-cache"]
# Performs license detection in parallel within the same text
detection-parallel = ["detection", "rayon"]
detection-parallel = ["std", "detection", "rayon"]

[dependencies]
rayon = { version = "1.11", optional = true }
regex = { version = "1.12", optional = true }
regex = { version = "1.12", optional = true, default-features = false, features = ["perf", "unicode"] }
# In most cases expressions are quite small so we can avoid heap allocations
smallvec = "1.15"
unicode-normalization = { version = "0.1", optional = true }
Expand Down
5 changes: 3 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{error::Error, fmt};
use alloc::string::String;
use core::{error::Error, fmt};

/// An error related to parsing of an SPDX license expression
/// or identifier
Expand All @@ -8,7 +9,7 @@ pub struct ParseError {
pub original: String,
/// The range of characters in the original string that result
/// in this error
pub span: std::ops::Range<usize>,
pub span: core::ops::Range<usize>,
/// The specific reason for the error
pub reason: Reason,
}
Expand Down
8 changes: 5 additions & 3 deletions src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ mod minimize;
mod parser;

use crate::{LicenseReq, error::ParseError};
use alloc::string::String;
use alloc::vec::Vec;
use core::fmt;
pub use minimize::MinimizeError;
use smallvec::SmallVec;
use std::fmt;

/// A license requirement inside an SPDX license expression
///
Expand All @@ -16,7 +18,7 @@ pub struct ExpressionReq {
/// The license requirement
pub req: LicenseReq,
/// The span in the original license expression string containing the requirement
pub span: std::ops::Range<u32>,
pub span: core::ops::Range<u32>,
}

impl PartialEq for ExpressionReq {
Expand Down Expand Up @@ -238,7 +240,7 @@ impl fmt::Display for Expression {
}
}

impl std::str::FromStr for Expression {
impl core::str::FromStr for Expression {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::parse(s)
Expand Down
5 changes: 3 additions & 2 deletions src/expression/minimize.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::Expression;
use crate::{LicenseReq, Licensee};
use std::fmt;
use alloc::vec::Vec;
use core::fmt;

/// Errors that can occur when trying to minimize the requirements for an [`Expression`]
#[derive(Debug, PartialEq, Eq)]
Expand All @@ -25,7 +26,7 @@ impl fmt::Display for MinimizeError {
}
}

impl std::error::Error for MinimizeError {
impl core::error::Error for MinimizeError {
fn description(&self) -> &str {
match self {
Self::TooManyRequirements(_) => "too many requirements in license expression",
Expand Down
7 changes: 5 additions & 2 deletions src/expression/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ use crate::{
expression::{ExprNode, Expression, ExpressionReq, Operator},
lexer::{Lexer, Token},
};
use alloc::borrow::ToOwned;
use alloc::boxed::Box;
use alloc::string::String;
use smallvec::SmallVec;

impl Expression {
Expand Down Expand Up @@ -163,7 +166,7 @@ impl Expression {

struct OpAndSpan {
op: Op,
span: std::ops::Range<usize>,
span: core::ops::Range<usize>,
}

let lexer = Lexer::new_mode(original, mode);
Expand All @@ -184,7 +187,7 @@ impl Expression {
Ok(())
};

let make_err_for_token = |last_token: Option<Token<'_>>, span: std::ops::Range<usize>| {
let make_err_for_token = |last_token: Option<Token<'_>>, span: core::ops::Range<usize>| {
let expected: &[&str] = match last_token {
None | Some(Token::And | Token::Or | Token::OpenParen) => &["<license>", "("],
Some(Token::CloseParen) => &["AND", "OR"],
Expand Down
9 changes: 5 additions & 4 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
ExceptionId, LicenseId,
error::{ParseError, Reason},
};
use alloc::borrow::ToOwned;

/// Parsing configuration for SPDX expression
#[derive(Default, Copy, Clone)]
Expand Down Expand Up @@ -106,9 +107,9 @@ pub enum Token<'a> {
Or,
}

impl std::fmt::Display for Token<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(self, f)
impl core::fmt::Display for Token<'_> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
core::fmt::Debug::fmt(self, f)
}
}

Expand Down Expand Up @@ -244,7 +245,7 @@ pub struct LexerToken<'a> {
/// The token that was lexed
pub token: Token<'a>,
/// The range of the token characters in the original license expression
pub span: std::ops::Range<usize>,
pub span: core::ops::Range<usize>,
}

impl<'a> Iterator for Lexer<'a> {
Expand Down
23 changes: 14 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#![cfg_attr(docsrs, feature(doc_cfg))]
#![deny(missing_docs)]
#![doc = include_str!("../README.md")]
#![cfg_attr(not(feature = "std"), no_std)]

extern crate alloc;

/// Error types
pub mod error;
Expand All @@ -21,14 +24,16 @@ pub mod detection;
#[allow(missing_docs)]
pub mod text;

use alloc::boxed::Box;
use alloc::string::String;
use core::{
cmp::{self, Ordering},
fmt,
};
pub use error::ParseError;
pub use expression::Expression;
pub use lexer::ParseMode;
pub use licensee::Licensee;
use std::{
cmp::{self, Ordering},
fmt,
};

/// Flags that can apply to licenses and/or license exceptions
pub mod flags {
Expand Down Expand Up @@ -87,7 +92,7 @@ impl Eq for LicenseId {}

impl Ord for LicenseId {
#[inline]
fn cmp(&self, o: &Self) -> Ordering {
fn cmp(&self, o: &Self) -> core::cmp::Ordering {
self.l.index.cmp(&o.l.index)
}
}
Expand All @@ -99,7 +104,7 @@ impl PartialOrd for LicenseId {
}
}

impl std::ops::Deref for LicenseId {
impl core::ops::Deref for LicenseId {
type Target = License;

#[inline]
Expand Down Expand Up @@ -253,7 +258,7 @@ impl PartialOrd for ExceptionId {
}
}

impl std::ops::Deref for ExceptionId {
impl core::ops::Deref for ExceptionId {
type Target = Exception;

#[inline]
Expand Down Expand Up @@ -585,7 +590,7 @@ pub fn gnu_license_id(base: &str, or_later: bool) -> Option<LicenseId> {
v[base.len()..].copy_from_slice(b"-only");
}

let Ok(s) = std::str::from_utf8(v.as_slice()) else {
let Ok(s) = core::str::from_utf8(v.as_slice()) else {
// Unreachable, but whatever
return None;
};
Expand Down Expand Up @@ -645,8 +650,8 @@ pub fn license_version() -> &'static str {
#[cfg(test)]
mod test {
use super::LicenseItem;

use crate::{Expression, license_id};
use alloc::string::ToString;

#[test]
fn gnu_or_later_display() {
Expand Down
12 changes: 9 additions & 3 deletions src/licensee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use crate::{
error::{ParseError, Reason},
lexer::{Lexer, Token},
};
use std::fmt;
use alloc::borrow::ToOwned;
use alloc::boxed::Box;
use alloc::string::String;
use core::fmt;

/// A convenience wrapper for a license and optional additional text that can be
/// checked against a license requirement to see if it satisfies the requirement
Expand All @@ -26,7 +29,7 @@ impl fmt::Display for Licensee {
}
}

impl std::str::FromStr for Licensee {
impl core::str::FromStr for Licensee {
type Err = ParseError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Expand Down Expand Up @@ -243,7 +246,7 @@ impl Licensee {

impl PartialOrd<LicenseReq> for Licensee {
#[inline]
fn partial_cmp(&self, o: &LicenseReq) -> Option<std::cmp::Ordering> {
fn partial_cmp(&self, o: &LicenseReq) -> Option<core::cmp::Ordering> {
self.inner.partial_cmp(o)
}
}
Expand All @@ -267,6 +270,9 @@ mod test {
use crate::{
AdditionItem, LicenseItem, LicenseRef, LicenseReq, Licensee, exception_id, license_id,
};
use alloc::borrow::ToOwned;
use alloc::boxed::Box;
use alloc::vec::Vec;

const LICENSEES: &[&str] = &[
"LicenseRef-Embark-Proprietary",
Expand Down
Loading