-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestrict.go
86 lines (75 loc) · 2.35 KB
/
restrict.go
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
package pkgcraft
// #cgo pkg-config: pkgcraft
// #include <pkgcraft.h>
import "C"
import (
"fmt"
"runtime"
"unsafe"
)
type Restrict struct {
ptr *C.Restrict
}
func restrictFromPtr(ptr *C.Restrict) *Restrict {
restrict := &Restrict{ptr}
runtime.SetFinalizer(restrict, func(self *Restrict) { C.pkgcraft_restrict_free(self.ptr) })
return restrict
}
// Return a new restriction from a given object.
func NewRestrict(obj interface{}) (*Restrict, error) {
ptr, err := objectToRestrict(obj)
if ptr != nil {
restrict := restrictFromPtr(ptr)
return restrict, nil
} else {
return nil, err
}
}
// Try to convert a string to a restriction.
func stringToRestrict(s string) (*C.Restrict, error) {
if cpv, _ := NewCpv(s); cpv != nil {
return C.pkgcraft_cpv_restrict(cpv.ptr), nil
} else if dep, _ := NewDepCached(s); dep != nil {
return C.pkgcraft_dep_restrict(dep.ptr), nil
} else {
c_str := C.CString(s)
defer C.free(unsafe.Pointer(c_str))
if ptr := C.pkgcraft_restrict_parse_dep(c_str); ptr != nil {
return ptr, nil
} else if ptr := C.pkgcraft_restrict_parse_pkg(c_str); ptr != nil {
return ptr, nil
}
}
return nil, fmt.Errorf("invalid restriction string: %s", s)
}
// Try to convert an object to a restriction.
func objectToRestrict(obj interface{}) (*C.Restrict, error) {
switch obj := obj.(type) {
case *Cpv:
return C.pkgcraft_cpv_restrict(obj.ptr), nil
case *Dep:
return C.pkgcraft_dep_restrict(obj.ptr), nil
case *BasePkg:
return C.pkgcraft_pkg_restrict(obj.ptr), nil
case string:
return stringToRestrict(obj)
default:
return nil, fmt.Errorf("unsupported restrict type: %t", obj)
}
}
// Create a new restriction combining two restrictions via logical AND.
func (self *Restrict) And(other *Restrict) *Restrict {
return restrictFromPtr(C.pkgcraft_restrict_and(self.ptr, other.ptr))
}
// Create a new restriction combining two restrictions via logical OR.
func (self *Restrict) Or(other *Restrict) *Restrict {
return restrictFromPtr(C.pkgcraft_restrict_or(self.ptr, other.ptr))
}
// Create a new restriction combining two restrictions via logical XOR.
func (self *Restrict) Xor(other *Restrict) *Restrict {
return restrictFromPtr(C.pkgcraft_restrict_xor(self.ptr, other.ptr))
}
// Create a new restriction inverting a restriction via logical NOT.
func (self *Restrict) Not() *Restrict {
return restrictFromPtr(C.pkgcraft_restrict_not(self.ptr))
}