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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;
use camino::Utf8PathBuf;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Dependency {
pub name: String,
pub version: String,
pub licenses: Vec<String>,
pub location: Utf8PathBuf,
pub source: Option<String>,
pub metadata: HashMap<String, String>,
}
impl Dependency {
pub fn new(name: String, version: String) -> Self {
Self {
name,
version,
licenses: Vec::new(),
location: Utf8PathBuf::new(),
source: None,
metadata: HashMap::new(),
}
}
pub fn with_location(mut self, location: Utf8PathBuf) -> Self {
self.location = location;
self
}
pub fn with_source(mut self, source: String) -> Self {
self.source = Some(source);
self
}
pub fn with_license(mut self, license: String) -> Self {
self.licenses.push(license);
self
}
pub fn with_licenses(mut self, licenses: Vec<String>) -> Self {
self.licenses = licenses;
self
}
pub fn add_metadata(mut self, key: String, value: String) -> Self {
self.metadata.insert(key, value);
self
}
pub fn id(&self) -> String {
format!("{}:{}", self.name, self.version)
}
pub fn has_licenses(&self) -> bool {
!self.licenses.is_empty()
}
pub fn license_display(&self) -> String {
if self.licenses.is_empty() {
"".to_string()
} else {
self.licenses.join(" AND ")
}
}
}
impl fmt::Display for Dependency {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} ({})", self.name, self.version)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DependencyCollection {
dependencies: Vec<Dependency>,
}
impl DependencyCollection {
pub fn new() -> Self {
Self {
dependencies: Vec::new(),
}
}
pub fn add(&mut self, dependency: Dependency) {
self.dependencies.push(dependency);
}
pub fn extend(&mut self, other: DependencyCollection) {
self.dependencies.extend(other.dependencies);
}
pub fn iter(&self) -> impl Iterator<Item = &Dependency> {
self.dependencies.iter()
}
pub fn into_iter(self) -> impl Iterator<Item = Dependency> {
self.dependencies.into_iter()
}
pub fn len(&self) -> usize {
self.dependencies.len()
}
pub fn is_empty(&self) -> bool {
self.dependencies.is_empty()
}
pub fn sort_by_name(&mut self) {
self.dependencies.sort_by(|a, b| a.name.cmp(&b.name));
}
pub fn filter_by_location(&self, location: &Utf8PathBuf) -> DependencyCollection {
let filtered: Vec<Dependency> = self
.dependencies
.iter()
.filter(|dep| dep.location == *location)
.cloned()
.collect();
DependencyCollection {
dependencies: filtered,
}
}
pub fn unique_licenses(&self) -> Vec<String> {
let mut licenses = std::collections::HashSet::new();
for dep in &self.dependencies {
for license in &dep.licenses {
licenses.insert(license.clone());
}
}
let mut unique_licenses: Vec<String> = licenses.into_iter().collect();
unique_licenses.sort();
unique_licenses
}
}
impl Default for DependencyCollection {
fn default() -> Self {
Self::new()
}
}
impl From<Vec<Dependency>> for DependencyCollection {
fn from(dependencies: Vec<Dependency>) -> Self {
Self { dependencies }
}
}
impl IntoIterator for DependencyCollection {
type Item = Dependency;
type IntoIter = std::vec::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
self.dependencies.into_iter()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_dependency_creation() {
let dep = Dependency::new("test".to_string(), "1.0.0".to_string());
assert_eq!(dep.name, "test");
assert_eq!(dep.version, "1.0.0");
assert!(dep.licenses.is_empty());
}
#[test]
fn test_dependency_builder() {
let dep = Dependency::new("test".to_string(), "1.0.0".to_string())
.with_license("MIT".to_string())
.with_source("rubygems".to_string());
assert_eq!(dep.licenses, vec!["MIT"]);
assert_eq!(dep.source, Some("rubygems".to_string()));
}
#[test]
fn test_dependency_id() {
let dep = Dependency::new("test".to_string(), "1.0.0".to_string());
assert_eq!(dep.id(), "test:1.0.0");
}
#[test]
fn test_dependency_collection() {
let mut collection = DependencyCollection::new();
let dep = Dependency::new("test".to_string(), "1.0.0".to_string());
collection.add(dep);
assert_eq!(collection.len(), 1);
assert!(!collection.is_empty());
}
}
|