]>
Commit | Line | Data |
---|---|---|
487cf647 FG |
1 | use std::collections::HashMap; |
2 | ||
3 | const COPYRIGHT_PREFIXES: &[&str] = &["SPDX-FileCopyrightText:", "Copyright", "(c)", "(C)", "©"]; | |
4 | ||
5 | pub(crate) struct LicensesInterner { | |
6 | by_id: Vec<License>, | |
7 | by_struct: HashMap<License, usize>, | |
8 | } | |
9 | ||
10 | impl LicensesInterner { | |
11 | pub(crate) fn new() -> Self { | |
12 | LicensesInterner { by_id: Vec::new(), by_struct: HashMap::new() } | |
13 | } | |
14 | ||
15 | pub(crate) fn intern(&mut self, mut license: License) -> LicenseId { | |
16 | license.simplify(); | |
17 | if let Some(id) = self.by_struct.get(&license) { | |
18 | LicenseId(*id) | |
19 | } else { | |
20 | let id = self.by_id.len(); | |
21 | self.by_id.push(license.clone()); | |
22 | self.by_struct.insert(license, id); | |
23 | LicenseId(id) | |
24 | } | |
25 | } | |
26 | ||
27 | pub(crate) fn resolve(&self, id: LicenseId) -> &License { | |
28 | &self.by_id[id.0] | |
29 | } | |
30 | } | |
31 | ||
32 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, serde::Serialize)] | |
33 | #[serde(transparent)] | |
34 | pub(crate) struct LicenseId(usize); | |
35 | ||
36 | #[derive(Clone, Hash, PartialEq, Eq, serde::Serialize)] | |
37 | pub(crate) struct License { | |
38 | pub(crate) spdx: String, | |
39 | pub(crate) copyright: Vec<String>, | |
40 | } | |
41 | ||
42 | impl License { | |
43 | fn simplify(&mut self) { | |
44 | self.remove_copyright_prefixes(); | |
353b0b11 | 45 | self.remove_trailing_dots(); |
487cf647 FG |
46 | self.copyright.sort(); |
47 | self.copyright.dedup(); | |
48 | } | |
49 | ||
50 | fn remove_copyright_prefixes(&mut self) { | |
51 | for copyright in &mut self.copyright { | |
52 | let mut stripped = copyright.trim(); | |
53 | let mut previous_stripped; | |
54 | loop { | |
55 | previous_stripped = stripped; | |
56 | for pattern in COPYRIGHT_PREFIXES { | |
57 | stripped = stripped.trim_start_matches(pattern).trim_start(); | |
58 | } | |
59 | if stripped == previous_stripped { | |
60 | break; | |
61 | } | |
62 | } | |
63 | *copyright = stripped.into(); | |
64 | } | |
65 | } | |
353b0b11 FG |
66 | |
67 | fn remove_trailing_dots(&mut self) { | |
68 | for copyright in &mut self.copyright { | |
69 | if copyright.ends_with('.') { | |
70 | *copyright = copyright.trim_end_matches('.').to_string(); | |
71 | } | |
72 | } | |
73 | } | |
487cf647 | 74 | } |