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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
use std::cmp::Ordering;
use std::ffi::{c_char, c_int, c_void, CStr, CString};
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::{fmt, ptr};
use pkgcraft::dep::{self, Dep, Flatten, Recursive, Uri};
use pkgcraft::eapi::{Eapi, IntoEapi};
use pkgcraft::set::Ordered;
use pkgcraft::utils::hash;
use crate::macros::*;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub enum DepSpecUnit {
Dep,
String,
Uri,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub enum DepSetKind {
Dependencies,
Restrict,
RequiredUse,
Properties,
SrcUri,
License,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum DepSetWrapper {
Dep(dep::DepSet<Dep>),
String(dep::DepSet<String>),
Uri(dep::DepSet<Uri>),
}
impl fmt::Display for DepSetWrapper {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Dep(d) => write!(f, "{d}"),
Self::String(d) => write!(f, "{d}"),
Self::Uri(d) => write!(f, "{d}"),
}
}
}
#[derive(Debug, Clone)]
#[repr(C)]
pub struct DepSet {
unit: DepSpecUnit,
kind: DepSetKind,
dep: *mut DepSetWrapper,
}
impl Drop for DepSet {
fn drop(&mut self) {
unsafe {
drop(Box::from_raw(self.dep));
}
}
}
impl DepSet {
pub(crate) fn new_dep(d: dep::DepSet<Dep>) -> Self {
Self {
unit: DepSpecUnit::Dep,
kind: DepSetKind::Dependencies,
dep: Box::into_raw(Box::new(DepSetWrapper::Dep(d))),
}
}
pub(crate) fn new_string(d: dep::DepSet<String>, kind: DepSetKind) -> Self {
Self {
unit: DepSpecUnit::String,
kind,
dep: Box::into_raw(Box::new(DepSetWrapper::String(d))),
}
}
pub(crate) fn new_uri(d: dep::DepSet<Uri>) -> Self {
Self {
unit: DepSpecUnit::Uri,
kind: DepSetKind::SrcUri,
dep: Box::into_raw(Box::new(DepSetWrapper::Uri(d))),
}
}
}
impl Hash for DepSet {
fn hash<H: Hasher>(&self, state: &mut H) {
self.deref().hash(state)
}
}
impl PartialEq for DepSet {
fn eq(&self, other: &Self) -> bool {
self.deref().eq(other.deref())
}
}
impl Eq for DepSet {}
impl Deref for DepSet {
type Target = DepSetWrapper;
fn deref(&self) -> &Self::Target {
null_ptr_check!(self.dep.as_ref())
}
}
impl fmt::Display for DepSet {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.deref())
}
}
#[derive(Debug)]
pub enum DepSpecIntoIter {
Dep(dep::spec::IntoIter<Dep>),
String(dep::spec::IntoIter<String>),
Uri(dep::spec::IntoIter<Uri>),
}
impl Iterator for DepSpecIntoIter {
type Item = DepSpec;
fn next(&mut self) -> Option<Self::Item> {
match self {
Self::Dep(iter) => iter.next().map(DepSpec::new_dep),
Self::String(iter) => iter.next().map(DepSpec::new_string),
Self::Uri(iter) => iter.next().map(DepSpec::new_uri),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum DepSpecWrapper {
Dep(dep::DepSpec<Dep>),
String(dep::DepSpec<String>),
Uri(dep::DepSpec<Uri>),
}
impl fmt::Display for DepSpecWrapper {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Dep(d) => write!(f, "{d}"),
Self::String(d) => write!(f, "{d}"),
Self::Uri(d) => write!(f, "{d}"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub enum DepSpecKind {
Enabled,
Disabled,
AllOf,
AnyOf,
ExactlyOneOf,
AtMostOneOf,
UseEnabled,
UseDisabled,
}
impl<T: Ordered> From<&dep::DepSpec<T>> for DepSpecKind {
fn from(d: &dep::DepSpec<T>) -> Self {
use dep::DepSpec::*;
match d {
Enabled(_) => Self::Enabled,
Disabled(_) => Self::Disabled,
AllOf(_) => Self::AllOf,
AnyOf(_) => Self::AnyOf,
ExactlyOneOf(_) => Self::ExactlyOneOf,
AtMostOneOf(_) => Self::AtMostOneOf,
UseEnabled(_, _) => Self::UseEnabled,
UseDisabled(_, _) => Self::UseDisabled,
}
}
}
#[derive(Debug, Clone)]
#[repr(C)]
pub struct DepSpec {
unit: DepSpecUnit,
kind: DepSpecKind,
dep: *mut DepSpecWrapper,
}
impl Drop for DepSpec {
fn drop(&mut self) {
unsafe {
drop(Box::from_raw(self.dep));
}
}
}
impl DepSpec {
pub(crate) fn new_dep(d: dep::DepSpec<Dep>) -> Self {
Self {
unit: DepSpecUnit::Dep,
kind: DepSpecKind::from(&d),
dep: Box::into_raw(Box::new(DepSpecWrapper::Dep(d))),
}
}
pub(crate) fn new_string(d: dep::DepSpec<String>) -> Self {
Self {
unit: DepSpecUnit::String,
kind: DepSpecKind::from(&d),
dep: Box::into_raw(Box::new(DepSpecWrapper::String(d))),
}
}
pub(crate) fn new_uri(d: dep::DepSpec<Uri>) -> Self {
Self {
unit: DepSpecUnit::Uri,
kind: DepSpecKind::from(&d),
dep: Box::into_raw(Box::new(DepSpecWrapper::Uri(d))),
}
}
}
impl Hash for DepSpec {
fn hash<H: Hasher>(&self, state: &mut H) {
self.deref().hash(state)
}
}
impl Ord for DepSpec {
fn cmp(&self, other: &Self) -> Ordering {
self.deref().cmp(other.deref())
}
}
impl PartialOrd for DepSpec {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl PartialEq for DepSpec {
fn eq(&self, other: &Self) -> bool {
self.cmp(other) == Ordering::Equal
}
}
impl Eq for DepSpec {}
impl Deref for DepSpec {
type Target = DepSpecWrapper;
fn deref(&self) -> &Self::Target {
null_ptr_check!(self.dep.as_ref())
}
}
impl fmt::Display for DepSpec {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.deref())
}
}
#[derive(Debug)]
pub enum DepSpecIntoIterFlatten {
Dep(dep::spec::IntoIterFlatten<Dep>),
String(dep::spec::IntoIterFlatten<String>),
Uri(dep::spec::IntoIterFlatten<Uri>),
}
impl Iterator for DepSpecIntoIterFlatten {
type Item = *mut c_void;
fn next(&mut self) -> Option<Self::Item> {
match self {
Self::Dep(iter) => iter
.next()
.map(|x| Box::into_raw(Box::new(x)) as *mut c_void),
Self::String(iter) => iter
.next()
.map(|x| CString::new(x.as_str()).unwrap().into_raw() as *mut c_void),
Self::Uri(iter) => iter
.next()
.map(|x| Box::into_raw(Box::new(x)) as *mut c_void),
}
}
}
#[derive(Debug)]
pub enum DepSpecIntoIterRecursive {
Dep(dep::spec::IntoIterRecursive<Dep>),
String(dep::spec::IntoIterRecursive<String>),
Uri(dep::spec::IntoIterRecursive<Uri>),
}
impl Iterator for DepSpecIntoIterRecursive {
type Item = DepSpec;
fn next(&mut self) -> Option<Self::Item> {
match self {
Self::Dep(iter) => iter.next().map(DepSpec::new_dep),
Self::String(iter) => iter.next().map(DepSpec::new_string),
Self::Uri(iter) => iter.next().map(DepSpec::new_uri),
}
}
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_set_dependencies(
s: *const c_char,
eapi: *const Eapi,
) -> *mut DepSet {
let s = null_ptr_check!(s.as_ref());
let s = unsafe { unwrap_or_return!(CStr::from_ptr(s).to_str(), ptr::null_mut()) };
let eapi = unwrap_or_return!(IntoEapi::into_eapi(eapi), ptr::null_mut());
let opt_dep = unwrap_or_return!(dep::parse::dependencies(s, eapi), ptr::null_mut());
let dep = DepSet::new_dep(opt_dep.unwrap_or_default());
Box::into_raw(Box::new(dep))
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_set_restrict(s: *const c_char) -> *mut DepSet {
let s = null_ptr_check!(s.as_ref());
let s = unsafe { unwrap_or_return!(CStr::from_ptr(s).to_str(), ptr::null_mut()) };
let opt_dep = unwrap_or_return!(dep::parse::restrict(s), ptr::null_mut());
let dep = DepSet::new_string(opt_dep.unwrap_or_default(), DepSetKind::Restrict);
Box::into_raw(Box::new(dep))
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_set_required_use(
s: *const c_char,
eapi: *const Eapi,
) -> *mut DepSet {
let s = null_ptr_check!(s.as_ref());
let s = unsafe { unwrap_or_return!(CStr::from_ptr(s).to_str(), ptr::null_mut()) };
let eapi = unwrap_or_return!(IntoEapi::into_eapi(eapi), ptr::null_mut());
let opt_dep = unwrap_or_return!(dep::parse::required_use(s, eapi), ptr::null_mut());
let dep = DepSet::new_string(opt_dep.unwrap_or_default(), DepSetKind::RequiredUse);
Box::into_raw(Box::new(dep))
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_set_properties(s: *const c_char) -> *mut DepSet {
let s = null_ptr_check!(s.as_ref());
let s = unsafe { unwrap_or_return!(CStr::from_ptr(s).to_str(), ptr::null_mut()) };
let opt_dep = unwrap_or_return!(dep::parse::properties(s), ptr::null_mut());
let dep = DepSet::new_string(opt_dep.unwrap_or_default(), DepSetKind::Properties);
Box::into_raw(Box::new(dep))
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_set_src_uri(
s: *const c_char,
eapi: *const Eapi,
) -> *mut DepSet {
let s = null_ptr_check!(s.as_ref());
let s = unsafe { unwrap_or_return!(CStr::from_ptr(s).to_str(), ptr::null_mut()) };
let eapi = unwrap_or_return!(IntoEapi::into_eapi(eapi), ptr::null_mut());
let opt_dep = unwrap_or_return!(dep::parse::src_uri(s, eapi), ptr::null_mut());
let dep = DepSet::new_uri(opt_dep.unwrap_or_default());
Box::into_raw(Box::new(dep))
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_set_license(s: *const c_char) -> *mut DepSet {
let s = null_ptr_check!(s.as_ref());
let s = unsafe { unwrap_or_return!(CStr::from_ptr(s).to_str(), ptr::null_mut()) };
let opt_dep = unwrap_or_return!(dep::parse::license(s), ptr::null_mut());
let dep = DepSet::new_string(opt_dep.unwrap_or_default(), DepSetKind::License);
Box::into_raw(Box::new(dep))
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_set_str(d: *mut DepSet) -> *mut c_char {
let deps = null_ptr_check!(d.as_ref());
CString::new(deps.to_string()).unwrap().into_raw()
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_set_eq(d1: *mut DepSet, d2: *mut DepSet) -> bool {
let d1 = null_ptr_check!(d1.as_ref());
let d2 = null_ptr_check!(d2.as_ref());
d1.eq(d2)
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_set_hash(d: *mut DepSet) -> u64 {
let deps = null_ptr_check!(d.as_ref());
hash(deps)
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_set_into_iter(d: *mut DepSet) -> *mut DepSpecIntoIter {
let deps = null_ptr_check!(d.as_ref());
let iter = match deps.deref().clone() {
DepSetWrapper::Dep(d) => DepSpecIntoIter::Dep(d.into_iter()),
DepSetWrapper::String(d) => DepSpecIntoIter::String(d.into_iter()),
DepSetWrapper::Uri(d) => DepSpecIntoIter::Uri(d.into_iter()),
};
Box::into_raw(Box::new(iter))
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_set_into_iter_next(i: *mut DepSpecIntoIter) -> *mut DepSpec {
let iter = null_ptr_check!(i.as_mut());
iter.next()
.map(|x| Box::into_raw(Box::new(x)))
.unwrap_or(ptr::null_mut())
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_set_into_iter_free(i: *mut DepSpecIntoIter) {
if !i.is_null() {
unsafe { drop(Box::from_raw(i)) };
}
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_spec_cmp(d1: *mut DepSpec, d2: *mut DepSpec) -> c_int {
let d1 = null_ptr_check!(d1.as_ref());
let d2 = null_ptr_check!(d2.as_ref());
match d1.cmp(d2) {
Ordering::Less => -1,
Ordering::Equal => 0,
Ordering::Greater => 1,
}
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_spec_hash(d: *mut DepSpec) -> u64 {
let deps = null_ptr_check!(d.as_ref());
hash(deps)
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_spec_free(r: *mut DepSpec) {
if !r.is_null() {
unsafe { drop(Box::from_raw(r)) };
}
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_spec_str(d: *mut DepSpec) -> *mut c_char {
let deps = null_ptr_check!(d.as_ref());
CString::new(deps.to_string()).unwrap().into_raw()
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_spec_into_iter_flatten(
d: *mut DepSpec,
) -> *mut DepSpecIntoIterFlatten {
let dep = null_ptr_check!(d.as_ref());
let iter = match dep.deref().clone() {
DepSpecWrapper::Dep(d) => DepSpecIntoIterFlatten::Dep(d.into_iter_flatten()),
DepSpecWrapper::String(d) => DepSpecIntoIterFlatten::String(d.into_iter_flatten()),
DepSpecWrapper::Uri(d) => DepSpecIntoIterFlatten::Uri(d.into_iter_flatten()),
};
Box::into_raw(Box::new(iter))
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_set_into_iter_flatten(
d: *mut DepSet,
) -> *mut DepSpecIntoIterFlatten {
let deps = null_ptr_check!(d.as_ref());
let iter = match deps.deref().clone() {
DepSetWrapper::Dep(d) => DepSpecIntoIterFlatten::Dep(d.into_iter_flatten()),
DepSetWrapper::String(d) => DepSpecIntoIterFlatten::String(d.into_iter_flatten()),
DepSetWrapper::Uri(d) => DepSpecIntoIterFlatten::Uri(d.into_iter_flatten()),
};
Box::into_raw(Box::new(iter))
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_set_into_iter_flatten_next(
i: *mut DepSpecIntoIterFlatten,
) -> *mut c_void {
let iter = null_ptr_check!(i.as_mut());
iter.next().unwrap_or(ptr::null_mut())
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_set_into_iter_flatten_free(i: *mut DepSpecIntoIterFlatten) {
if !i.is_null() {
unsafe { drop(Box::from_raw(i)) };
}
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_spec_into_iter_recursive(
d: *mut DepSpec,
) -> *mut DepSpecIntoIterRecursive {
let dep = null_ptr_check!(d.as_ref());
let iter = match dep.deref().clone() {
DepSpecWrapper::Dep(d) => DepSpecIntoIterRecursive::Dep(d.into_iter_recursive()),
DepSpecWrapper::String(d) => DepSpecIntoIterRecursive::String(d.into_iter_recursive()),
DepSpecWrapper::Uri(d) => DepSpecIntoIterRecursive::Uri(d.into_iter_recursive()),
};
Box::into_raw(Box::new(iter))
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_set_into_iter_recursive(
d: *mut DepSet,
) -> *mut DepSpecIntoIterRecursive {
let deps = null_ptr_check!(d.as_ref());
let iter = match deps.deref().clone() {
DepSetWrapper::Dep(d) => DepSpecIntoIterRecursive::Dep(d.into_iter_recursive()),
DepSetWrapper::String(d) => DepSpecIntoIterRecursive::String(d.into_iter_recursive()),
DepSetWrapper::Uri(d) => DepSpecIntoIterRecursive::Uri(d.into_iter_recursive()),
};
Box::into_raw(Box::new(iter))
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_set_into_iter_recursive_next(
i: *mut DepSpecIntoIterRecursive,
) -> *mut DepSpec {
let iter = null_ptr_check!(i.as_mut());
iter.next()
.map(|x| Box::into_raw(Box::new(x)))
.unwrap_or(ptr::null_mut())
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_set_into_iter_recursive_free(
i: *mut DepSpecIntoIterRecursive,
) {
if !i.is_null() {
unsafe { drop(Box::from_raw(i)) };
}
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_dep_set_free(d: *mut DepSet) {
if !d.is_null() {
unsafe { drop(Box::from_raw(d)) };
}
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_uri_uri(u: *mut Uri) -> *mut c_char {
let uri = null_ptr_check!(u.as_ref());
CString::new(uri.uri()).unwrap().into_raw()
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_uri_rename(u: *mut Uri) -> *mut c_char {
let uri = null_ptr_check!(u.as_ref());
match uri.rename() {
None => ptr::null_mut(),
Some(s) => CString::new(s).unwrap().into_raw(),
}
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_uri_str(u: *mut Uri) -> *mut c_char {
let uri = null_ptr_check!(u.as_ref());
CString::new(uri.to_string()).unwrap().into_raw()
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_uri_free(u: *mut Uri) {
if !u.is_null() {
unsafe { drop(Box::from_raw(u)) };
}
}