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
use std::collections::HashMap;
use std::error::Error;
use std::ffi::OsStr;
use std::fs::File;
use std::io::prelude::*;
use walkdir::WalkDir;
use license::LicenseContent;
#[derive(Serialize, Deserialize)]
pub struct LicenseData {
pub original: LicenseContent,
pub alternates: Vec<LicenseContent>,
pub headers: Vec<LicenseContent>,
pub reference_score: f32,
}
#[derive(Default, Serialize, Deserialize)]
pub struct Store {
pub licenses: HashMap<String, LicenseData>,
}
impl LicenseData {
pub fn new(original: LicenseContent) -> LicenseData {
LicenseData {
original,
alternates: Vec::new(),
headers: Vec::new(),
reference_score: 0.0,
}
}
}
impl Store {
pub fn new() -> Store {
Store {
licenses: HashMap::new(),
}
}
pub fn load_spdx(&mut self, dir: &str, include_texts: bool) -> Result<(), Box<Error>> {
use json::{from_str, Value};
for entry in WalkDir::new(dir).into_iter().filter_map(|e| e.ok()) {
let path = entry.path();
if !path.is_file() || path.extension().unwrap_or_else(|| OsStr::new("")) != "json" {
continue;
}
let mut f = File::open(path)?;
let mut data = String::new();
f.read_to_string(&mut data)?;
let val: Value = from_str(&data)?;
let name = val["licenseId"].as_str().ok_or("missing licenseId")?;
let text = val["licenseText"].as_str().ok_or("missing licenseText")?;
let template = val["standardLicenseTemplate"].as_str();
let header = val["standardLicenseHeader"].as_str();
info!("Processing {}", name);
let content = LicenseContent::from_text(text, include_texts);
let license = self.licenses
.entry(name.to_owned())
.or_insert_with(|| LicenseData::new(content));
if template.is_some() {
let template_content = LicenseContent::from_text(template.unwrap(), include_texts);
license.reference_score = license
.original
.grams
.combined_dice(&template_content.grams);
}
if header.is_some() {
license.headers = vec![LicenseContent::from_text(header.unwrap(), include_texts)];
}
}
Ok(())
}
}