1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// This file is part of helpers4.
// Copyright (C) 2025 baxyz
// SPDX-License-Identifier: LGPL-3.0-or-later
use std::fmt;
/// Why a string is not a valid SPDX license expression.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ParseExpressionError {
/// The expression is empty or only whitespace.
Empty,
/// The expression stops where a license or an operand is expected (`MIT OR`).
UnexpectedEnd,
/// A token that cannot come here: two licenses in a row, a stray `)`, an operator with
/// nothing before it.
UnexpectedToken {
/// Byte offset of the token in the expression.
index: usize,
},
/// A license identifier with a character outside letters, digits, `.`, `-` and `:`.
InvalidIdentifier {
/// Byte offset of the identifier in the expression.
index: usize,
},
/// An opening parenthesis that is never closed.
UnclosedParenthesis {
/// Byte offset of the `(`.
index: usize,
},
}
impl fmt::Display for ParseExpressionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Empty => write!(f, "empty license expression"),
Self::UnexpectedEnd => write!(f, "the expression ends too early"),
Self::UnexpectedToken { index } => write!(f, "unexpected token at byte {index}"),
Self::InvalidIdentifier { index } => {
write!(f, "invalid license identifier at byte {index}")
}
Self::UnclosedParenthesis { index } => {
write!(f, "unclosed parenthesis at byte {index}")
}
}
}
}
impl std::error::Error for ParseExpressionError {}
#[cfg(test)]
#[path = "error.test.rs"]
mod tests;