libspm_rs/
lib.rs

1//! `LibSPM-rs` is a rust wrapper for [LibSPM](https://github.com/Soviet-Linux/CCCP)
2//!
3
4extern crate libc;
5// rust stuff version
6
7extern "C" {
8    fn version() -> libc::c_float;
9    fn clean();
10    fn update();
11    // using c_uchar here because String::as_ptr() returns a *const u8 pointer, so it's pretty easy
12    fn init();
13    fn get(p_name: *const libc::c_uchar, out_path: *const libc::c_uchar);
14    fn uninstall(p_name: *const libc::c_uchar);
15    fn installSpmFile(pkg_name: *const libc::c_uchar, as_dep: i32);
16    fn sync();
17}
18
19//  import libspm.rs
20
21const VERSION: &str = "0.04";
22
23
24///Checks if all dependencies are installed
25pub fn init_spm() {
26    unsafe {
27        init();
28    }
29}
30///Syncs package database
31pub fn sync_db() {
32    unsafe {
33        sync();
34    }
35}
36
37pub fn install_spm_file(pkg_name: String, as_dep: i32) {
38    unsafe {
39        installSpmFile(pkg_name.as_ptr(), as_dep);
40    }
41}
42
43/// Gets the current version
44pub fn get_version() -> f32 {
45    unsafe { crate::version() }
46}
47/// Cleans work dirs
48pub fn clean_dirs() {
49    unsafe {
50        crate::clean();
51    }
52}
53/// Gets a package by name
54pub fn get_package(package_name: String, out_path: String) {
55    unsafe {
56        get(package_name.as_ptr(), out_path.as_ptr());
57    }
58}
59
60/// Uninstall a package by name (and path)
61pub fn uninstall_package(package_name: String) {
62    unsafe {
63        uninstall(package_name.as_ptr());
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70
71    #[test]
72    fn version_test() {
73        println!(
74            "LibSPM version: {}\nLibSPM-rs wrapper version: {}",
75            get_version(),
76            VERSION
77        );
78    }
79
80    #[test]
81    fn test_clean() {
82        clean_dirs()
83    }
84
85    // this test shouldn't be ran as it promps the user for input (which might also cause it to
86    // panic or crash)
87    // #[test]
88    // fn test_get() {
89    //     get_package("testing".to_string(), "./fake".to_string());
90    // }
91}