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
use std::cmp::Ordering;
use std::ffi::{c_char, c_int, c_void, CString};
use std::{fmt, ptr};

use pkgcraft::atom::Atom;
use pkgcraft::depset::{self, IntoIteratorFlatten, Uri};
use pkgcraft::utils::hash;

use crate::macros::*;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum DepSet {
    Atom(depset::DepSet<Atom>),
    String(depset::DepSet<String>),
    Uri(depset::DepSet<Uri>),
}

impl fmt::Display for DepSet {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Atom(d) => write!(f, "{}", d),
            Self::String(d) => write!(f, "{}", d),
            Self::Uri(d) => write!(f, "{}", d),
        }
    }
}

#[derive(Debug)]
pub enum DepSetIntoIter {
    Atom(depset::DepSetIntoIter<Atom>),
    String(depset::DepSetIntoIter<String>),
    Uri(depset::DepSetIntoIter<Uri>),
}

impl Iterator for DepSetIntoIter {
    type Item = DepRestrict;

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            Self::Atom(iter) => iter.next().map(DepRestrict::Atom),
            Self::String(iter) => iter.next().map(DepRestrict::String),
            Self::Uri(iter) => iter.next().map(DepRestrict::Uri),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum DepRestrict {
    Atom(depset::DepRestrict<Atom>),
    String(depset::DepRestrict<String>),
    Uri(depset::DepRestrict<Uri>),
}

impl fmt::Display for DepRestrict {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Atom(d) => write!(f, "{}", d),
            Self::String(d) => write!(f, "{}", d),
            Self::Uri(d) => write!(f, "{}", d),
        }
    }
}

#[derive(Debug)]
pub enum DepSetIntoIterFlatten {
    Atom(depset::DepSetIntoIterFlatten<Atom>),
    String(depset::DepSetIntoIterFlatten<String>),
    Uri(depset::DepSetIntoIterFlatten<Uri>),
}

impl Iterator for DepSetIntoIterFlatten {
    type Item = *mut c_void;

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            Self::Atom(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),
        }
    }
}

/// Return the formatted string for a DepSet object.
///
/// # Safety
/// The argument must be a non-null DepSet pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_depset_str(d: *mut DepSet) -> *mut c_char {
    let deps = null_ptr_check!(d.as_ref());
    CString::new(deps.to_string()).unwrap().into_raw()
}

/// Determine if two DepSets are equal.
///
/// # Safety
/// The arguments must be non-null DepSet pointers.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_depset_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)
}

/// Return the hash value for a DepSet.
///
/// # Safety
/// The argument must be a non-null DepSet pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_depset_hash(d: *mut DepSet) -> u64 {
    let deps = null_ptr_check!(d.as_ref());
    hash(deps)
}

/// Return an iterator for a depset.
///
/// # Safety
/// The argument must be a non-null DepSet pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_depset_into_iter(d: *mut DepSet) -> *mut DepSetIntoIter {
    let deps = null_ptr_check!(d.as_ref());
    let iter = match deps.clone() {
        DepSet::Atom(d) => DepSetIntoIter::Atom(d.into_iter()),
        DepSet::String(d) => DepSetIntoIter::String(d.into_iter()),
        DepSet::Uri(d) => DepSetIntoIter::Uri(d.into_iter()),
    };
    Box::into_raw(Box::new(iter))
}

/// Return the next object from a depset iterator.
///
/// Returns NULL when the iterator is empty.
///
/// # Safety
/// The argument must be a non-null DepSetIntoIter pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_depset_into_iter_next(
    i: *mut DepSetIntoIter,
) -> *mut DepRestrict {
    let iter = null_ptr_check!(i.as_mut());
    iter.next()
        .map(|x| Box::into_raw(Box::new(x)))
        .unwrap_or(ptr::null_mut())
}

/// Free a depset iterator.
///
/// # Safety
/// The argument must be a non-null DepSetIntoIter pointer or NULL.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_depset_into_iter_free(i: *mut DepSetIntoIter) {
    if !i.is_null() {
        unsafe { drop(Box::from_raw(i)) };
    }
}

/// Compare two DepRestricts returning -1, 0, or 1 if the first is less than, equal to, or greater
/// than the second, respectively.
///
/// # Safety
/// The arguments must be non-null DepRestrict pointers.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_deprestrict_cmp(
    d1: *mut DepRestrict,
    d2: *mut DepRestrict,
) -> 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,
    }
}

/// Return the hash value for a DepRestrict.
///
/// # Safety
/// The argument must be a non-null DepRestrict pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_deprestrict_hash(d: *mut DepRestrict) -> u64 {
    let deps = null_ptr_check!(d.as_ref());
    hash(deps)
}

/// Free a DepRestrict object.
///
/// # Safety
/// The argument must be a DepRestrict pointer or NULL.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_deprestrict_free(r: *mut DepRestrict) {
    if !r.is_null() {
        unsafe { drop(Box::from_raw(r)) };
    }
}

/// Return the formatted string for a DepRestrict object.
///
/// # Safety
/// The argument must be a non-null DepRestrict pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_deprestrict_str(d: *mut DepRestrict) -> *mut c_char {
    let deps = null_ptr_check!(d.as_ref());
    CString::new(deps.to_string()).unwrap().into_raw()
}

/// Return an iterator for a flattened DepRestrict.
///
/// # Safety
/// The argument must be a non-null DepRestrict pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_deprestrict_into_iter_flatten(
    d: *mut DepRestrict,
) -> *mut DepSetIntoIterFlatten {
    let deps = null_ptr_check!(d.as_ref());
    let iter = match deps.clone() {
        DepRestrict::Atom(d) => DepSetIntoIterFlatten::Atom(d.into_iter_flatten()),
        DepRestrict::String(d) => DepSetIntoIterFlatten::String(d.into_iter_flatten()),
        DepRestrict::Uri(d) => DepSetIntoIterFlatten::Uri(d.into_iter_flatten()),
    };
    Box::into_raw(Box::new(iter))
}

/// Return an iterator for a flattened DepSet.
///
/// # Safety
/// The argument must be a non-null DepSet pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_depset_into_iter_flatten(
    d: *mut DepSet,
) -> *mut DepSetIntoIterFlatten {
    let deps = null_ptr_check!(d.as_ref());
    let iter = match deps.clone() {
        DepSet::Atom(d) => DepSetIntoIterFlatten::Atom(d.into_iter_flatten()),
        DepSet::String(d) => DepSetIntoIterFlatten::String(d.into_iter_flatten()),
        DepSet::Uri(d) => DepSetIntoIterFlatten::Uri(d.into_iter_flatten()),
    };
    Box::into_raw(Box::new(iter))
}

/// Return the next object from a flattened depset iterator.
///
/// Returns NULL when the iterator is empty.
///
/// # Safety
/// The argument must be a non-null DepSetIntoIterFlatten pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_depset_into_iter_flatten_next(
    i: *mut DepSetIntoIterFlatten,
) -> *mut c_void {
    let iter = null_ptr_check!(i.as_mut());
    iter.next().unwrap_or(ptr::null_mut())
}

/// Free a flattened depset iterator.
///
/// # Safety
/// The argument must be a non-null DepSetIntoIterFlatten pointer or NULL.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_depset_into_iter_flatten_free(i: *mut DepSetIntoIterFlatten) {
    if !i.is_null() {
        unsafe { drop(Box::from_raw(i)) };
    }
}

/// Free a DepSet.
///
/// # Safety
/// The argument must be a DepSet pointer or NULL.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_depset_free(d: *mut DepSet) {
    if !d.is_null() {
        unsafe { drop(Box::from_raw(d)) };
    }
}

/// Get the main URI from a Uri object.
///
/// # Safety
/// The argument must be a Uri pointer.
#[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()
}

/// Get the filename rename for a Uri.
///
/// Returns NULL when no rename exists.
///
/// # Safety
/// The argument must be a Uri pointer.
#[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(),
    }
}

/// Return the formatted string for a Uri object.
///
/// # Safety
/// The argument must be a Uri pointer.
#[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()
}

/// Free a Uri object.
///
/// # Safety
/// The argument must be a non-null Uri pointer or NULL.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_uri_free(u: *mut Uri) {
    if !u.is_null() {
        unsafe { drop(Box::from_raw(u)) };
    }
}