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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
use crate::recipe::Derivation;
use super::{
object::{Blob, Tree},
ObjectId, PackageId,
};
use console::style;
use semver::Version;
use serde::{Deserialize, Serialize};
use std::{
collections::{BTreeMap, HashMap, HashSet},
fmt::{self},
path::{Path, PathBuf},
};
use url::Url;
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct LocalPackageSource {
pub drv: Derivation,
pub path: PathBuf,
}
impl LocalPackageSource {
pub fn new(drv: Derivation, path: PathBuf) -> Self {
Self { drv, path }
}
pub fn name(&self) -> &String {
&self.drv.name
}
pub fn version(&self) -> &Version {
&self.drv.version
}
pub fn path_in_store<P: AsRef<Path>>(&self, store_path: P, id: &PackageId) -> PathBuf {
self.drv.path_in_store(store_path, id)
}
}
impl fmt::Display for LocalPackageSource {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Path: {:?}\nDerivation: {}", self.path, self.drv)
}
}
pub struct RemotePackageSource {
pub id: PackageId,
pub drv: Derivation,
pub base: Url,
pub blobs: BTreeMap<Blob, ObjectId>,
pub trees: BTreeMap<Tree, ObjectId>,
}
impl ToString for RemotePackageSource {
fn to_string(&self) -> String {
format!(
"{} {}",
style(&self.base).red(),
style(&self.drv.name).blue()
)
}
}
impl RemotePackageSource {
pub fn new(
id: PackageId,
drv: Derivation,
base: Url,
blobs: BTreeMap<Blob, ObjectId>,
trees: BTreeMap<Tree, ObjectId>,
) -> Self {
Self {
id,
drv,
base,
blobs,
trees,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Packages {
nodes: HashMap<PackageId, Derivation>,
children: HashMap<PackageId, HashSet<ObjectId>>,
}
impl Packages {
pub fn new() -> Self {
Self::default()
}
pub fn contains_drv(&self, drv: &Derivation) -> Option<PackageId> {
self.nodes
.iter()
.find_map(|(id, other)| if drv == other { Some(*id) } else { None })
}
pub fn contains(&self, id: &PackageId) -> bool {
self.nodes.contains_key(id)
}
pub fn insert(
&mut self,
id: PackageId,
drv: Derivation,
objects: HashSet<ObjectId>,
) -> Option<Derivation> {
self.children.insert(id, objects);
self.nodes.insert(id, drv)
}
pub fn insert_child(&mut self, id: &PackageId, child: ObjectId) -> Option<bool> {
if let Some(children) = self.children.get_mut(id) {
Some(children.insert(child))
} else {
None
}
}
pub fn get(&self, id: &PackageId) -> Option<&Derivation> {
self.nodes.get(id)
}
pub unsafe fn get_unchecked(&self, id: &PackageId) -> &Derivation {
self.nodes.get(id).unwrap_unchecked()
}
pub fn get_children(&self, id: &PackageId) -> Option<&HashSet<ObjectId>> {
self.children.get(id)
}
pub fn get_full(&self, id: &PackageId) -> Option<(&Derivation, &HashSet<ObjectId>)> {
if let Some(drv) = self.nodes.get(id) {
self.children.get(id).map(|objects| ((drv, objects)))
} else {
None
}
}
pub fn remove(&mut self, id: &PackageId) -> Option<(Derivation, HashSet<ObjectId>)> {
let desc = self.nodes.remove(id);
let children = self.children.remove(id);
if let Some(drv) = desc && let Some(children) = children {
Some((drv, children))
} else {
None
}
}
pub fn path_in_store<P: AsRef<Path>>(&self, id: &PackageId, store_path: P) -> Option<PathBuf> {
if let Some(desc) = self.get(id) {
let name_version_id = format!("{}-{}-{}", desc.name, desc.version, id);
Some(store_path.as_ref().join(name_version_id))
} else {
None
}
}
pub fn url_in_store(&self, id: &PackageId, store_url: &Url) -> Option<Url> {
if let Some(desc) = self.get(id) {
let name_version_id = format!("{}-{}-{}/", desc.name, desc.version, id);
Some(
store_url
.join(&name_version_id)
.expect("INTERNAL ERROR: The package part of an url must be parseable"),
)
} else {
None
}
}
pub fn filter<P>(
&self,
predicate: P,
) -> impl Iterator<Item = (&PackageId, &Derivation, &HashSet<ObjectId>)>
where
P: Fn(&PackageId, &Derivation, &HashSet<ObjectId>) -> bool,
{
self.nodes.iter().filter_map(move |(id, drv)| {
let objects = unsafe { self.children.get(id).unwrap_unchecked() };
if predicate(id, drv, objects) {
Some((id, drv, objects))
} else {
None
}
})
}
pub fn filter_by_name_starting_with<'a>(
&'a self,
name: &'a str,
) -> impl Iterator<Item = (&PackageId, &Derivation, &HashSet<ObjectId>)> + '_ {
self.filter(move |_id, desc, _objects| desc.name.starts_with(name))
}
pub fn filter_by_name_containing<'a>(
&'a self,
name: &'a str,
) -> impl Iterator<Item = (&PackageId, &Derivation, &HashSet<ObjectId>)> + '_ {
self.filter(move |_id, desc, _objects| desc.name.contains(name))
}
pub fn find<P: Fn(&PackageId, &Derivation, &HashSet<ObjectId>) -> bool>(
&self,
predicate: P,
) -> Option<(&PackageId, &Derivation)> {
self.nodes.iter().find(move |(id, drv)| {
let objects = unsafe { self.children.get(id).unwrap_unchecked() };
predicate(*id, *drv, objects)
})
}
pub fn find_by_name_starting_with(&self, name: &str) -> Option<(&PackageId, &Derivation)> {
self.find(|_id, p, _objects| p.name.starts_with(name))
}
pub fn find_by_name_containing(&self, name: &str) -> Option<(&PackageId, &Derivation)> {
self.find(|_id, p, _objects| p.name.contains(name))
}
pub fn find_by_name(&self, name: &str) -> Option<(&PackageId, &Derivation)> {
self.find(|_id, p, _objects| p.name == name)
}
pub fn find_package_id(&self, object_id: &ObjectId) -> Option<&PackageId> {
self.children.iter().find_map(|(package_id, set)| {
if set.contains(object_id) {
Some(package_id)
} else {
None
}
})
}
}