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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
pub mod exec_nix_store;
pub mod parsing;
use super::tree::{Path, Tree, TreePathMap};
use std::path::PathBuf;
use std::str::FromStr;
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct NixQueryDrv(PathBuf);
impl<T: ?Sized + AsRef<std::ffi::OsStr>> From<&T> for NixQueryDrv {
fn from(s: &T) -> NixQueryDrv {
NixQueryDrv(PathBuf::from(s.as_ref().to_os_string()))
}
}
impl std::ops::Deref for NixQueryDrv {
type Target = std::path::Path;
fn deref(&self) -> &std::path::Path {
&self.0
}
}
impl NixQueryDrv {
pub fn cmp_hash(&self, other: &Self) -> std::cmp::Ordering {
self.0.cmp(&other.0)
}
pub fn cmp_drv_name(&self, other: &Self) -> std::cmp::Ordering {
self.drv_name().cmp(&other.drv_name())
}
pub fn hash_and_drv_name(&self) -> String {
let drv_str = self.0.to_string_lossy();
String::from((drv_str).trim_start_matches("/nix/store/"))
}
pub fn short_hash_and_drv_name(&self) -> String {
let drv_str = self.0.to_string_lossy();
let drv_str_no_store =
String::from(drv_str.trim_start_matches("/nix/store/"));
let option_drv_name = drv_str_no_store
.find('-')
.and_then(|i| drv_str_no_store.get(i + 1..));
let option_short_hash = drv_str_no_store.get(0..7);
match (option_drv_name, option_short_hash) {
(Some(drv_name), Some(short_hash)) => {
format!("{}..{}", short_hash, drv_name)
}
_ => panic!("Ill-formed nix path"),
}
}
pub fn drv_name(&self) -> String {
let drv_str = self.0.to_string_lossy();
let option_dash_index = drv_str.find('-');
match option_dash_index {
None => drv_str.into_owned(),
Some(dash_index) => {
let option_just_drv_name = drv_str.get(dash_index + 1..);
match option_just_drv_name {
None => {
panic!("Nix paths will always have a dash in them.")
}
Some(drv_name) => drv_name.to_string(),
}
}
}
}
}
impl FromStr for NixQueryDrv {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(s.into())
}
}
impl std::fmt::Display for NixQueryDrv {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0.to_string_lossy())
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Recurse {
Yes,
No,
}
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct NixQueryEntry(pub NixQueryDrv, pub Recurse);
impl FromStr for NixQueryEntry {
type Err = nom::Err<(String, nom::error::ErrorKind)>;
fn from_str(s: &str) -> Result<Self, Self::Err> {
parsing::nix_query_entry_parser(s).map_err(|err| err.to_owned())
}
}
impl std::ops::Deref for NixQueryEntry {
type Target = std::path::Path;
fn deref(&self) -> &std::path::Path {
&self.0
}
}
impl NixQueryEntry {
pub fn new<T>(nix_query_drv: &T, recurse: Recurse) -> NixQueryEntry
where
T: ?Sized + AsRef<std::ffi::OsStr>,
{
NixQueryEntry(NixQueryDrv::from(nix_query_drv), recurse)
}
pub fn cmp_hash(&self, other: &Self) -> std::cmp::Ordering {
self.0.cmp_hash(&other.0)
}
pub fn cmp_drv_name(&self, other: &Self) -> std::cmp::Ordering {
self.0.cmp_drv_name(&other.0)
}
pub fn hash_and_drv_name(&self) -> String {
self.0.hash_and_drv_name()
}
pub fn short_hash_and_drv_name(&self) -> String {
self.0.short_hash_and_drv_name()
}
pub fn drv_name(&self) -> String {
self.0.drv_name()
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct NixQueryTree(pub Tree<NixQueryEntry>);
impl NixQueryTree {
pub fn path_map(&self) -> NixQueryPathMap {
let tree: &Tree<NixQueryEntry> = &self.0;
let tree_path_map =
tree.path_map_map(&|nix_query_entry| nix_query_entry.0.clone());
NixQueryPathMap(tree_path_map)
}
pub fn lookup(&self, path: Path) -> Option<&NixQueryEntry> {
self.0.lookup(path)
}
}
impl FromStr for NixQueryTree {
type Err = nom::Err<(String, nom::error::ErrorKind)>;
fn from_str(s: &str) -> Result<Self, Self::Err> {
parsing::nix_query_tree_parser(s).map_err(|err| err.to_owned())
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct NixQueryPathMap(pub TreePathMap<NixQueryDrv>);
impl NixQueryPathMap {
pub fn lookup_first(&self, k: &NixQueryDrv) -> Option<&Path> {
self.0.lookup_first(k)
}
}