aboutsummaryrefslogtreecommitdiff
path: root/nix-rust/src/c.rs
blob: c1358545fd1944f43f98cb680f0e7d902697a750 (plain)
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
use super::{error, store::path, store::StorePath, util};

#[no_mangle]
pub unsafe extern "C" fn ffi_String_new(s: &str, out: *mut String) {
    // FIXME: check whether 's' is valid UTF-8?
    out.write(s.to_string())
}

#[no_mangle]
pub unsafe extern "C" fn ffi_String_drop(self_: *mut String) {
    std::ptr::drop_in_place(self_);
}

#[no_mangle]
pub extern "C" fn ffi_StorePath_new(
    path: &str,
    store_dir: &str,
) -> Result<StorePath, error::CppException> {
    StorePath::new(std::path::Path::new(path), std::path::Path::new(store_dir))
        .map_err(|err| err.into())
}

#[no_mangle]
pub extern "C" fn ffi_StorePath_new2(
    hash: &[u8; crate::store::path::STORE_PATH_HASH_BYTES],
    name: &str,
) -> Result<StorePath, error::CppException> {
    StorePath::from_parts(*hash, name).map_err(|err| err.into())
}

#[no_mangle]
pub extern "C" fn ffi_StorePath_fromBaseName(
    base_name: &str,
) -> Result<StorePath, error::CppException> {
    StorePath::new_from_base_name(base_name).map_err(|err| err.into())
}

#[no_mangle]
pub unsafe extern "C" fn ffi_StorePath_drop(self_: *mut StorePath) {
    std::ptr::drop_in_place(self_);
}

#[no_mangle]
pub extern "C" fn ffi_StorePath_to_string(self_: &StorePath) -> Vec<u8> {
    let mut buf = vec![0; path::STORE_PATH_HASH_CHARS + 1 + self_.name.name().len()];
    util::base32::encode_into(self_.hash.hash(), &mut buf[0..path::STORE_PATH_HASH_CHARS]);
    buf[path::STORE_PATH_HASH_CHARS] = b'-';
    buf[path::STORE_PATH_HASH_CHARS + 1..].clone_from_slice(self_.name.name().as_bytes());
    buf
}

#[no_mangle]
pub extern "C" fn ffi_StorePath_less_than(a: &StorePath, b: &StorePath) -> bool {
    a < b
}

#[no_mangle]
pub extern "C" fn ffi_StorePath_eq(a: &StorePath, b: &StorePath) -> bool {
    a == b
}

#[no_mangle]
pub extern "C" fn ffi_StorePath_clone(self_: &StorePath) -> StorePath {
    self_.clone()
}

#[no_mangle]
pub extern "C" fn ffi_StorePath_name(self_: &StorePath) -> &str {
    self_.name.name()
}

#[no_mangle]
pub extern "C" fn ffi_StorePath_hash_data(
    self_: &StorePath,
) -> &[u8; crate::store::path::STORE_PATH_HASH_BYTES] {
    self_.hash.hash()
}