use std::ffi::{c_char, c_int};
use pkgcraft::config::{Config, Repos};
use pkgcraft::repo::set::RepoSet;
use pkgcraft::repo::Repo;
use crate::macros::*;
use crate::panic::ffi_catch_panic;
#[no_mangle]
pub extern "C" fn pkgcraft_config_new() -> *mut Config {
let config = Config::new("pkgcraft", "").collapse();
Box::into_raw(Box::new(config))
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_config_add_repo_path(
c: *mut Config,
id: *const c_char,
priority: c_int,
path: *const c_char,
external: bool,
) -> *mut Repo {
ffi_catch_panic! {
let path = try_str_from_ptr!(path);
let id = if id.is_null() {
path
} else {
try_str_from_ptr!(id)
};
let config = try_mut_from_ptr!(c);
let repo = unwrap_or_panic!(config.add_repo_path(id, priority, path, external));
Box::into_raw(Box::new(repo))
}
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_config_add_repo(
c: *mut Config,
r: *mut Repo,
external: bool,
) -> *mut Repo {
ffi_catch_panic! {
let config = try_mut_from_ptr!(c);
let repo = try_ref_from_ptr!(r);
unwrap_or_panic!(config.add_repo(repo, external));
r
}
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_config_load(c: *mut Config) -> *mut Config {
ffi_catch_panic! {
let config = try_mut_from_ptr!(c);
unwrap_or_panic!(config.load());
c
}
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_config_load_portage_conf(
c: *mut Config,
path: *const c_char,
) -> *mut Config {
ffi_catch_panic! {
let path = try_opt_str_from_ptr!(path);
let config = try_mut_from_ptr!(c);
unwrap_or_panic!(config.load_portage_conf(path));
c
}
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_config_repos(
c: *mut Config,
len: *mut usize,
) -> *mut *const Repo {
let config = try_ref_from_ptr!(c);
iter_to_array!(config.repos.into_iter(), len, |(_, r)| { r as *const _ })
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_config_repos_set(c: *mut Config, kind: Repos) -> *mut RepoSet {
let config = try_ref_from_ptr!(c);
Box::into_raw(Box::new(config.repos.set(kind)))
}
#[no_mangle]
pub unsafe extern "C" fn pkgcraft_config_free(c: *mut Config) {
if !c.is_null() {
unsafe { drop(Box::from_raw(c)) };
}
}