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
use std::cmp::Ordering;
use std::os::raw::c_int;
use std::ptr;

use pkgcraft::atom::Atom;
use pkgcraft::pkg::ebuild::Pkg as EbuildPkg;
use pkgcraft::pkg::Package;
use pkgcraft::{eapi, pkg, repo, restrict, utils::hash, Error};

use crate::macros::*;
use crate::types::AtomVersion;

pub mod ebuild;

/// Return a package's atom.
///
/// # Safety
/// The argument must be a non-null Pkg pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_pkg_atom(p: *mut pkg::Pkg) -> *const Atom {
    let pkg = null_ptr_check!(p.as_ref());
    pkg.atom()
}

/// Return a package's repo.
///
/// # Safety
/// The argument must be a non-null Pkg pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_pkg_repo(p: *mut pkg::Pkg) -> *const repo::Repo {
    let pkg = null_ptr_check!(p.as_ref());
    pkg.repo()
}

/// Return a package's EAPI.
///
/// # Safety
/// The argument must be a non-null Pkg pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_pkg_eapi(p: *mut pkg::Pkg) -> *const eapi::Eapi {
    let pkg = null_ptr_check!(p.as_ref());
    pkg.eapi()
}

/// Return a package's version.
///
/// # Safety
/// The argument must be a non-null Pkg pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_pkg_version(p: *mut pkg::Pkg) -> *const AtomVersion {
    let pkg = null_ptr_check!(p.as_ref());
    pkg.version()
}

/// Compare two packages returning -1, 0, or 1 if the first package is less than, equal to, or
/// greater than the second package, respectively.
///
/// # Safety
/// The arguments must be non-null Pkg pointers.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_pkg_cmp<'a>(
    p1: *mut pkg::Pkg<'a>,
    p2: *mut pkg::Pkg<'a>,
) -> c_int {
    let pkg1 = null_ptr_check!(p1.as_ref());
    let pkg2 = null_ptr_check!(p2.as_ref());

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

/// Convert a Pkg into an EbuildPkg.
///
/// Returns NULL on error.
///
/// # Safety
/// The argument must be a non-null Pkg pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_pkg_as_ebuild(p: *mut pkg::Pkg) -> *const EbuildPkg {
    let pkg = null_ptr_check!(p.as_ref());
    let result = match pkg.as_ebuild() {
        Some((pkg, _repo)) => Ok(pkg),
        None => Err(Error::InvalidValue("invalid pkg format".to_string())),
    };
    unwrap_or_return!(result, ptr::null())
}

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

/// Return the restriction for a package.
///
/// # Safety
/// The argument must be a non-null Pkg pointer.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_pkg_restrict(p: *mut pkg::Pkg) -> *mut restrict::Restrict {
    let pkg = null_ptr_check!(p.as_ref());
    Box::into_raw(Box::new(pkg.into()))
}

/// Free an package.
///
/// # Safety
/// The argument must be a non-null Pkg pointer or NULL.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_pkg_free(p: *mut pkg::Pkg) {
    if !p.is_null() {
        unsafe { drop(Box::from_raw(p)) };
    }
}