mirror of
https://github.com/git/git.git
synced 2026-01-12 13:53:11 +09:00
The C functions exported by libgit-sys do not provide an idiomatic Rust interface. To make it easier to use these functions via Rust, add a higher-level "libgit" crate, that wraps the lower-level configset API with an interface that is more Rust-y. This combination of $X and $X-sys crates is a common pattern for FFI in Rust, as documented in "The Cargo Book" [1]. [1] https://doc.rust-lang.org/cargo/reference/build-scripts.html#-sys-packages Co-authored-by: Josh Steadmon <steadmon@google.com> Signed-off-by: Josh Steadmon <steadmon@google.com> Signed-off-by: Calvin Wan <calvinwan@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
80 lines
2.0 KiB
Rust
80 lines
2.0 KiB
Rust
use std::ffi::c_void;
|
|
|
|
#[cfg(has_std__ffi__c_char)]
|
|
use std::ffi::{c_char, c_int};
|
|
|
|
#[cfg(not(has_std__ffi__c_char))]
|
|
#[allow(non_camel_case_types)]
|
|
pub type c_char = i8;
|
|
|
|
#[cfg(not(has_std__ffi__c_char))]
|
|
#[allow(non_camel_case_types)]
|
|
pub type c_int = i32;
|
|
|
|
extern crate libz_sys;
|
|
|
|
#[allow(non_camel_case_types)]
|
|
#[repr(C)]
|
|
pub struct libgit_config_set {
|
|
_data: [u8; 0],
|
|
_marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>,
|
|
}
|
|
|
|
extern "C" {
|
|
pub fn free(ptr: *mut c_void);
|
|
|
|
pub fn libgit_user_agent() -> *const c_char;
|
|
pub fn libgit_user_agent_sanitized() -> *const c_char;
|
|
|
|
pub fn libgit_configset_alloc() -> *mut libgit_config_set;
|
|
pub fn libgit_configset_free(cs: *mut libgit_config_set);
|
|
|
|
pub fn libgit_configset_add_file(cs: *mut libgit_config_set, filename: *const c_char) -> c_int;
|
|
|
|
pub fn libgit_configset_get_int(
|
|
cs: *mut libgit_config_set,
|
|
key: *const c_char,
|
|
int: *mut c_int,
|
|
) -> c_int;
|
|
|
|
pub fn libgit_configset_get_string(
|
|
cs: *mut libgit_config_set,
|
|
key: *const c_char,
|
|
dest: *mut *mut c_char,
|
|
) -> c_int;
|
|
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use std::ffi::CStr;
|
|
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn user_agent_starts_with_git() {
|
|
let c_str = unsafe { CStr::from_ptr(libgit_user_agent()) };
|
|
let agent = c_str
|
|
.to_str()
|
|
.expect("User agent contains invalid UTF-8 data");
|
|
assert!(
|
|
agent.starts_with("git/"),
|
|
r#"Expected user agent to start with "git/", got: {}"#,
|
|
agent
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn sanitized_user_agent_starts_with_git() {
|
|
let c_str = unsafe { CStr::from_ptr(libgit_user_agent_sanitized()) };
|
|
let agent = c_str
|
|
.to_str()
|
|
.expect("Sanitized user agent contains invalid UTF-8 data");
|
|
assert!(
|
|
agent.starts_with("git/"),
|
|
r#"Expected user agent to start with "git/", got: {}"#,
|
|
agent
|
|
);
|
|
}
|
|
}
|