-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathlicense.rs
More file actions
42 lines (36 loc) · 1.25 KB
/
Copy pathlicense.rs
File metadata and controls
42 lines (36 loc) · 1.25 KB
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
use askalono::Store;
use once_cell::sync::Lazy;
use spdx::{Expression, ParseMode};
use tracing::debug;
use crate::utils::ResultExt;
pub static LICENSE_STORE: Lazy<Option<Store>> = Lazy::new(|| {
Store::from_cache(include_bytes!("../data/license-store-cache.zstd") as &[_]).ok_warn()
});
include!("../data/get_nix_license.rs");
pub fn parse_spdx_expression(license: &str, source: &'static str) -> Vec<&'static str> {
Expression::parse_mode(license, ParseMode::LAX)
.ok_warn()
.map_or_else(Vec::new, |expr| {
expr.requirements()
.filter_map(|req| {
let license = get_nix_license(req.req.license.id()?.name)?;
debug!("license from {source}: {license}");
Some(license)
})
.collect()
})
}
#[cfg(test)]
mod tests {
use super::parse_spdx_expression;
#[test]
fn basic() {
assert_eq!(parse_spdx_expression("MPL-2.0", ""), ["mpl20"]);
assert_eq!(parse_spdx_expression("GPL-3.0", ""), ["gpl3Only"]);
assert_eq!(parse_spdx_expression("unknown license", ""), [""; 0]);
assert_eq!(
parse_spdx_expression("MIT or Apache-2.0", ""),
["mit", "asl20"],
);
}
}