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
use std::ffi::CStr;
use std::os::raw::c_char;

use env_logger;

#[derive(Debug)]
pub enum RepoError {
}

#[derive(Debug)]
pub struct Repo {
    name: String,
    uri: String,
}

impl Repo {
    pub fn open_repo(name: &str) -> Result<Self, RepoError> {
        Ok(Repo {
            name: name.to_string(),
            uri: String::from("abc"),
        })
    }
}

fn cstr_to_string(s: *const c_char) -> String {
    unsafe {
        CStr::from_ptr(s).to_string_lossy().into_owned()
    }
}

#[no_mangle]
pub extern fn zbox_repo_open(name: *const c_char) -> *mut Repo {
    env_logger::init().unwrap();
    let name = cstr_to_string(name);
    let repo = Repo::open_repo(&name).unwrap();
    debug!("zbox_repo_open(): {:?}", repo);
    Box::into_raw(Box::new(repo))
}

#[no_mangle]
pub extern fn zbox_repo_close(repo: *mut Repo) {
    if repo.is_null() {
        return;
    }
    debug!("repo_repo_close(): {:?}", repo);
    unsafe {
        Box::from_raw(repo);
    }
}