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

use pkgcraft::atom::Version;
use pkgcraft::utils::hash;

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

/// Parse a string into a version.
///
/// Returns NULL on error.
///
/// # Safety
/// The version argument should point to a valid string.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_version(version: *const c_char) -> *mut AtomVersion {
    let ver_str = unsafe { unwrap_or_return!(CStr::from_ptr(version).to_str(), ptr::null_mut()) };
    let ver = unwrap_or_return!(Version::new(ver_str), ptr::null_mut());
    Box::into_raw(Box::new(ver))
}

/// Parse a string into a version with an operator.
///
/// Returns NULL on error.
///
/// # Safety
/// The version argument should point to a valid string.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_version_with_op(version: *const c_char) -> *mut AtomVersion {
    let ver_str = unsafe { unwrap_or_return!(CStr::from_ptr(version).to_str(), ptr::null_mut()) };
    let ver = unwrap_or_return!(Version::new_with_op(ver_str), ptr::null_mut());
    Box::into_raw(Box::new(ver))
}

/// Compare two versions returning -1, 0, or 1 if the first version is less than, equal to, or greater
/// than the second version, respectively.
///
/// # Safety
/// The version arguments should be non-null Version pointers received from pkgcraft_version().
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_version_cmp(v1: *mut AtomVersion, v2: *mut AtomVersion) -> c_int {
    let v1 = null_ptr_check!(v1.as_ref());
    let v2 = null_ptr_check!(v2.as_ref());

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

/// Return a version's revision, e.g. the version "1-r2" has a revision of "2".
///
/// # Safety
/// The version argument should be a non-null Version pointer received from pkgcraft_version().
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_version_revision(version: *mut AtomVersion) -> *mut c_char {
    let version = null_ptr_check!(version.as_ref());
    let s = version.revision().as_str();
    CString::new(s).unwrap().into_raw()
}

/// Return the string for a version.
///
/// # Safety
/// The version argument should be a non-null Version pointer received from pkgcraft_version().
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_version_str(version: *mut AtomVersion) -> *mut c_char {
    let version = null_ptr_check!(version.as_ref());
    CString::new(version.as_str()).unwrap().into_raw()
}

/// Free a version.
///
/// # Safety
/// The version argument should be a non-null Version pointer received from pkgcraft_version().
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_version_free(version: *mut AtomVersion) {
    if !version.is_null() {
        let _ = unsafe { Box::from_raw(version) };
    }
}

/// Return the hash value for a version.
///
/// # Safety
/// The version argument should be a non-null Version pointer received from pkgcraft_version().
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_version_hash(version: *mut AtomVersion) -> u64 {
    let version = null_ptr_check!(version.as_ref());
    hash(version)
}