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
use std::ffi::{c_char, CStr};
use std::ptr;

use pkgcraft::dep::{parse, Dep, Version};
use pkgcraft::eapi::{Eapi, IntoEapi};

use crate::macros::*;

/// Parse a package dependency.
///
/// Returns NULL on error.
///
/// # Safety
/// The eapi argument may be NULL to use the default EAPI.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_parse_dep(s: *const c_char, eapi: *const Eapi) -> *const c_char {
    let val = null_ptr_check!(s.as_ref());
    let val = unsafe { unwrap_or_return!(CStr::from_ptr(val).to_str(), ptr::null()) };
    let eapi = unwrap_or_return!(IntoEapi::into_eapi(eapi), ptr::null());
    unwrap_or_return!(Dep::valid(val, eapi), ptr::null());
    s
}

/// Parse a package category.
///
/// Returns NULL on error.
///
/// # Safety
/// The argument should point to a UTF-8 string.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_parse_category(s: *const c_char) -> *const c_char {
    let val = null_ptr_check!(s.as_ref());
    let val = unsafe { unwrap_or_return!(CStr::from_ptr(val).to_str(), ptr::null()) };
    unwrap_or_return!(parse::category(val), ptr::null());
    s
}

/// Parse a package name.
///
/// Returns NULL on error.
///
/// # Safety
/// The argument should point to a UTF-8 string.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_parse_package(s: *const c_char) -> *const c_char {
    let val = null_ptr_check!(s.as_ref());
    let val = unsafe { unwrap_or_return!(CStr::from_ptr(val).to_str(), ptr::null()) };
    unwrap_or_return!(parse::package(val), ptr::null());
    s
}

/// Parse a package version.
///
/// Returns NULL on error.
///
/// # Safety
/// The argument should point to a UTF-8 string.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_parse_version(s: *const c_char) -> *const c_char {
    let val = null_ptr_check!(s.as_ref());
    let val = unsafe { unwrap_or_return!(CStr::from_ptr(val).to_str(), ptr::null()) };
    unwrap_or_return!(Version::valid(val), ptr::null());
    s
}

/// Parse a package repo.
///
/// Returns NULL on error.
///
/// # Safety
/// The argument should point to a UTF-8 string.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_parse_repo(s: *const c_char) -> *const c_char {
    let val = null_ptr_check!(s.as_ref());
    let val = unsafe { unwrap_or_return!(CStr::from_ptr(val).to_str(), ptr::null()) };
    unwrap_or_return!(parse::repo(val), ptr::null());
    s
}

/// Parse a package CPV.
///
/// Returns NULL on error.
///
/// # Safety
/// The argument should point to a UTF-8 string.
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_parse_cpv(s: *const c_char) -> *const c_char {
    let val = null_ptr_check!(s.as_ref());
    let val = unsafe { unwrap_or_return!(CStr::from_ptr(val).to_str(), ptr::null()) };
    unwrap_or_return!(Dep::valid_cpv(val), ptr::null());
    s
}