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
use std::cmp::Ordering;
use std::ffi::{CStr, CString};
use std::os::raw::{c_char, c_int};
use std::str::FromStr;
use std::{mem, ptr};

use pkgcraft::eapi::{self, Eapi};
use pkgcraft::utils::hash;

use crate::macros::*;

/// Get all known EAPIS.
///
/// # Safety
/// The returned array must be freed via pkgcraft_eapis_free().
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_eapis(len: *mut usize) -> *mut *const Eapi {
    let mut ptrs: Vec<_> = eapi::EAPIS.values().map(|&e| e as *const _).collect();
    ptrs.shrink_to_fit();
    unsafe { *len = ptrs.len() };
    let p = ptrs.as_mut_ptr();
    mem::forget(ptrs);
    p
}

/// Get all official EAPIS.
///
/// # Safety
/// The returned array must be freed via pkgcraft_eapis_free().
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_eapis_official(len: *mut usize) -> *mut *const Eapi {
    let mut ptrs: Vec<_> = eapi::EAPIS_OFFICIAL
        .values()
        .copied()
        .map(|e| e as *const _)
        .collect();
    ptrs.shrink_to_fit();
    unsafe { *len = ptrs.len() };
    let p = ptrs.as_mut_ptr();
    mem::forget(ptrs);
    p
}

/// Free an array of borrowed Eapi objects.
///
/// # Safety
/// The argument must be the value received from pkgcraft_eapis(), pkgcraft_eapis_official(), or
/// NULL along with the length of the array.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_eapis_free(eapis: *mut *const Eapi, len: usize) {
    if !eapis.is_null() {
        unsafe { Vec::from_raw_parts(eapis, len, len) };
    }
}

/// Get an EAPI from its identifier.
///
/// Returns NULL on error.
///
/// # Safety
/// The argument must be a non-null string.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_eapi_from_str(s: *const c_char) -> *const Eapi {
    let s = null_ptr_check!(s.as_ref());
    let s = unsafe { unwrap_or_return!(CStr::from_ptr(s).to_str(), ptr::null()) };
    unwrap_or_return!(<&Eapi>::from_str(s), ptr::null())
}

/// Check if an EAPI has a feature.
///
/// # Safety
/// The arguments must be a non-null Eapi pointer and non-null string.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_eapi_has(eapi: *const Eapi, s: *const c_char) -> bool {
    let eapi = null_ptr_check!(eapi.as_ref());
    let s = null_ptr_check!(s.as_ref());
    let s = unsafe { unwrap_or_return!(CStr::from_ptr(s).to_str(), false) };
    let feature = unwrap_or_return!(eapi::Feature::from_str(s), false);
    eapi.has(feature)
}

/// Return an EAPI's identifier.
///
/// # Safety
/// The arguments must be a non-null Eapi pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_eapi_as_str(eapi: *const Eapi) -> *mut c_char {
    let eapi = null_ptr_check!(eapi.as_ref());
    CString::new(eapi.as_str()).unwrap().into_raw()
}

/// Compare two Eapi objects chronologically 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 Eapi pointers.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_eapi_cmp(e1: *const Eapi, e2: *const Eapi) -> c_int {
    let eapi1 = null_ptr_check!(e1.as_ref());
    let eapi2 = null_ptr_check!(e2.as_ref());

    match eapi1.cmp(eapi2) {
        Ordering::Less => -1,
        Ordering::Equal => 0,
        Ordering::Greater => 1,
    }
}

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

/// Convert EAPI range into an array of Eapi objects.
///
/// Returns NULL on error.
///
/// # Safety
/// The argument must be a non-null string.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_eapis_range(
    s: *const c_char,
    len: *mut usize,
) -> *mut *const Eapi {
    let s = null_ptr_check!(s.as_ref());
    let s = unsafe { unwrap_or_return!(CStr::from_ptr(s).to_str(), ptr::null_mut()) };
    let eapis = unwrap_or_return!(eapi::range(s), ptr::null_mut());
    let mut ptrs: Vec<_> = eapis.iter().map(|&e| e as *const _).collect();
    ptrs.shrink_to_fit();
    unsafe { *len = ptrs.len() };
    let p = ptrs.as_mut_ptr();
    mem::forget(ptrs);
    p
}