use std::os::raw::{c_char, c_void};
use crate::enums::PkgFrom;
use crate::types::{alpm_list_t, AlpmList,AlpmListItem};
use crate::db::{alpm_db_t, AlpmDB};
#[link(name="alpm")]
extern {
fn alpm_pkg_find(pkg_list: *mut alpm_list_t, haystack: *const c_char) -> *mut alpm_pkg_t ;
fn alpm_pkg_free(pkg: *mut alpm_pkg_t) -> i32;
fn alpm_pkg_vercmp(a: *const c_char, b: *const c_char) -> i32;
fn alpm_pkg_checkmd5sum(pkg: *mut alpm_pkg_t) -> i32;
fn alpm_pkg_get_filename(pkg: *mut alpm_pkg_t) -> *const c_char;
fn alpm_pkg_get_base(pkg: *mut alpm_pkg_t) -> *const c_char;
fn alpm_pkg_get_base64_sig(pkg: *mut alpm_pkg_t) -> *const c_char;
fn alpm_pkg_get_name(pkg: *mut alpm_pkg_t) -> *const c_char;
fn alpm_pkg_get_version(pkg: *mut alpm_pkg_t) -> *const c_char;
fn alpm_pkg_get_origin(pkg: *mut alpm_pkg_t) -> PkgFrom;
fn alpm_pkg_get_desc(pkg: *mut alpm_pkg_t) -> *const c_char;
fn alpm_pkg_get_url(pkg: *mut alpm_pkg_t) -> *const c_char;
fn alpm_pkg_compute_requiredby(pkg: *mut alpm_pkg_t)-> *mut alpm_list_t;
fn alpm_pkg_compute_optionalfor(pkg: *mut alpm_pkg_t)-> *mut alpm_list_t;
fn alpm_pkg_get_validation(pkg: *mut alpm_pkg_t) -> i32;
fn alpm_pkg_get_db(pkg: *mut alpm_pkg_t) -> *mut alpm_db_t;
}
pub type PackageList = AlpmList<Package>;
#[repr(C)]
pub struct alpm_pkg_t{
pub (crate) __unused: [u8;0],}
impl alpm_pkg_t{
pub fn none() -> alpm_pkg_t{
alpm_pkg_t{
__unused: [],
}
}
}
pub struct Package {
pub (crate) pkg: *mut alpm_pkg_t,
}
impl AlpmListItem<Package> for Package{
fn new(data_ptr: *mut c_void) -> Package{
(data_ptr as *mut alpm_pkg_t).into()
}
fn to_ptr(&self) -> *mut c_void {
self.pkg as *mut c_void
}
}
impl PackageList {
pub fn find(&self, haystack: &str) -> Package{
unsafe{
alpm_pkg_find(self.list, strc!(haystack)).into()
}
}
}
impl From<*mut alpm_pkg_t> for Package{
fn from(c_pkg: *mut alpm_pkg_t) -> Package{
Package{
pkg: c_pkg,
}
}
}
impl Drop for Package {
fn drop(&mut self) {
self.free();
}
}
impl Package{
pub fn vercmp(a : &str, b :&str) -> i32 {
unsafe{
alpm_pkg_vercmp(
strc!(a),
strc!(b)
)
}
}
pub fn validation(&self) -> i32{
unsafe{
alpm_pkg_get_validation(self.pkg)
}
}
pub fn filename(&self) -> &str {
unsafe{
cstr!(alpm_pkg_get_filename(self.pkg))
}
}
pub fn base(&self) -> &str {
unsafe{
cstr!(alpm_pkg_get_base(self.pkg))
}
}
pub fn base64_sig(&self) -> &str {
unsafe{
cstr!(alpm_pkg_get_base64_sig(self.pkg))
}
}
pub fn name(&self) -> &str {
unsafe{
cstr!(alpm_pkg_get_name(self.pkg))
}
}
pub fn version(&self) -> &str {
unsafe{
cstr!(alpm_pkg_get_version(self.pkg))
}
}
pub fn origin(&self) -> PkgFrom {
unsafe{
alpm_pkg_get_origin(self.pkg)
}
}
pub fn description(&self) -> &str {
unsafe{
cstr!(alpm_pkg_get_desc(self.pkg))
}
}
pub fn db(&self) -> AlpmDB{
unsafe{
alpm_pkg_get_db(self.pkg).into()
}
}
pub fn url(&self) -> &str {
unsafe{
cstr!(alpm_pkg_get_url(self.pkg))
}
}
pub fn required_by(&self) -> PackageList {
unsafe{
alpm_pkg_compute_requiredby(self.pkg).into()
}
}
pub fn optional_for(&self) -> PackageList {
unsafe{
alpm_pkg_compute_optionalfor(self.pkg).into()
}
}
pub fn checkmd5sum(&self) -> bool {
unsafe{
to_bool!(alpm_pkg_checkmd5sum(self.pkg))
}
}
pub fn free(&self) -> bool {
unsafe{
to_bool!(alpm_pkg_free(self.pkg))
}
}
}