forked from sstadick/cargo-bundle-licenses
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinalized_license.rs
101 lines (89 loc) · 2.8 KB
/
finalized_license.rs
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//! A finalized license representation meant for serializing / deserialzing
use std::collections::HashMap;
use cargo_metadata::Package;
use serde::{Deserialize, Serialize};
use crate::license::License;
pub static LICENSE_NOT_FOUNT_TEXT: &str = "NOT FOUND";
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
pub struct LicenseAndText {
/// The license itself in SPDX format
pub license: String,
/// The lines of the license text, or NOT FOUND
pub text: String,
}
impl LicenseAndText {
pub fn new(license: &License, text: String) -> Self {
Self {
license: license.to_string(),
text,
}
}
}
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct FinalizedLicense {
/// The name of the package this license is for.
pub package_name: String,
/// The version of the package this license is for.
pub package_version: String,
/// The full license from the Cargo.toml
pub license: String,
/// The licenses and their associated text
pub licenses: Vec<LicenseAndText>,
}
impl FinalizedLicense {
pub fn new(package: &Package, license: License, licenses: Vec<LicenseAndText>) -> Self {
Self {
package_name: package.name.clone(),
package_version: package.version.to_string(),
license: license.to_string(),
licenses,
}
}
}
impl PartialEq for FinalizedLicense {
fn eq(&self, other: &Self) -> bool {
if self.package_version != other.package_version || self.package_name != other.package_name
{
return false;
}
for (a, b) in self.licenses.iter().zip(other.licenses.iter()) {
if a != b {
return false;
}
}
true
}
}
/// Hashable struct to for a package for easy lookup
// TODO: could probably use package id? or str refs?
#[derive(Clone, Hash, PartialEq, Eq)]
pub struct LicenseKey {
package_name: String,
package_version: String,
}
impl LicenseKey {
pub fn new(package_name: String, package_version: String) -> Self {
Self {
package_name,
package_version,
}
}
}
/// Get a lookup of package name + package version to another hashmpa of the licenses for that package.
pub fn finalized_licenses_lookup(
licenses: &[FinalizedLicense],
) -> HashMap<LicenseKey, HashMap<String, LicenseAndText>> {
let mut map = HashMap::new();
for final_license in licenses {
let mut inner_map = HashMap::new();
for lic in &final_license.licenses {
inner_map.insert(lic.license.to_string(), lic.clone());
}
let key = LicenseKey::new(
final_license.package_name.clone(),
final_license.package_version.clone(),
);
map.insert(key, inner_map);
}
map
}