pkgcraft/
restrict.rs

1use std::ffi::c_char;
2
3use pkgcraft::restrict::{parse, Restrict};
4use pkgcraft::utils::hash;
5
6use crate::macros::*;
7use crate::panic::ffi_catch_panic;
8
9/// Parse a dependency restriction.
10///
11/// Returns NULL on error.
12///
13/// # Safety
14/// The argument must be a non-null string.
15#[no_mangle]
16pub unsafe extern "C" fn pkgcraft_restrict_parse_dep(s: *const c_char) -> *mut Restrict {
17    ffi_catch_panic! {
18        let s = try_str_from_ptr!(s);
19        let restrict = unwrap_or_panic!(parse::dep(s));
20        Box::into_raw(Box::new(restrict))
21    }
22}
23
24/// Parse a package query restriction.
25///
26/// Returns NULL on error.
27///
28/// # Safety
29/// The argument must be a non-null string.
30#[no_mangle]
31pub unsafe extern "C" fn pkgcraft_restrict_parse_pkg(s: *const c_char) -> *mut Restrict {
32    ffi_catch_panic! {
33        let s = try_str_from_ptr!(s);
34        let restrict = unwrap_or_panic!(parse::pkg(s));
35        Box::into_raw(Box::new(restrict))
36    }
37}
38
39/// Determine if two restrictions are equal.
40///
41/// # Safety
42/// The arguments must be non-null Restrict pointers.
43#[no_mangle]
44pub unsafe extern "C" fn pkgcraft_restrict_eq(r1: *mut Restrict, r2: *mut Restrict) -> bool {
45    let r1 = try_ref_from_ptr!(r1);
46    let r2 = try_ref_from_ptr!(r2);
47    r1.eq(r2)
48}
49
50/// Return the hash value for a restriction.
51///
52/// # Safety
53/// The argument must be a non-null Restrict pointer.
54#[no_mangle]
55pub unsafe extern "C" fn pkgcraft_restrict_hash(r: *mut Restrict) -> u64 {
56    let restrict = try_ref_from_ptr!(r);
57    hash(restrict)
58}
59
60/// Create a new restriction combining two restrictions via logical AND.
61///
62/// # Safety
63/// The arguments must be Restrict pointers.
64#[no_mangle]
65pub unsafe extern "C" fn pkgcraft_restrict_and(
66    r1: *mut Restrict,
67    r2: *mut Restrict,
68) -> *mut Restrict {
69    let r1 = try_ref_from_ptr!(r1);
70    let r2 = try_ref_from_ptr!(r2);
71    Box::into_raw(Box::new(r1.clone() & r2.clone()))
72}
73
74/// Create a new restriction combining two restrictions via logical OR.
75///
76/// # Safety
77/// The arguments must be Restrict pointers.
78#[no_mangle]
79pub unsafe extern "C" fn pkgcraft_restrict_or(
80    r1: *mut Restrict,
81    r2: *mut Restrict,
82) -> *mut Restrict {
83    let r1 = try_ref_from_ptr!(r1);
84    let r2 = try_ref_from_ptr!(r2);
85    Box::into_raw(Box::new(r1.clone() | r2.clone()))
86}
87
88/// Create a new restriction combining two restrictions via logical XOR.
89///
90/// # Safety
91/// The arguments must be Restrict pointers.
92#[no_mangle]
93pub unsafe extern "C" fn pkgcraft_restrict_xor(
94    r1: *mut Restrict,
95    r2: *mut Restrict,
96) -> *mut Restrict {
97    let r1 = try_ref_from_ptr!(r1);
98    let r2 = try_ref_from_ptr!(r2);
99    Box::into_raw(Box::new(r1.clone() ^ r2.clone()))
100}
101
102/// Create a new restriction inverting a restriction via logical NOT.
103///
104/// # Safety
105/// The arguments must be a Restrict pointer.
106#[no_mangle]
107pub unsafe extern "C" fn pkgcraft_restrict_not(r: *mut Restrict) -> *mut Restrict {
108    let r = try_ref_from_ptr!(r);
109    Box::into_raw(Box::new(!r.clone()))
110}
111
112/// Free a restriction.
113///
114/// # Safety
115/// The argument must be a Restrict pointer or NULL.
116#[no_mangle]
117pub unsafe extern "C" fn pkgcraft_restrict_free(r: *mut Restrict) {
118    if !r.is_null() {
119        unsafe { drop(Box::from_raw(r)) };
120    }
121}